Example usage for org.springframework.amqp.rabbit.connection CachingConnectionFactory CachingConnectionFactory

List of usage examples for org.springframework.amqp.rabbit.connection CachingConnectionFactory CachingConnectionFactory

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.connection CachingConnectionFactory CachingConnectionFactory.

Prototype

public CachingConnectionFactory(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory) 

Source Link

Document

Create a new CachingConnectionFactory for the given target ConnectionFactory.

Usage

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

@Test
public void setAddressesEmpty() throws Exception {
    ConnectionFactory mock = mock(com.rabbitmq.client.ConnectionFactory.class);
    CachingConnectionFactory ccf = new CachingConnectionFactory(mock);
    ccf.setExecutor(mock(ExecutorService.class));
    ccf.setHost("abc");
    ccf.setAddresses("");
    ccf.createConnection();//from w  w w  .  j  a va2s  .  com
    verify(mock).isAutomaticRecoveryEnabled();
    verify(mock).setHost("abc");
    Log logger = TestUtils.getPropertyValue(ccf, "logger", Log.class);
    if (logger.isInfoEnabled()) {
        verify(mock).getHost();
        verify(mock).getPort();
    }
    verify(mock).newConnection(any(ExecutorService.class), anyString());
    verifyNoMoreInteractions(mock);
}

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

@Test
public void setAddressesOneHost() throws Exception {
    ConnectionFactory mock = mock(com.rabbitmq.client.ConnectionFactory.class);
    CachingConnectionFactory ccf = new CachingConnectionFactory(mock);
    ccf.setAddresses("mq1");
    ccf.createConnection();/*from ww  w.  j  a v  a2  s .com*/
    verify(mock).isAutomaticRecoveryEnabled();
    verify(mock).newConnection(isNull(), aryEq(new Address[] { new Address("mq1") }), anyString());
    verifyNoMoreInteractions(mock);
}

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

@Test
public void setAddressesTwoHosts() throws Exception {
    ConnectionFactory mock = mock(com.rabbitmq.client.ConnectionFactory.class);
    doReturn(true).when(mock).isAutomaticRecoveryEnabled();
    CachingConnectionFactory ccf = new CachingConnectionFactory(mock);
    ccf.setAddresses("mq1,mq2");
    ccf.createConnection();//w w w.  ja  va2  s .co  m
    verify(mock).isAutomaticRecoveryEnabled();
    verify(mock).newConnection(isNull(), aryEq(new Address[] { new Address("mq1"), new Address("mq2") }),
            anyString());
    verifyNoMoreInteractions(mock);
}

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

@Test
public void setUri() throws Exception {
    URI uri = new URI("amqp://localhost:1234/%2f");

    ConnectionFactory mock = mock(com.rabbitmq.client.ConnectionFactory.class);
    CachingConnectionFactory ccf = new CachingConnectionFactory(mock);
    ccf.setExecutor(mock(ExecutorService.class));

    ccf.setUri(uri);//ww  w .  jav  a 2 s  . com
    ccf.createConnection();

    InOrder order = inOrder(mock);
    order.verify(mock).isAutomaticRecoveryEnabled();
    order.verify(mock).setUri(uri);
    Log logger = TestUtils.getPropertyValue(ccf, "logger", Log.class);
    if (logger.isInfoEnabled()) {
        order.verify(mock).getHost();
        order.verify(mock).getPort();
    }
    order.verify(mock).newConnection(any(ExecutorService.class), anyString());
    verifyNoMoreInteractions(mock);
}

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

@Test
public void testChannelCloseIdempotency() throws Exception {
    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(any(ExecutorService.class), anyString()))
            .thenReturn(mockConnection);
    when(mockConnection.isOpen()).thenReturn(true);
    when(mockConnection.createChannel()).thenReturn(mockChannel);
    when(mockChannel.isOpen()).thenReturn(true).thenReturn(false);

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setExecutor(mock(ExecutorService.class));
    Connection con = ccf.createConnection();

    Channel channel = con.createChannel(false);
    //      RabbitUtils.setPhysicalCloseRequired(true);
    channel.close(); // should be ignored, and placed into channel cache.
    channel.close(); // physically closed, so remove from the cache.
    channel.close(); // physically closed and removed from the cache  before, so void "close".
    Channel channel2 = con.createChannel(false);
    assertNotSame(channel, channel2);/*w w  w .  ja v a2  s  .c  om*/
}

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

@Test
@Ignore // Test to verify log message is suppressed after patch to CCF
public void testReturnsNormalCloseDeferredClose() throws Exception {
    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(any(ExecutorService.class), anyString()))
            .thenReturn(mockConnection);
    when(mockConnection.isOpen()).thenReturn(true);
    when(mockConnection.createChannel()).thenReturn(mockChannel);
    when(mockChannel.isOpen()).thenReturn(true);
    doThrow(new ShutdownSignalException(true, false,
            new com.rabbitmq.client.AMQP.Connection.Close.Builder().replyCode(200).replyText("OK").build(),
            null)).when(mockChannel).close();

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setPublisherReturns(true);//  ww  w .j av a2s . c  o  m
    ccf.setExecutor(Executors.newSingleThreadExecutor());
    Connection conn = ccf.createConnection();
    Channel channel = conn.createChannel(false);
    RabbitUtils.setPhysicalCloseRequired(channel, true);
    channel.close();
    Thread.sleep(6000);
}

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

