Search in sources :

Example 6 with ConnectionParams

use of com.rabbitmq.client.impl.ConnectionParams in project rabbitmq-java-client by rabbitmq.

the class ConnectionFactory method params.

public ConnectionParams params(ExecutorService consumerWorkServiceExecutor) {
    ConnectionParams result = new ConnectionParams();
    result.setCredentialsProvider(credentialsProvider);
    result.setConsumerWorkServiceExecutor(consumerWorkServiceExecutor);
    result.setVirtualHost(virtualHost);
    result.setClientProperties(getClientProperties());
    result.setRequestedFrameMax(requestedFrameMax);
    result.setRequestedChannelMax(requestedChannelMax);
    result.setShutdownTimeout(shutdownTimeout);
    result.setSaslConfig(saslConfig);
    result.setNetworkRecoveryInterval(networkRecoveryInterval);
    result.setRecoveryDelayHandler(recoveryDelayHandler);
    result.setTopologyRecovery(topologyRecovery);
    result.setExceptionHandler(exceptionHandler);
    result.setThreadFactory(threadFactory);
    result.setHandshakeTimeout(handshakeTimeout);
    result.setRequestedHeartbeat(requestedHeartbeat);
    result.setShutdownExecutor(shutdownExecutor);
    result.setHeartbeatExecutor(heartbeatExecutor);
    result.setChannelRpcTimeout(channelRpcTimeout);
    result.setChannelShouldCheckRpcResponseType(channelShouldCheckRpcResponseType);
    result.setWorkPoolTimeout(workPoolTimeout);
    result.setErrorOnWriteListener(errorOnWriteListener);
    return result;
}
Also used : ConnectionParams(com.rabbitmq.client.impl.ConnectionParams)

Example 7 with ConnectionParams

use of com.rabbitmq.client.impl.ConnectionParams in project rabbitmq-java-client by rabbitmq.

the class ConnectionFactory method newConnection.

/**
 * Create a new broker connection with a client-provided name, picking the first available address from
 * the list provided by the {@link AddressResolver}.
 *
 * If <a href="http://www.rabbitmq.com/api-guide.html#recovery">automatic connection recovery</a>
 * is enabled, the connection returned by this method will be {@link Recoverable}. Future
 * reconnection attempts will pick a random accessible address provided by the {@link AddressResolver}.
 *
 * @param executor thread execution service for consumers on the connection
 * @param addressResolver discovery service to list potential addresses (hostname/port pairs) to connect to
 * @param clientProvidedName application-specific connection name, will be displayed
 *                           in the management UI if RabbitMQ server supports it.
 *                           This value doesn't have to be unique and cannot be used
 *                           as a connection identifier e.g. in HTTP API requests.
 *                           This value is supposed to be human-readable.
 * @return an interface to the connection
 * @throws java.io.IOException if it encounters a problem
 * @see <a href="http://www.rabbitmq.com/api-guide.html#recovery">Automatic Recovery</a>
 */
public Connection newConnection(ExecutorService executor, AddressResolver addressResolver, String clientProvidedName) throws IOException, TimeoutException {
    if (this.metricsCollector == null) {
        this.metricsCollector = new NoOpMetricsCollector();
    }
    // make sure we respect the provided thread factory
    FrameHandlerFactory fhFactory = createFrameHandlerFactory();
    ConnectionParams params = params(executor);
    // set client-provided via a client property
    if (clientProvidedName != null) {
        Map<String, Object> properties = new HashMap<String, Object>(params.getClientProperties());
        properties.put("connection_name", clientProvidedName);
        params.setClientProperties(properties);
    }
    if (isAutomaticRecoveryEnabled()) {
        // see com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory#newConnection
        AutorecoveringConnection conn = new AutorecoveringConnection(params, fhFactory, addressResolver, metricsCollector);
        conn.init();
        return conn;
    } else {
        List<Address> addrs = addressResolver.getAddresses();
        Exception lastException = null;
        for (Address addr : addrs) {
            try {
                FrameHandler handler = fhFactory.create(addr, clientProvidedName);
                AMQConnection conn = createConnection(params, handler, metricsCollector);
                conn.start();
                this.metricsCollector.newConnection(conn);
                return conn;
            } catch (IOException e) {
                lastException = e;
            } catch (TimeoutException te) {
                lastException = te;
            }
        }
        if (lastException != null) {
            if (lastException instanceof IOException) {
                throw (IOException) lastException;
            } else if (lastException instanceof TimeoutException) {
                throw (TimeoutException) lastException;
            }
        }
        throw new IOException("failed to connect");
    }
}
Also used : SocketChannelFrameHandlerFactory(com.rabbitmq.client.impl.nio.SocketChannelFrameHandlerFactory) FrameHandlerFactory(com.rabbitmq.client.impl.FrameHandlerFactory) SocketFrameHandlerFactory(com.rabbitmq.client.impl.SocketFrameHandlerFactory) FrameHandler(com.rabbitmq.client.impl.FrameHandler) HashMap(java.util.HashMap) AMQConnection(com.rabbitmq.client.impl.AMQConnection) IOException(java.io.IOException) ConnectionParams(com.rabbitmq.client.impl.ConnectionParams) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AutorecoveringConnection(com.rabbitmq.client.impl.recovery.AutorecoveringConnection) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

ConnectionParams (com.rabbitmq.client.impl.ConnectionParams)7 AMQConnection (com.rabbitmq.client.impl.AMQConnection)5 Test (org.junit.Test)5 FrameHandler (com.rabbitmq.client.impl.FrameHandler)4 Connection (com.rabbitmq.client.Connection)3 MetricsCollector (com.rabbitmq.client.MetricsCollector)3 IOException (java.io.IOException)3 Address (com.rabbitmq.client.Address)2 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)2 FrameHandlerFactory (com.rabbitmq.client.impl.FrameHandlerFactory)2 SocketTimeoutException (java.net.SocketTimeoutException)2 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)2 TimeoutException (java.util.concurrent.TimeoutException)2 AddressResolver (com.rabbitmq.client.AddressResolver)1 CredentialsProvider (com.rabbitmq.client.impl.CredentialsProvider)1 SocketFrameHandlerFactory (com.rabbitmq.client.impl.SocketFrameHandlerFactory)1 SocketChannelFrameHandlerFactory (com.rabbitmq.client.impl.nio.SocketChannelFrameHandlerFactory)1 AutorecoveringConnection (com.rabbitmq.client.impl.recovery.AutorecoveringConnection)1 RecoveryAwareAMQConnection (com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnection)1 RecoveryAwareAMQConnectionFactory (com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory)1