/** * @see SimplePool#getIdleCount() * @return the idle count. */ public int getIdleCount() { return this.pool.getIdleCount(); }
/** * Set the pool size for deserialization buffers. * @param size the size, -1 for unlimited. * @since 4.3 */ public void setPoolSize(int size) { Assert.isNull(this.pool, "Cannot change pool size once set"); this.pool = new SimplePool<byte[]>(size, new PoolItemCallback<byte[]>() { @Override public byte[] createForPool() { return new byte[getMaxMessageSize()]; } @Override public boolean isStale(byte[] item) { return false; // never stale } @Override public void removedFromPool(byte[] item) { } }); this.pool.setWaitTimeout(this.poolWaitTimeout); }
/** * @see SimplePool#getAllocatedCount() * @return the allocated count. */ public int getAllocatedCount() { return this.pool.getAllocatedCount(); }
@Override public final byte[] deserialize(InputStream inputStream) throws IOException { byte[] buffer = this.pool == null ? new byte[getMaxMessageSize()] : this.pool.getItem(); try { return doDeserialize(inputStream, buffer); } finally { if (this.pool != null) { this.pool.releaseItem(buffer); } } }
final AtomicBoolean stale = new AtomicBoolean(); SimplePool<String> pool = stringPool(2, strings, stale); String s1 = pool.getItem(); assertEquals(0, pool.getIdleCount()); assertEquals(1, pool.getActiveCount()); assertEquals(1, pool.getAllocatedCount()); pool.releaseItem(s1); assertEquals(1, pool.getIdleCount()); assertEquals(0, pool.getActiveCount()); assertEquals(1, pool.getAllocatedCount()); s1 = pool.getItem(); assertEquals(0, pool.getIdleCount()); assertEquals(1, pool.getActiveCount()); assertEquals(1, pool.getAllocatedCount()); String s2 = pool.getItem(); assertNotSame(s1, s2); pool.setWaitTimeout(1); assertEquals(0, pool.getIdleCount()); assertEquals(2, pool.getActiveCount()); assertEquals(2, pool.getAllocatedCount()); try { pool.getItem(); fail("Expected exception"); pool.setPoolSize(4); assertEquals(0, pool.getIdleCount()); assertEquals(2, pool.getActiveCount()); assertEquals(2, pool.getAllocatedCount());
@Test public void testReuseAndStale() { final Set<String> strings = new HashSet<String>(); final AtomicBoolean stale = new AtomicBoolean(); SimplePool<String> pool = stringPool(2, strings, stale); String s1 = pool.getItem(); String s2 = pool.getItem(); assertNotSame(s1, s2); pool.releaseItem(s1); String s3 = pool.getItem(); assertSame(s1, s3); stale.set(true); pool.releaseItem(s3); s3 = pool.getItem(); assertNotSame(s1, s3); assertFalse(strings.remove(s1)); assertEquals(2, pool.getAllocatedCount()); }
@SuppressWarnings("unchecked") @Test public <F> void testServerException() throws Exception { SessionFactory<F> sf = mock(SessionFactory.class); CachingSessionFactory<F> csf = new CachingSessionFactory<F>(sf, 2); FileTransferringMessageHandler<F> handler = new FileTransferringMessageHandler<F>(csf); handler.setBeanFactory(mock(BeanFactory.class)); Session<F> session1 = newSession(); Session<F> session2 = newSession(); Session<F> session3 = newSession(); when(sf.getSession()).thenReturn(session1, session2, session3); handler.setRemoteDirectoryExpressionString("'foo'"); handler.afterPropertiesSet(); for (int i = 0; i < 3; i++) { try { handler.handleMessage(new GenericMessage<String>("hello")); } catch (Exception e) { assertEquals("test", e.getCause().getCause().getMessage()); } } verify(session1, times(1)).write(Mockito.any(InputStream.class), Mockito.anyString()); verify(session2, times(1)).write(Mockito.any(InputStream.class), Mockito.anyString()); verify(session3, times(1)).write(Mockito.any(InputStream.class), Mockito.anyString()); SimplePool<?> pool = TestUtils.getPropertyValue(csf, "pool", SimplePool.class); assertEquals(1, pool.getAllocatedCount()); assertEquals(1, pool.getIdleCount()); assertSame(session3, TestUtils.getPropertyValue(pool, "allocated", Set.class).iterator().next()); }
/** * @param connectionWaitTimeout the new timeout. * @see SimplePool#setWaitTimeout(long) */ public void setConnectionWaitTimeout(int connectionWaitTimeout) { this.pool.setWaitTimeout(connectionWaitTimeout); }
"'sessionFactory' cannot be a 'DelegatingSessionFactory'; cache each delegate instead"); this.sessionFactory = sessionFactory; this.pool = new SimplePool<Session<F>>(sessionCacheSize, new SimplePool.PoolItemCallback<Session<F>>() { @Override public Session<F> createForPool() {
/** * Get a session from the pool (or block if none available). */ @Override public Session<F> getSession() { return new CachedSession(this.pool.getItem(), this.sharedSessionEpoch); }
/** * @param poolSize the new pool size. * @see SimplePool#setPoolSize(int) */ public void setPoolSize(int poolSize) { this.pool.setPoolSize(poolSize); }
/** * Remove (close) any unused sessions in the pool. */ @Override public void destroy() { this.pool.removeAllIdleItems(); }
/** * @see SimplePool#getPoolSize() * @return the pool size. */ public int getPoolSize() { return this.pool.getPoolSize(); }
/** * @see SimplePool#getActiveCount() * @return the active count. */ public int getActiveCount() { return this.pool.getActiveCount(); }
@Override public void close() { if (!this.released.compareAndSet(false, true)) { if (logger.isDebugEnabled()) { logger.debug("Connection " + getConnectionId() + " has already been released"); } } else { /* * If the delegate is stopped, actually close the connection, but still release * it to the pool, it will be discarded/renewed the next time it is retrieved. */ if (!isRunning()) { if (logger.isDebugEnabled()) { logger.debug("Factory not running - closing " + getConnectionId()); } super.close(); } CachingClientConnectionFactory.this.pool.releaseItem(getTheConnection()); } }
@Test(expected = IllegalArgumentException.class) public void testForeignObject() { final Set<String> strings = new HashSet<String>(); final AtomicBoolean stale = new AtomicBoolean(); SimplePool<String> pool = stringPool(2, strings, stale); pool.getItem(); pool.releaseItem("Hello, world!"); }
/** * Set the pool wait timeout if a pool is configured, default unlimited. * @param poolWaitTimeout the timeout. */ public void setPoolWaitTimeout(long poolWaitTimeout) { this.poolWaitTimeout = poolWaitTimeout; if (this.pool != null) { this.pool.setWaitTimeout(poolWaitTimeout); } }
private SimplePool<String> stringPool(int size, final Set<String> strings, final AtomicBoolean stale) { SimplePool<String> pool = new SimplePool<String>(size, new SimplePool.PoolItemCallback<String>() { private int i; @Override public String createForPool() { String string = "String" + i++; strings.add(string); return string; } @Override public boolean isStale(String item) { if (stale.get()) { strings.remove(item); } return stale.get(); } @Override public void removedFromPool(String item) { strings.remove(item); } }); return pool; }
@Override public TcpConnectionSupport obtainConnection() throws Exception { return new CachedConnection(this.pool.getItem(), getListener()); }
/** * Modify the target session pool size; the actual pool size will adjust up/down * to this size as and when sessions are requested or retrieved. * * @param poolSize The pool size. */ public void setPoolSize(int poolSize) { this.pool.setPoolSize(poolSize); }