Example usage for com.rabbitmq.client ConnectionFactory newConnection

List of usage examples for com.rabbitmq.client ConnectionFactory newConnection

Introduction

In this page you can find the example usage for com.rabbitmq.client ConnectionFactory newConnection.

Prototype

public Connection newConnection(ExecutorService executor) throws IOException, TimeoutException 

Source Link

Document

Create a new broker connection.

Usage

From source file:org.graylog2.inputs.amqp.AMQPConsumer.java

License:Open Source License

private Channel connect() throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername(server.getConfiguration().getAmqpUsername());
    factory.setPassword(server.getConfiguration().getAmqpPassword());
    factory.setVirtualHost(server.getConfiguration().getAmqpVirtualhost());
    factory.setHost(server.getConfiguration().getAmqpHost());
    factory.setPort(server.getConfiguration().getAmqpPort());

    connection = factory.newConnection(Executors.newCachedThreadPool(
            new ThreadFactoryBuilder().setNameFormat("amqp-consumer-" + queueConfig.getId() + "-%d").build()));

    return connection.createChannel();
}

From source file:org.onosproject.rabbitmq.impl.MQSender.java

License:Apache License

@Override
public void start() {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setAutomaticRecoveryEnabled(true);
    factory.setNetworkRecoveryInterval(RECOVERY_INTERVAL);
    try {// w  w  w.  j  a v a2 s .c o m
        factory.setUri(url);
        if (executorService != null) {
            conn = factory.newConnection(executorService);
        } else {
            conn = factory.newConnection();
        }
        channel = conn.createChannel();
        channel.exchangeDeclare(exchangeName, TOPIC, true);
        /*
         * Setting the following parameters to queue
         * durable    - true
         * exclusive  - false
         * autoDelete - false
         * arguments  - null
         */
        channel.queueDeclare(this.queueName, true, false, false, null);
        channel.queueBind(queueName, exchangeName, routingKey);
    } catch (Exception e) {
        log.error(E_CREATE_CHAN, e);
    }
}

From source file:org.ow2.chameleon.fuchsia.importer.mqtt.MQTTOutputRouter.java

License:Apache License

@Validate
public void start() throws IOException {

    ConnectionFactory factory = new ConnectionFactory();
    connection = factory.newConnection(new Address[] { new Address(serverHost, serverPort) });
    channel = connection.createChannel();
    channel.queueDeclare(queue, false, false, false, null);

    eventArrivalMonitor = new Thread(this);
    eventArrivalMonitor.start();//from w w  w  .j  a  va 2 s  .  c om

    isMonitoringArrivals = true;

}

From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java

License:Apache License

@Test
public void testWithConnectionFactoryDefaults() throws IOException {
    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);
    com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class);
    Channel mockChannel = mock(Channel.class);

    when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection);
    when(mockConnection.createChannel()).thenReturn(mockChannel);
    when(mockChannel.isOpen()).thenReturn(true);
    when(mockConnection.isOpen()).thenReturn(true);

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    Connection con = ccf.createConnection();

    Channel channel = con.createChannel(false);
    channel.close(); // should be ignored, and placed into channel cache.
    con.close(); // should be ignored

    Connection con2 = ccf.createConnection();
    /*/*w  ww .j a  v  a  2  s .c om*/
     * will retrieve same channel object that was just put into channel cache
     */
    Channel channel2 = con2.createChannel(false);
    channel2.close(); // should be ignored
    con2.close(); // should be ignored

    assertSame(con, con2);
    assertSame(channel, channel2);
    verify(mockConnection, never()).close();
    verify(mockChannel, never()).close();

}

From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java

License:Apache License

@Test
public void testWithConnectionFactoryCacheSize() throws IOException {
    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);
    com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class);
    Channel mockChannel1 = mock(Channel.class);
    Channel mockChannel2 = mock(Channel.class);

    when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection);
    when(mockConnection.isOpen()).thenReturn(true);
    when(mockConnection.createChannel()).thenReturn(mockChannel1).thenReturn(mockChannel2);

    when(mockChannel1.basicGet("foo", false)).thenReturn(new GetResponse(null, null, null, 1));
    when(mockChannel2.basicGet("bar", false)).thenReturn(new GetResponse(null, null, null, 1));
    when(mockChannel1.isOpen()).thenReturn(true);
    when(mockChannel2.isOpen()).thenReturn(true);

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setChannelCacheSize(2);/*w w w. jav  a  2 s. c  om*/

    Connection con = ccf.createConnection();

    Channel channel1 = con.createChannel(false);
    Channel channel2 = con.createChannel(false);

    channel1.basicGet("foo", true);
    channel2.basicGet("bar", true);

    channel1.close(); // should be ignored, and add last into channel cache.
    channel2.close(); // should be ignored, and add last into channel cache.

    Channel ch1 = con.createChannel(false); // remove first entry in cache
    // (channel1)
    Channel ch2 = con.createChannel(false); // remove first entry in cache
    // (channel2)

    assertNotSame(ch1, ch2);
    assertSame(ch1, channel1);
    assertSame(ch2, channel2);

    ch1.close();
    ch2.close();

    verify(mockConnection, times(2)).createChannel();

    con.close(); // should be ignored

    verify(mockConnection, never()).close();
    verify(mockChannel1, never()).close();
    verify(mockChannel2, never()).close();

}

