Example usage for org.apache.commons.pool2 SwallowedExceptionListener SwallowedExceptionListener

List of usage examples for org.apache.commons.pool2 SwallowedExceptionListener SwallowedExceptionListener

Introduction

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

Prototype

SwallowedExceptionListener

Source Link

Usage

From source file:net.sf.jasperreports.phantomjs.ProcessDirector.java

private GenericObjectPool<PhantomJSProcess> createProcessPool(JRPropertiesUtil properties) {
    ProcessFactory processFactory = new ProcessFactory(this, properties);
    GenericObjectPool<PhantomJSProcess> pool = new GenericObjectPool<>(processFactory);
    pool.setLifo(true);//from w  ww .j a v  a2 s .co  m

    int maxProcessCount = properties.getIntegerProperty(PhantomJS.PROPERTY_PHANTOMJS_MAX_PROCESS_COUNT,
            PhantomJS.DEFAULT_PHANTOMJS_MAX_PROCESS_COUNT);
    pool.setMaxTotal(maxProcessCount);
    pool.setMaxIdle(maxProcessCount);

    int borrowTimeout = properties.getIntegerProperty(PhantomJS.PROPERTY_PHANTOMJS_POOL_BORROW_TIMEOUT,
            PhantomJS.DEFAULT_PHANTOMJS_POOL_BORROW_TIMEOUT);
    pool.setMaxWaitMillis(borrowTimeout);

    int idleTimeout = properties.getIntegerProperty(PhantomJS.PROPERTY_PHANTOMJS_IDLE_TIMEOUT,
            PhantomJS.DEFAULT_PHANTOMJS_IDLE_TIMEOUT);
    pool.setMinEvictableIdleTimeMillis(idleTimeout);

    pool.setTimeBetweenEvictionRunsMillis(idlePingInterval);

    pool.setTestWhileIdle(true);
    pool.setNumTestsPerEvictionRun(Integer.MAX_VALUE);

    pool.setSwallowedExceptionListener(new SwallowedExceptionListener() {
        @Override
        public void onSwallowException(Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("Pool exception", e);
            }
        }
    });

    return pool;
}

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

private void constructConnectionPool() {
    ModbusSlaveConnectionFactoryImpl connectionFactory = new ModbusSlaveConnectionFactoryImpl();
    connectionFactory.setDefaultPoolConfigurationFactory(endpoint -> {
        return endpoint.accept(new ModbusSlaveEndpointVisitor<EndpointPoolConfiguration>() {

            @Override/*from   w ww .ja v  a2  s.c o m*/
            public @NonNull EndpointPoolConfiguration visit(ModbusTCPSlaveEndpoint modbusIPSlavePoolingKey) {
                EndpointPoolConfiguration endpointPoolConfig = new EndpointPoolConfiguration();
                endpointPoolConfig.setInterTransactionDelayMillis(DEFAULT_TCP_INTER_TRANSACTION_DELAY_MILLIS);
                endpointPoolConfig.setConnectMaxTries(Modbus.DEFAULT_RETRIES);
                return endpointPoolConfig;
            }

            @Override
            public @NonNull EndpointPoolConfiguration visit(
                    ModbusSerialSlaveEndpoint modbusSerialSlavePoolingKey) {
                EndpointPoolConfiguration endpointPoolConfig = new EndpointPoolConfiguration();
                // never "disconnect" (close/open serial port) serial connection between borrows
                endpointPoolConfig.setReconnectAfterMillis(-1);
                endpointPoolConfig
                        .setInterTransactionDelayMillis(DEFAULT_SERIAL_INTER_TRANSACTION_DELAY_MILLIS);
                endpointPoolConfig.setConnectMaxTries(Modbus.DEFAULT_RETRIES);
                return endpointPoolConfig;
            }

            @Override
            public @NonNull EndpointPoolConfiguration visit(ModbusUDPSlaveEndpoint modbusUDPSlavePoolingKey) {
                EndpointPoolConfiguration endpointPoolConfig = new EndpointPoolConfiguration();
                endpointPoolConfig.setInterTransactionDelayMillis(DEFAULT_TCP_INTER_TRANSACTION_DELAY_MILLIS);
                endpointPoolConfig.setConnectMaxTries(Modbus.DEFAULT_RETRIES);
                return endpointPoolConfig;
            }
        });
    });

    GenericKeyedObjectPool<ModbusSlaveEndpoint, ModbusSlaveConnection> genericKeyedObjectPool = new ModbusConnectionPool(
            connectionFactory);
    genericKeyedObjectPool.setSwallowedExceptionListener(new SwallowedExceptionListener() {

        @SuppressWarnings("null")
        @Override
        public void onSwallowException(@Nullable Exception e) {
            LoggerFactory.getLogger(ModbusManagerImpl.class).error(
                    "Connection pool swallowed unexpected exception: {}",
                    Optional.ofNullable(e).map(ex -> ex.getMessage()).orElse("<null>"));
        }

    });
    connectionPool = genericKeyedObjectPool;
    this.connectionFactory = connectionFactory;
}