Skip to content
Permalink
Browse files
Add GenericKeyedObjectPool.getKeys().
  • Loading branch information
Gary Gregory committed Apr 15, 2022
1 parent 0167e3b commit 0011710334ef3a836315765575d7e22443f9a6b6
Showing 3 changed files with 33 additions and 6 deletions.
@@ -61,6 +61,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add PooledObject.getFullDuration().
</action>
<action dev="ggregory" type="add" due-to="Vamsi Pavan Kumar Sanka, Phil Steitz, Gary Gregory">
Add GenericKeyedObjectPool.getKeys().
</action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Dependabot">
Bump actions/checkout from 2.3.4 to 3 #109, #112, #134.
@@ -221,7 +221,7 @@ public String toString() {
* to ensure any changes to the list of current keys is made in a
* thread-safe manner.
*/
private final List<K> poolKeyList = new ArrayList<>(); // @GuardedBy("keyLock")
private final ArrayList<K> poolKeyList = new ArrayList<>(); // @GuardedBy("keyLock")

private final ReadWriteLock keyLock = new ReentrantReadWriteLock(true);

@@ -1106,6 +1106,17 @@ public KeyedPooledObjectFactory<K, T> getFactory() {
return factory;
}

/**
* Gets a copy of the pool key list.
*
* @return a copy of the pool key list.
* @since 2.12.0
*/
@SuppressWarnings("unchecked")
public List<K> getKeys() {
return (List<K>) poolKeyList.clone();
}

/**
* Gets the cap on the number of "idle" instances per key in the pool.
* If maxIdlePerKey is set too low on heavily loaded systems it is possible
@@ -1201,15 +1212,15 @@ public int getNumIdle() {
return poolMap.values().stream().mapToInt(e -> e.getIdleObjects().size()).sum();
}


//--- JMX support ----------------------------------------------------------

@Override
public int getNumIdle(final K key) {
final ObjectDeque<T> objectDeque = poolMap.get(key);
return objectDeque != null ? objectDeque.getIdleObjects().size() : 0;
}


//--- JMX support ----------------------------------------------------------

/**
* Calculate the number of objects to test in a run of the idle object
* evictor.
@@ -1649,11 +1649,24 @@ public void testFIFO() throws Exception {
assertEquals( "key4", gkoPool.borrowObject(key),"new-4");
}

@Test
@Timeout(value = 60, unit = TimeUnit.SECONDS)
public void testGetKeys() throws Exception {
gkoPool.addObject("one");
assertEquals(1, gkoPool.getKeys().size());
gkoPool.addObject("two");
assertEquals(2, gkoPool.getKeys().size());
gkoPool.clear("one");
assertEquals(1, gkoPool.getKeys().size());
assertEquals("two", (String) gkoPool.getKeys().get(0));
gkoPool.clear();
}

@Test
public void testGetStatsString() {
assertNotNull((gkoPool.getStatsString()));
}

/**
* Verify that threads waiting on a depleted pool get served when a checked out object is
* invalidated.
@@ -1685,7 +1698,7 @@ public void testInvalidateFreesCapacity() throws Exception {
}
}
}

@Test
public void testInvalidateFreesCapacityForOtherKeys() throws Exception {
gkoPool.setMaxTotal(1);

0 comments on commit 0011710

Please sign in to comment.