/**
 * Create a dedicated connection factory for the address.
 * @param address the address to which the factory should connect.
 * @param node  the node.//from   www .  ja v  a  2  s. c  om
 * @return the connection factory.
 * @throws Exception if errors occur during creation.
 */
protected ConnectionFactory createConnectionFactory(String address, String node) throws Exception {
    RabbitConnectionFactoryBean rcfb = new RabbitConnectionFactoryBean();
    rcfb.setUseSSL(this.useSSL);
    rcfb.setSslPropertiesLocation(this.sslPropertiesLocation);
    rcfb.setKeyStore(this.keyStore);
    rcfb.setTrustStore(this.trustStore);
    rcfb.setKeyStorePassphrase(this.keyStorePassPhrase);
    rcfb.setTrustStorePassphrase(this.trustStorePassPhrase);
    rcfb.afterPropertiesSet();
    CachingConnectionFactory ccf = new CachingConnectionFactory(rcfb.getObject());
    ccf.setAddresses(address);
    ccf.setUsername(this.username);
    ccf.setPassword(this.password);
    ccf.setVirtualHost(this.vhost);
    ccf.setBeanName("node:" + node);
    return ccf;
}

From source file:org.springframework.amqp.rabbit.core.RabbitAdminTests.java

@Test
public void testAvoidHangAMQP_508() {
    CachingConnectionFactory cf = new CachingConnectionFactory("localhost");
    RabbitAdmin admin = new RabbitAdmin(cf);
    String longName = new String(new byte[300]).replace('\u0000', 'x');
    try {/*from   w w w.  j  av  a  2s .com*/
        admin.declareQueue(new Queue(longName));
    } catch (Exception e) {
        e.printStackTrace();
    }
    String goodName = "foobar";
    admin.declareQueue(new Queue(goodName));
    assertNull(admin.getQueueProperties(longName));
    assertNotNull(admin.getQueueProperties(goodName));
    admin.deleteQueue(goodName);
    cf.destroy();
}

From source file:org.springframework.amqp.rabbit.core.RabbitAdminTests.java

@Test
public void testIgnoreDeclarationExeptionsTimeout() throws Exception {
    com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);
    TimeoutException toBeThrown = new TimeoutException("test");
    doThrow(toBeThrown).when(rabbitConnectionFactory).newConnection(any(ExecutorService.class), anyString());
    CachingConnectionFactory ccf = new CachingConnectionFactory(rabbitConnectionFactory);
    RabbitAdmin admin = new RabbitAdmin(ccf);
    List<DeclarationExceptionEvent> events = new ArrayList<DeclarationExceptionEvent>();
    admin.setApplicationEventPublisher(new EventPublisher(events));
    admin.setIgnoreDeclarationExceptions(true);
    admin.declareQueue(new AnonymousQueue());
    admin.declareQueue();//from  ww  w .  ja v a2s .c om
    admin.declareExchange(new DirectExchange("foo"));
    admin.declareBinding(new Binding("foo", DestinationType.QUEUE, "bar", "baz", null));
    assertThat(events.size(), equalTo(4));
    assertThat(events.get(0).getSource(), sameInstance(admin));
    assertThat(events.get(0).getDeclarable(), instanceOf(AnonymousQueue.class));
    assertSame(toBeThrown, events.get(0).getThrowable().getCause());
    assertNull(events.get(1).getDeclarable());
    assertSame(toBeThrown, events.get(1).getThrowable().getCause());
    assertThat(events.get(2).getDeclarable(), instanceOf(DirectExchange.class));
    assertSame(toBeThrown, events.get(2).getThrowable().getCause());
    assertThat(events.get(3).getDeclarable(), instanceOf(Binding.class));
    assertSame(toBeThrown, events.get(3).getThrowable().getCause());

    assertSame(events.get(3), admin.getLastDeclarationExceptionEvent());
}

From source file:org.springframework.amqp.rabbit.core.RabbitTemplatePublisherCallbacksIntegrationTests.java

@Test
public void testPublisherConfirmNotReceived() throws Exception {
    ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class);
    Connection mockConnection = mock(Connection.class);
    Channel mockChannel = mock(Channel.class);
    when(mockChannel.isOpen()).thenReturn(true);

    when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString()))
            .thenReturn(mockConnection);
    when(mockConnection.isOpen()).thenReturn(true);
    doReturn(new PublisherCallbackChannelImpl(mockChannel)).when(mockConnection).createChannel();

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setPublisherConfirms(true);// w w w.j  a  v a  2 s  . c om
    final RabbitTemplate template = new RabbitTemplate(ccf);

    final AtomicBoolean confirmed = new AtomicBoolean();
    template.setConfirmCallback((correlationData, ack, cause) -> confirmed.set(true));
    template.convertAndSend(ROUTE, (Object) "message", new CorrelationData("abc"));
    Thread.sleep(5);
    assertEquals(1, template.getUnconfirmedCount());
    Collection<CorrelationData> unconfirmed = template.getUnconfirmed(-1);
    assertEquals(0, template.getUnconfirmedCount());
    assertEquals(1, unconfirmed.size());
    assertEquals("abc", unconfirmed.iterator().next().getId());
    assertFalse(confirmed.get());
}