在JBoss Remoting 2.2.2中存在这么一个bug,如果刚好客户端的timeout比服务器端处理时间短的话,会出现客户端连接池中的连接被无故用掉一个的状况,而且是没法回收的,终会导致很快客户端的连接池被占满的现象,在分析JBoss Remoting 2.2.2的代码后发现了问题的所在,同时查看了下JBoss Remoting 2.4的代码,发现在2.4中此bug已被修复。
  来看下JBoss Remoting 2.2.2中有问题的这段代码的片断:
synchronized(usedPoolLock)
{
if (pooled != null)
{
usedPooled++;
if (trace) log.trace(this + " got a socket, usedPooled: " + usedPooled);
break;
}
if (usedPooled < maxPoolSize)
{
// Try to get a socket.
if (trace) log.trace(this + " getting a socket, usedPooled: " + usedPooled);
usedPooled++;
}
else
{
retry = true;
if (trace) log.trace(this + " will try again to get a socket");
}
}
Socket socket = null;
long timestamp = System.currentTimeMillis();
try
{
if (trace) { log.trace(this + " creating socket " + (counter++) + ", attempt " + (i + 1)); }
socket = createSocket(address.address, address.port, timeRemaining);
if (trace) log.trace(this + " created socket: " + socket);
}
catch (Exception ex)
{
log.debug(this + " got Exception " + ex + ", creation attempt took " +
(System.currentTimeMillis() - timestamp) + " ms");
synchronized(usedPoolLock)
{
usedPooled--;
}
if (i + 1 < numberOfRetries)
{
Thread.sleep(1);
continue;
}
throw ex;
}
socket.setTcpNoDelay(address.enableTcpNoDelay);
Map metadata = getLocator().getParameters();
if (metadata == null)
{
metadata = new HashMap(2);
}
else
{
metadata = new HashMap(metadata);
}
metadata.put(SocketWrapper.MARSHALLER, marshaller);
metadata.put(SocketWrapper.UNMARSHALLER, unmarshaller);
if (timeAllowed > 0)
{
timeRemaining = (int) (timeAllowed - (System.currentTimeMillis() - start));
if (timeRemaining <= 0)
break;
metadata.put(SocketWrapper.TEMP_TIMEOUT, new Integer(timeRemaining));
}
pooled = createClientSocket(socket, address.timeout, metadata);