From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java

License:Apache License

@Test
public void testCacheSizeExceeded() throws IOException {
    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);
    com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class);
    Channel mockChannel1 = mock(Channel.class);
    Channel mockChannel2 = mock(Channel.class);
    Channel mockChannel3 = mock(Channel.class);

    when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection);
    when(mockConnection.createChannel()).thenReturn(mockChannel1).thenReturn(mockChannel2)
            .thenReturn(mockChannel3);/*from w ww  .  j  a v a  2  s .co  m*/
    when(mockConnection.isOpen()).thenReturn(true);

    // Called during physical close
    when(mockChannel1.isOpen()).thenReturn(true);
    when(mockChannel2.isOpen()).thenReturn(true);
    when(mockChannel3.isOpen()).thenReturn(true);

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setChannelCacheSize(1);

    Connection con = ccf.createConnection();

    Channel channel1 = con.createChannel(false);
    // cache size is 1, but the other connection is not released yet so this
    // creates a new one
    Channel channel2 = con.createChannel(false);
    assertNotSame(channel1, channel2);

    // should be ignored, and added last into channel cache.
    channel1.close();
    // should be physically closed
    channel2.close();

    // remove first entry in cache (channel1)
    Channel ch1 = con.createChannel(false);
    // create a new channel
    Channel ch2 = con.createChannel(false);

    assertNotSame(ch1, ch2);
    assertSame(ch1, channel1);
    assertNotSame(ch2, channel2);

    ch1.close();
    ch2.close();

    verify(mockConnection, times(3)).createChannel();

    con.close(); // should be ignored

    verify(mockConnection, never()).close();
    verify(mockChannel1, never()).close();
    verify(mockChannel2, atLeastOnce()).close();
    verify(mockChannel3, atLeastOnce()).close();

}

From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java

License:Apache License

@Test
public void testCacheSizeExceededAfterClose() throws IOException {
    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);
    com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class);
    Channel mockChannel1 = mock(Channel.class);
    Channel mockChannel2 = mock(Channel.class);

    when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection);
    when(mockConnection.createChannel()).thenReturn(mockChannel1).thenReturn(mockChannel2);
    when(mockConnection.isOpen()).thenReturn(true);

    // Called during physical close
    when(mockChannel1.isOpen()).thenReturn(true);
    when(mockChannel2.isOpen()).thenReturn(true);

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setChannelCacheSize(1);//  w w  w .j  av  a2  s.  c o  m

    Connection con = ccf.createConnection();

    Channel channel1 = con.createChannel(false);
    channel1.close(); // should be ignored, and add last into channel cache.
    Channel channel2 = con.createChannel(false);
    channel2.close(); // should be ignored, and add last into channel cache.
    assertSame(channel1, channel2);

    Channel ch1 = con.createChannel(false); // remove first entry in cache
    // (channel1)
    Channel ch2 = con.createChannel(false); // create new channel

    assertNotSame(ch1, ch2);
    assertSame(ch1, channel1);
    assertNotSame(ch2, channel2);

    ch1.close();
    ch2.close();

    verify(mockConnection, times(2)).createChannel();

    con.close(); // should be ignored

    verify(mockConnection, never()).close();
    verify(mockChannel1, never()).close();
    verify(mockChannel2, atLeastOnce()).close();

}

From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java

License:Apache License

