Search in sources :

Example 1 with Address

use of com.rabbitmq.client.Address in project camel by apache.

the class RabbitMQEndpointTest method brokerEndpointAddressesSettings.

@Test
public void brokerEndpointAddressesSettings() throws Exception {
    RabbitMQEndpoint endpoint = context.getEndpoint("rabbitmq:localhost/exchange?addresses=server1:12345,server2:12345", RabbitMQEndpoint.class);
    assertEquals("Wrong size of endpoint addresses.", 2, endpoint.getAddresses().length);
    assertEquals("Get a wrong endpoint address.", new Address("server1", 12345), endpoint.getAddresses()[0]);
    assertEquals("Get a wrong endpoint address.", new Address("server2", 12345), endpoint.getAddresses()[1]);
}
Also used : Address(com.rabbitmq.client.Address) Test(org.junit.Test)

Example 2 with Address

use of com.rabbitmq.client.Address in project rabbitmq-java-client by rabbitmq.

the class ConnectionFactoryTest method tryNextAddressIfTimeoutExceptionNoAutoRecovery.

// see https://github.com/rabbitmq/rabbitmq-java-client/issues/262
@Test
public void tryNextAddressIfTimeoutExceptionNoAutoRecovery() throws IOException, TimeoutException {
    final AMQConnection connectionThatThrowsTimeout = mock(AMQConnection.class);
    final AMQConnection connectionThatSucceeds = mock(AMQConnection.class);
    final Queue<AMQConnection> connections = new ArrayBlockingQueue<AMQConnection>(10);
    connections.add(connectionThatThrowsTimeout);
    connections.add(connectionThatSucceeds);
    ConnectionFactory connectionFactory = new ConnectionFactory() {

        @Override
        protected AMQConnection createConnection(ConnectionParams params, FrameHandler frameHandler, MetricsCollector metricsCollector) {
            return connections.poll();
        }

        @Override
        protected synchronized FrameHandlerFactory createFrameHandlerFactory() throws IOException {
            return mock(FrameHandlerFactory.class);
        }
    };
    connectionFactory.setAutomaticRecoveryEnabled(false);
    doThrow(TimeoutException.class).when(connectionThatThrowsTimeout).start();
    doNothing().when(connectionThatSucceeds).start();
    Connection returnedConnection = connectionFactory.newConnection(new Address[] { new Address("host1"), new Address("host2") });
    assertSame(connectionThatSucceeds, returnedConnection);
}
Also used : MetricsCollector(com.rabbitmq.client.MetricsCollector) FrameHandler(com.rabbitmq.client.impl.FrameHandler) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Address(com.rabbitmq.client.Address) AMQConnection(com.rabbitmq.client.impl.AMQConnection) AMQConnection(com.rabbitmq.client.impl.AMQConnection) Connection(com.rabbitmq.client.Connection) ConnectionParams(com.rabbitmq.client.impl.ConnectionParams) Test(org.junit.Test)

Example 3 with Address

use of com.rabbitmq.client.Address in project wso2-axis2-transports by wso2.

the class RabbitMQConnectionFactory method initConnectionFactory.

/**
 * Initialize connection factory
 */
