Skip to content

Commit 987f5b1

Browse files
committed
Add DefaultPooledObjectInfo.pooledObject()
1 parent f4fdc23 commit 987f5b1

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/main/java/org/apache/commons/pool3/impl/DefaultPooledObjectInfo.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ private String getTimeMillisFormatted(final long millis) {
101101
return new SimpleDateFormat(PATTERN).format(Long.valueOf(millis));
102102
}
103103

104+
/**
105+
* Gets the pooled object for debugging, use at your own risk, changing the state of this object may have unintended consequences for the pool.
106+
* <p>
107+
* This can't be a traditional getter as that would expose the pooled object via JMX.
108+
* </p>
109+
*
110+
* @return the pooled object for debugging, use at your own risk, changing the state of this object may have unintended consequences for the pool.
111+
* @since 2.14.0
112+
*/
113+
public PooledObject<?> pooledObject() {
114+
return pooledObject;
115+
}
116+
104117
/**
105118
* @since 2.4.3
106119
*/

src/test/java/org/apache/commons/pool3/impl/TestBaseGenericObjectPool.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import static org.junit.jupiter.api.Assertions.assertEquals;
2121
import static org.junit.jupiter.api.Assertions.assertFalse;
22+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2223
import static org.junit.jupiter.api.Assertions.assertTrue;
2324

2425
import java.lang.management.ManagementFactory;
@@ -134,11 +135,13 @@ void testEvictionTimerMultiplePools() throws InterruptedException {
134135
void testJMXRegistrationLatency() {
135136
final int numPools = 1000;
136137
final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
137-
final ArrayList<GenericObjectPool<Waiter, IllegalStateException>> pools = new ArrayList<>();
138+
final ArrayList<GenericObjectPool<Waiter, ?>> pools = new ArrayList<>();
138139
try {
139140
// final long startTime = System.currentTimeMillis();
140141
for (int i = 0; i < numPools; i++) {
141-
pools.add(new GenericObjectPool<>(new WaiterFactory<>(0, 0, 0, 0, 0, 0), new GenericObjectPoolConfig<>()));
142+
final GenericObjectPool<Waiter, ?> gop = new GenericObjectPool<>(new WaiterFactory<>(0, 0, 0, 0, 0, 0), new GenericObjectPoolConfig<>());
143+
assertNotNull(gop.getJmxName());
144+
pools.add(gop);
142145
}
143146
// System.out.println("Duration: " + (System.currentTimeMillis() - startTime));
144147
final ObjectName oname = pools.get(numPools - 1).getJmxName();

src/test/java/org/apache/commons/pool3/impl/TestDefaultPooledObjectInfo.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ void testGetLastBorrowTrace() throws Exception {
4545
}
4646
}
4747

48+
@Test
49+
void testGetPooledObject() throws Exception {
50+
try (GenericObjectPool<String, TestException> pool = new GenericObjectPool<>(new SimpleFactory())) {
51+
pool.borrowObject();
52+
final Set<DefaultPooledObjectInfo> strings = pool.listAllObjects();
53+
assertEquals(1, strings.size());
54+
final DefaultPooledObjectInfo s1Info = strings.iterator().next();
55+
assertEquals("0", s1Info.pooledObject().getObject());
56+
}
57+
}
58+
4859
@Test
4960
void testGetPooledObjectToString() throws Exception {
5061
try (GenericObjectPool<String, TestException> pool = new GenericObjectPool<>(new SimpleFactory())) {

0 commit comments

Comments
 (0)