Example usage for org.apache.commons.pool2 KeyedObjectPool getNumActive

List of usage examples for org.apache.commons.pool2 KeyedObjectPool getNumActive

Introduction

In this page you can find the example usage for org.apache.commons.pool2 KeyedObjectPool getNumActive.

Prototype

int getNumActive(K key);

Source Link

Document

Returns the number of instances currently borrowed from but not yet returned to the pool corresponding to the given key.

Usage

From source file:org.openhab.io.transport.modbus.internal.ModbusManagerImpl.java

/**
 * Establishes connection to the endpoint specified by the task
 *
 * In case connection cannot be established, callback is called with {@link ModbusConnectionException}
 *
 * @param operationId id appened to log messages for identifying the operation
 * @param oneOffTask whether this is one-off, or execution of previously scheduled poll
 * @param task task representing the read or write operation
 * @return {@link ModbusSlaveConnection} to the endpoint as specified by the task, or empty {@link Optional} when
 *         connection cannot be established
 * @throws PollTaskUnregistered/*from  ww  w . j av  a2s .c  o m*/
 */
private <R extends ModbusRequestBlueprint, C extends ModbusCallback, T extends TaskWithEndpoint<R, C>> Optional<ModbusSlaveConnection> getConnection(
        AggregateStopWatch timer, boolean oneOffTask, @NonNull T task) throws PollTaskUnregistered {
    KeyedObjectPool<ModbusSlaveEndpoint, ModbusSlaveConnection> connectionPool = this.connectionPool;
    if (connectionPool == null) {
        return Optional.empty();
    }
    String operationId = timer.operationId;
    logger.trace(
            "Executing task {} (oneOff={})! Waiting for connection. Idle connections for this endpoint: {}, and active {} [operation ID {}]",
            task, oneOffTask, connectionPool.getNumIdle(task.getEndpoint()),
            connectionPool.getNumActive(task.getEndpoint()), operationId);
    long connectionBorrowStart = System.currentTimeMillis();
    ModbusCallback callback = task.getCallback();
    ModbusSlaveEndpoint endpoint = task.getEndpoint();

    ModbusRequestBlueprint request = task.getRequest();
    Optional<ModbusSlaveConnection> connection = timer.connection
            .timeSupplier(() -> borrowConnection(endpoint));
    logger.trace("Executing task {} (oneOff={})! Connection received in {} ms [operation ID {}]", task,
            oneOffTask, System.currentTimeMillis() - connectionBorrowStart, operationId);
    if (scheduledThreadPoolExecutor == null) {
        // manager deactivated
        timer.connection.timeRunnable(() -> invalidate(endpoint, connection));
        return Optional.empty();
    }
    if (!connection.isPresent()) {
        logger.warn("Could not connect to endpoint {} -- aborting request {} [operation ID {}]", endpoint,
                request, operationId);
        if (callback != null) {
            timer.callback.timeRunnable(
                    () -> invokeCallbackWithError(request, callback, new ModbusConnectionException(endpoint)));
        }
    }
    return connection;
}