private void initConnectionFactory() {
    connectionFactory = new ConnectionFactory();
    String hostName = parameters.get(RabbitMQConstants.SERVER_HOST_NAME);
    String portValue = parameters.get(RabbitMQConstants.SERVER_PORT);
    String serverRetryIntervalS = parameters.get(RabbitMQConstants.SERVER_RETRY_INTERVAL);
    String retryIntervalS = parameters.get(RabbitMQConstants.RETRY_INTERVAL);
    String retryCountS = parameters.get(RabbitMQConstants.RETRY_COUNT);
    String heartbeat = parameters.get(RabbitMQConstants.HEARTBEAT);
    String connectionTimeout = parameters.get(RabbitMQConstants.CONNECTION_TIMEOUT);
    String sslEnabledS = parameters.get(RabbitMQConstants.SSL_ENABLED);
    String userName = parameters.get(RabbitMQConstants.SERVER_USER_NAME);
    String password = parameters.get(RabbitMQConstants.SERVER_PASSWORD);
    String virtualHost = parameters.get(RabbitMQConstants.SERVER_VIRTUAL_HOST);
    String connectionPoolSizeS = parameters.get(RabbitMQConstants.CONNECTION_POOL_SIZE);
    if (!StringUtils.isEmpty(heartbeat)) {
        try {
            int heartbeatValue = Integer.parseInt(heartbeat);
            connectionFactory.setRequestedHeartbeat(heartbeatValue);
        } catch (NumberFormatException e) {
            // proceeding with rabbitmq default value
            log.warn("Number format error in reading heartbeat value. Proceeding with default");
        }
    }
    if (!StringUtils.isEmpty(connectionTimeout)) {
        try {
            int connectionTimeoutValue = Integer.parseInt(connectionTimeout);
            connectionFactory.setConnectionTimeout(connectionTimeoutValue);
        } catch (NumberFormatException e) {
            // proceeding with rabbitmq default value
            log.warn("Number format error in reading connection timeout value. Proceeding with default");
        }
    }
    if (!StringUtils.isEmpty(connectionPoolSizeS)) {
        try {
            connectionPoolSize = Integer.parseInt(connectionPoolSizeS);
        } catch (NumberFormatException e) {
            // proceeding with rabbitmq default value
            log.warn("Number format error in reading connection timeout value. Proceeding with default");
        }
    }
    if (!StringUtils.isEmpty(sslEnabledS)) {
        try {
            boolean sslEnabled = Boolean.parseBoolean(sslEnabledS);
            if (sslEnabled) {
                String keyStoreLocation = parameters.get(RabbitMQConstants.SSL_KEYSTORE_LOCATION);
                String keyStoreType = parameters.get(RabbitMQConstants.SSL_KEYSTORE_TYPE);
                String keyStorePassword = parameters.get(RabbitMQConstants.SSL_KEYSTORE_PASSWORD);
                String trustStoreLocation = parameters.get(RabbitMQConstants.SSL_TRUSTSTORE_LOCATION);
                String trustStoreType = parameters.get(RabbitMQConstants.SSL_TRUSTSTORE_TYPE);
                String trustStorePassword = parameters.get(RabbitMQConstants.SSL_TRUSTSTORE_PASSWORD);
                String sslVersion = parameters.get(RabbitMQConstants.SSL_VERSION);
                if (StringUtils.isEmpty(keyStoreLocation) || StringUtils.isEmpty(keyStoreType) || StringUtils.isEmpty(keyStorePassword) || StringUtils.isEmpty(trustStoreLocation) || StringUtils.isEmpty(trustStoreType) || StringUtils.isEmpty(trustStorePassword)) {
                    log.info("Trustore and keystore information is not provided");
                    if (StringUtils.isNotEmpty(sslVersion)) {
                        connectionFactory.useSslProtocol(sslVersion);
                    } else {
                        log.info("Proceeding with default SSL configuration");
                        connectionFactory.useSslProtocol();
                    }
                } else {
                    char[] keyPassphrase = keyStorePassword.toCharArray();
                    KeyStore ks = KeyStore.getInstance(keyStoreType);
                    ks.load(new FileInputStream(keyStoreLocation), keyPassphrase);
                    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
                    kmf.init(ks, keyPassphrase);
                    char[] trustPassphrase = trustStorePassword.toCharArray();
                    KeyStore tks = KeyStore.getInstance(trustStoreType);
                    tks.load(new FileInputStream(trustStoreLocation), trustPassphrase);
                    TrustManagerFactory tmf = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
                    tmf.init(tks);
                    SSLContext c = SSLContext.getInstance(sslVersion);
                    c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
                    connectionFactory.useSslProtocol(c);
                }
            }
        } catch (Exception e) {
            log.warn("Format error in SSL enabled value. Proceeding without enabling SSL", e);
        }
    }
    if (!StringUtils.isEmpty(retryCountS)) {
        try {
            retryCount = Integer.parseInt(retryCountS);
        } catch (NumberFormatException e) {
            log.warn("Number format error in reading retry count value. Proceeding with default value (3)", e);
        }
    }
    // Resolving hostname(s) and port(s)
    if (!StringUtils.isEmpty(hostName) && !StringUtils.isEmpty(portValue)) {
        String[] hostNames = hostName.split(",");
        String[] portValues = portValue.split(",");
        if (hostNames.length == portValues.length) {
            addresses = new Address[hostNames.length];
            for (int i = 0; i < hostNames.length; i++) {
                if (!hostNames[i].isEmpty() && !portValues[i].isEmpty()) {
                    try {
                        addresses[i] = new Address(hostNames[i].trim(), Integer.parseInt(portValues[i].trim()));
                    } catch (NumberFormatException e) {
                        handleException("Number format error in port number", e);
                    }
                }
            }
        }
    } else {
        handleException("Host name(s) and port(s) are not correctly defined");
    }
    if (!StringUtils.isEmpty(userName)) {
        connectionFactory.setUsername(userName);
    }
    if (!StringUtils.isEmpty(password)) {
        connectionFactory.setPassword(password);
    }
    if (!StringUtils.isEmpty(virtualHost)) {
        connectionFactory.setVirtualHost(virtualHost);
    }
    if (!StringUtils.isEmpty(retryIntervalS)) {
        try {
            retryInterval = Integer.parseInt(retryIntervalS);
        } catch (NumberFormatException e) {
            log.warn("Number format error in reading retry interval value. Proceeding with default value (30000ms)", e);
        }
    }
    if (!StringUtils.isEmpty(serverRetryIntervalS)) {
        try {
            int serverRetryInterval = Integer.parseInt(serverRetryIntervalS);
            connectionFactory.setNetworkRecoveryInterval(serverRetryInterval);
        } catch (NumberFormatException e) {
            log.warn("Number format error in reading server retry interval value. Proceeding with default value", e);
        }
    }
    connectionFactory.setAutomaticRecoveryEnabled(true);
    connectionFactory.setTopologyRecoveryEnabled(false);
}
Also used : Address(com.rabbitmq.client.Address) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) AxisRabbitMQException(org.apache.axis2.transport.rabbitmq.utils.AxisRabbitMQException) SecureVaultException(org.wso2.securevault.SecureVaultException) IOException(java.io.IOException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) TrustManagerFactory(javax.net.ssl.TrustManagerFactory)

