use of com.mongodb.event.ConnectionPoolWaitQueueEnteredEvent in project mongo-java-driver by mongodb.
the class DefaultConnectionPool method get.
@Override
public InternalConnection get(final long timeout, final TimeUnit timeUnit) {
try {
if (waitQueueSize.incrementAndGet() > settings.getMaxWaitQueueSize()) {
throw createWaitQueueFullException();
}
try {
connectionPoolListener.waitQueueEntered(new ConnectionPoolWaitQueueEnteredEvent(serverId, currentThread().getId()));
PooledConnection pooledConnection = getPooledConnection(timeout, timeUnit);
if (!pooledConnection.opened()) {
try {
pooledConnection.open();
} catch (Throwable t) {
pool.release(pooledConnection.wrapped, true);
if (t instanceof MongoException) {
throw (MongoException) t;
} else {
throw new MongoInternalException(t.toString(), t);
}
}
}
return pooledConnection;
} finally {
connectionPoolListener.waitQueueExited(new ConnectionPoolWaitQueueExitedEvent(serverId, currentThread().getId()));
}
} finally {
waitQueueSize.decrementAndGet();
}
}
use of com.mongodb.event.ConnectionPoolWaitQueueEnteredEvent in project mongo-java-driver by mongodb.
the class DefaultConnectionPool method getAsync.
@Override
public void getAsync(final SingleResultCallback<InternalConnection> callback) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(String.format("Asynchronously getting a connection from the pool for server %s", serverId));
}
final SingleResultCallback<InternalConnection> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
PooledConnection connection = null;
try {
connection = getPooledConnection(0, MILLISECONDS);
} catch (MongoTimeoutException e) {
// fall through
} catch (Throwable t) {
callback.onResult(null, t);
return;
}
if (connection != null) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(String.format("Asynchronously opening pooled connection %s to server %s", connection.getDescription().getConnectionId(), serverId));
}
openAsync(connection, errHandlingCallback);
} else if (waitQueueSize.incrementAndGet() > settings.getMaxWaitQueueSize()) {
waitQueueSize.decrementAndGet();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(String.format("Asynchronously failing to get a pooled connection to %s because the wait queue is full", serverId));
}
callback.onResult(null, createWaitQueueFullException());
} else {
final long startTimeMillis = System.currentTimeMillis();
connectionPoolListener.waitQueueEntered(new ConnectionPoolWaitQueueEnteredEvent(serverId, currentThread().getId()));
getAsyncGetter().submit(new Runnable() {
@Override
public void run() {
try {
if (getRemainingWaitTime() <= 0) {
errHandlingCallback.onResult(null, createTimeoutException());
} else {
PooledConnection connection = getPooledConnection(getRemainingWaitTime(), MILLISECONDS);
openAsync(connection, errHandlingCallback);
}
} catch (Throwable t) {
errHandlingCallback.onResult(null, t);
} finally {
waitQueueSize.decrementAndGet();
connectionPoolListener.waitQueueExited(new ConnectionPoolWaitQueueExitedEvent(serverId, currentThread().getId()));
}
}
private long getRemainingWaitTime() {
return startTimeMillis + settings.getMaxWaitTime(MILLISECONDS) - System.currentTimeMillis();
}
});
}
}
Aggregations