Search in sources :

Example 6 with ConnectionFactory

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

the class RabbitMQEndpointTest method testCreateConnectionFactoryCustom.

@Test
public void testCreateConnectionFactoryCustom() throws Exception {
    ConnectionFactory connectionFactory = createConnectionFactory("rabbitmq:localhost:1234/exchange" + "?username=userxxx" + "&password=passxxx" + "&connectionTimeout=123" + "&requestedChannelMax=456" + "&requestedFrameMax=789" + "&requestedHeartbeat=987" + "&sslProtocol=true" + "&automaticRecoveryEnabled=true" + "&networkRecoveryInterval=654" + "&topologyRecoveryEnabled=false");
    assertEquals("localhost", connectionFactory.getHost());
    assertEquals(1234, connectionFactory.getPort());
    assertEquals("userxxx", connectionFactory.getUsername());
    assertEquals("passxxx", connectionFactory.getPassword());
    assertEquals(123, connectionFactory.getConnectionTimeout());
    assertEquals(456, connectionFactory.getRequestedChannelMax());
    assertEquals(789, connectionFactory.getRequestedFrameMax());
    assertEquals(987, connectionFactory.getRequestedHeartbeat());
    assertTrue(connectionFactory.isSSL());
    assertTrue(connectionFactory.isAutomaticRecoveryEnabled());
    assertEquals(654, connectionFactory.getNetworkRecoveryInterval());
    assertFalse(connectionFactory.isTopologyRecoveryEnabled());
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Test(org.junit.Test)

Example 7 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project flink by apache.

the class RMQSource method open.

@Override
public void open(Configuration config) throws Exception {
    super.open(config);
    ConnectionFactory factory = setupConnectionFactory();
    try {
        connection = factory.newConnection();
        channel = connection.createChannel();
        if (channel == null) {
            throw new RuntimeException("None of RabbitMQ channels are available");
        }
        setupQueue();
        consumer = new QueueingConsumer(channel);
        RuntimeContext runtimeContext = getRuntimeContext();
        if (runtimeContext instanceof StreamingRuntimeContext && ((StreamingRuntimeContext) runtimeContext).isCheckpointingEnabled()) {
            autoAck = false;
            // enables transaction mode
            channel.txSelect();
        } else {
            autoAck = true;
        }
        LOG.debug("Starting RabbitMQ source with autoAck status: " + autoAck);
        channel.basicConsume(queueName, autoAck, consumer);
    } catch (IOException e) {
        throw new RuntimeException("Cannot create RMQ connection with " + queueName + " at " + rmqConnectionConfig.getHost(), e);
    }
    running = true;
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext) QueueingConsumer(com.rabbitmq.client.QueueingConsumer) IOException(java.io.IOException) RuntimeContext(org.apache.flink.api.common.functions.RuntimeContext) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext)

Example 8 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project flink by apache.

the class RMQConnectionConfigTest method shouldSetProvidedValueIfConnectionTimeoutNotGiven.

@Test
public void shouldSetProvidedValueIfConnectionTimeoutNotGiven() throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException {
    RMQConnectionConfig connectionConfig = new RMQConnectionConfig.Builder().setHost("localhost").setPort(5000).setUserName("guest").setPassword("guest").setVirtualHost("/").setConnectionTimeout(5000).build();
    ConnectionFactory factory = connectionConfig.getConnectionFactory();
    assertEquals(5000, factory.getConnectionTimeout());
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Test(org.junit.Test)

Example 9 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project graylog2-server by Graylog2.

the class AmqpConsumer method connect.

public void connect() throws IOException {
    final ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(hostname);
    factory.setPort(port);
    factory.setVirtualHost(virtualHost);
    factory.setRequestedHeartbeat(heartbeatTimeout);
    if (tls) {
        try {
            LOG.info("Enabling TLS for AMQP input [{}/{}].", sourceInput.getName(), sourceInput.getId());
            factory.useSslProtocol();
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            throw new IOException("Couldn't enable TLS for AMQP input.", e);
        }
    }
    // Authenticate?
    if (!isNullOrEmpty(username) && !isNullOrEmpty(password)) {
        factory.setUsername(username);
        factory.setPassword(password);
    }
    try {
        connection = factory.newConnection();
    } catch (TimeoutException e) {
        throw new IOException("Timeout while opening new AMQP connection", e);
    }
    channel = connection.createChannel();
    if (null == channel) {
        LOG.error("No channel descriptor available!");
    }
    if (null != channel && prefetchCount > 0) {
        channel.basicQos(prefetchCount);
        LOG.info("AMQP prefetch count overriden to <{}>.", prefetchCount);
    }
    connection.addShutdownListener(new ShutdownListener() {

        @Override
        public void shutdownCompleted(ShutdownSignalException cause) {
            if (cause.isInitiatedByApplication()) {
                LOG.info("Not reconnecting connection, we disconnected explicitly.");
                return;
            }
            while (true) {
                try {
                    LOG.error("AMQP connection lost! Trying reconnect in 1 second.");
                    Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
                    connect();
                    LOG.info("Connected! Re-starting consumer.");
                    run();
                    LOG.info("Consumer running.");
                    break;
                } catch (IOException e) {
                    LOG.error("Could not re-connect to AMQP broker.", e);
                }
            }
        }
    });
}
Also used : ShutdownListener(com.rabbitmq.client.ShutdownListener) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) TimeoutException(java.util.concurrent.TimeoutException)

Example 10 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project flink by apache.

the class RMQSink method open.

@Override
public void open(Configuration config) throws Exception {
    ConnectionFactory factory = rmqConnectionConfig.getConnectionFactory();
    try {
        connection = factory.newConnection();
        channel = connection.createChannel();
        if (channel == null) {
            throw new RuntimeException("None of RabbitMQ channels are available");
        }
        setupQueue();
    } catch (IOException e) {
        throw new RuntimeException("Error while creating the channel", e);
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) IOException(java.io.IOException)

Aggregations

ConnectionFactory (com.rabbitmq.client.ConnectionFactory)19 Test (org.junit.Test)7 IOException (java.io.IOException)5 Connection (com.rabbitmq.client.Connection)4 KeyManagementException (java.security.KeyManagementException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 Channel (com.rabbitmq.client.Channel)2 QueueingConsumer (com.rabbitmq.client.QueueingConsumer)2 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)2 ConnectException (java.net.ConnectException)2 HashMap (java.util.HashMap)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 Queue (com.rabbitmq.client.AMQP.Queue)1 AlreadyClosedException (com.rabbitmq.client.AlreadyClosedException)1 ShutdownListener (com.rabbitmq.client.ShutdownListener)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 UnknownHostException (java.net.UnknownHostException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1