Example 4 with Address

use of com.rabbitmq.client.Address in project microservices by pwillhan.

the class ClusterSenderDemo method sendToDefaultExchange.

// default port 5672 which corresponds
// to the 'rabbit@Domain' instance
// is being used for the connection to the broker
public static void sendToDefaultExchange() {
    ClusterSender sender = new ClusterSender();
    Address address = new Address(NODE_HOSTNAME);
    sender.initialize(address);
    sender.send("Test message.");
    sender.destroy();
}
Also used : Address(com.rabbitmq.client.Address)

Example 5 with Address

use of com.rabbitmq.client.Address in project microservices by pwillhan.

the class ClusterReceiverDemo method main.

public static void main(String[] args) throws InterruptedException {
    final ClusterReceiver receiver = new ClusterReceiver();
    Address address = new Address(NODE_HOSTNAME, NODE_PORT);
    receiver.initialize(address);
    receiver.receive();
    receiver.destroy();
}
Also used : Address(com.rabbitmq.client.Address)

Aggregations

Address (com.rabbitmq.client.Address)13 Test (org.junit.Test)6 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)5 Connection (com.rabbitmq.client.Connection)4 MetricsCollector (com.rabbitmq.client.MetricsCollector)2 ConnectionParams (com.rabbitmq.client.impl.ConnectionParams)2 FrameHandler (com.rabbitmq.client.impl.FrameHandler)2 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)2 RabbitMqConfiguration (com.godaddy.pubsub.configurations.RabbitMqConfiguration)1 Provides (com.google.inject.Provides)1 LazySingleton (com.netflix.governator.guice.lazy.LazySingleton)1 AddressResolver (com.rabbitmq.client.AddressResolver)1 DnsSrvRecordAddressResolver (com.rabbitmq.client.DnsSrvRecordAddressResolver)1 AMQConnection (com.rabbitmq.client.impl.AMQConnection)1 FrameHandlerFactory (com.rabbitmq.client.impl.FrameHandlerFactory)1 RecoveryAwareAMQConnection (com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnection)1 RecoveryAwareAMQConnectionFactory (com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory)1 Host (io.paradoxical.rabbitmq.connectionManagment.Host)1 SimpleChannelProvider (io.paradoxical.rabbitmq.connectionManagment.SimpleChannelProvider)1 FileInputStream (java.io.FileInputStream)1