@Test
public void testTransactionalAndNonTransactionalChannelsSegregated() throws IOException {
    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);
    com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class);
    Channel mockChannel1 = mock(Channel.class);
    Channel mockChannel2 = mock(Channel.class);

    when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection);
    when(mockConnection.createChannel()).thenReturn(mockChannel1).thenReturn(mockChannel2);
    when(mockConnection.isOpen()).thenReturn(true);

    // Called during physical close
    when(mockChannel1.isOpen()).thenReturn(true);
    when(mockChannel2.isOpen()).thenReturn(true);

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setChannelCacheSize(1);//from   w w w  .ja  va2 s  .co m

    Connection con = ccf.createConnection();

    Channel channel1 = con.createChannel(true);
    channel1.txSelect();
    channel1.close(); // should be ignored, and add last into channel cache.
    /*
     * When a channel is created as non-transactional we should create a new one.
     */
    Channel channel2 = con.createChannel(false);
    channel2.close(); // should be ignored, and add last into channel cache.
    assertNotSame(channel1, channel2);

    Channel ch1 = con.createChannel(true); // remove first entry in cache (channel1)
    Channel ch2 = con.createChannel(false); // create new channel

    assertNotSame(ch1, ch2);
    assertSame(ch1, channel1); // The non-transactional one
    assertSame(ch2, channel2);

    ch1.close();
    ch2.close();

    verify(mockConnection, times(2)).createChannel();

    con.close(); // should be ignored

    verify(mockConnection, never()).close();
    verify(mockChannel1, never()).close();
    verify(mockChannel2, never()).close();

    @SuppressWarnings("unchecked")
    List<Channel> notxlist = (List<Channel>) ReflectionTestUtils.getField(ccf,
            "cachedChannelsNonTransactional");
    assertEquals(1, notxlist.size());
    @SuppressWarnings("unchecked")
    List<Channel> txlist = (List<Channel>) ReflectionTestUtils.getField(ccf, "cachedChannelsTransactional");
    assertEquals(1, txlist.size());

}

From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java

License:Apache License

@Test
public void testWithConnectionFactoryDestroy() throws IOException {
    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);
    com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class);

    Channel mockChannel1 = mock(Channel.class);
    Channel mockChannel2 = mock(Channel.class);

    assertNotSame(mockChannel1, mockChannel2);

    when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection);
    // You can't repeat 'when' statements for stubbing consecutive calls to
    // the same method to returning different
    // values.//from w  ww .  j  a  va2s. com
    when(mockConnection.createChannel()).thenReturn(mockChannel1).thenReturn(mockChannel2);
    when(mockConnection.isOpen()).thenReturn(true);
    // Called during physical close
    when(mockChannel1.isOpen()).thenReturn(true);
    when(mockChannel2.isOpen()).thenReturn(true);

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setChannelCacheSize(2);

    Connection con = ccf.createConnection();

    // This will return a proxy that surpresses calls to close
    Channel channel1 = con.createChannel(false);
    Channel channel2 = con.createChannel(false);

    // Should be ignored, and add last into channel cache.
    channel1.close();
    channel2.close();

    // remove first entry in cache (channel1)
    Channel ch1 = con.createChannel(false);
    // remove first entry in cache (channel2)
    Channel ch2 = con.createChannel(false);

    assertSame(ch1, channel1);
    assertSame(ch2, channel2);

    Channel target1 = ((ChannelProxy) ch1).getTargetChannel();
    Channel target2 = ((ChannelProxy) ch2).getTargetChannel();

    // make sure mokito returned different mocks for the channel
    assertNotSame(target1, target2);

    ch1.close();
    ch2.close();
    con.close(); // should be ignored

    ccf.destroy(); // should call close on connection and channels in cache

    verify(mockConnection, times(2)).createChannel();

    verify(mockConnection).close(anyInt());

    // verify(mockChannel1).close();
    verify(mockChannel2).close();

    // After destroy we can get a new connection
    Connection con1 = ccf.createConnection();
    assertNotSame(con, con1);

    // This will return a proxy that surpresses calls to close
    Channel channel3 = con.createChannel(false);
    assertNotSame(channel3, channel1);
    assertNotSame(channel3, channel2);
}

From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java

License:Apache License

@Test
public void testWithChannelListener() throws IOException {

    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);
    com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class);
    Channel mockChannel = mock(Channel.class);

    when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection);
    when(mockConnection.isOpen()).thenReturn(true);
    when(mockChannel.isOpen()).thenReturn(true);
    when(mockConnection.createChannel()).thenReturn(mockChannel);

    final AtomicInteger called = new AtomicInteger(0);
    AbstractConnectionFactory connectionFactory = createConnectionFactory(mockConnectionFactory);
    connectionFactory.setChannelListeners(Arrays.asList(new ChannelListener() {
        @Override// w ww . j a va  2  s  .c o  m
        public void onCreate(Channel channel, boolean transactional) {
            called.incrementAndGet();
        }
    }));
    ((CachingConnectionFactory) connectionFactory).setChannelCacheSize(1);

    Connection con = connectionFactory.createConnection();
    Channel channel = con.createChannel(false);
    assertEquals(1, called.get());
    channel.close();

    con.close();
    verify(mockConnection, never()).close();

    connectionFactory.createConnection();
    con.createChannel(false);
    assertEquals(1, called.get());

    connectionFactory.destroy();
    verify(mockConnection, atLeastOnce()).close(anyInt());

    verify(mockConnectionFactory).newConnection((ExecutorService) null);

}