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

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

Introduction

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

Prototype

public LocalizedQueueConnectionFactory(ConnectionFactory defaultConnectionFactory, String[] addresses,
        String[] adminUris, String[] nodes, String vhost, String username, String password, boolean useSSL,
        @Nullable Resource sslPropertiesLocation) 

Source Link

Usage

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

@SuppressWarnings("unchecked")
@Test/*from  w  w w. j  av a2  s  .co m*/
public void testFailOver() throws Exception {
    ConnectionFactory defaultConnectionFactory = mockCF("localhost:1234", null);
    String rabbit1 = "localhost:1235";
    String rabbit2 = "localhost:1236";
    String[] addresses = new String[] { rabbit1, rabbit2 };
    String[] adminUris = new String[] { "http://localhost:11235", "http://localhost:11236" };
    String[] nodes = new String[] { "rabbit@foo", "rabbit@bar" };
    String vhost = "/";
    String username = "guest";
    String password = "guest";
    final AtomicBoolean firstServer = new AtomicBoolean(true);
    final Client client1 = doCreateClient(adminUris[0], username, password, nodes[0]);
    final Client client2 = doCreateClient(adminUris[1], username, password, nodes[1]);
    final Map<String, ConnectionFactory> mockCFs = new HashMap<String, ConnectionFactory>();
    CountDownLatch latch1 = new CountDownLatch(1);
    CountDownLatch latch2 = new CountDownLatch(1);
    mockCFs.put(rabbit1, mockCF(rabbit1, latch1));
    mockCFs.put(rabbit2, mockCF(rabbit2, latch2));
    LocalizedQueueConnectionFactory lqcf = new LocalizedQueueConnectionFactory(defaultConnectionFactory,
            addresses, adminUris, nodes, vhost, username, password, false, null) {

        @Override
        protected Client createClient(String adminUri, String username, String password)
                throws MalformedURLException, URISyntaxException {
            return firstServer.get() ? client1 : client2;
        }

        @Override
        protected ConnectionFactory createConnectionFactory(String address, String node) throws Exception {
            return mockCFs.get(address);
        }

    };
    Log logger = spy(TestUtils.getPropertyValue(lqcf, "logger", Log.class));
    doReturn(true).when(logger).isInfoEnabled();
    new DirectFieldAccessor(lqcf).setPropertyValue("logger", logger);
    doAnswer(new CallsRealMethods()).when(logger).debug(anyString());
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(lqcf);
    container.setQueueNames("q");
    container.afterPropertiesSet();
    container.start();
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    Channel channel = this.channels.get(rabbit1);
    assertNotNull(channel);
    verify(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(),
            Matchers.any(Consumer.class));
    verify(logger, atLeast(1)).info(captor.capture());
    assertTrue(assertLog(captor.getAllValues(), "Queue: q is on node: rabbit@foo at: localhost:1235"));

    // Fail rabbit1 and verify the container switches to rabbit2

    firstServer.set(false);
    this.consumers.get(rabbit1).handleCancel(consumerTags.get(rabbit1));
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    channel = this.channels.get(rabbit2);
    assertNotNull(channel);
    verify(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(),
            Matchers.any(Consumer.class));
    container.stop();
    verify(logger, atLeast(1)).info(captor.capture());
    assertTrue(assertLog(captor.getAllValues(), "Queue: q is on node: rabbit@bar at: localhost:1236"));
}

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

@Test
public void test2Queues() throws Exception {
    try {//  w  ww.jav a 2s  .c o  m
        String rabbit1 = "localhost:1235";
        String rabbit2 = "localhost:1236";
        String[] addresses = new String[] { rabbit1, rabbit2 };
        String[] adminUris = new String[] { "http://localhost:11235", "http://localhost:11236" };
        String[] nodes = new String[] { "rabbit@foo", "rabbit@bar" };
        String vhost = "/";
        String username = "guest";
        String password = "guest";
        LocalizedQueueConnectionFactory lqcf = new LocalizedQueueConnectionFactory(
                mockCF("localhost:1234", null), addresses, adminUris, nodes, vhost, username, password, false,
                null);
        lqcf.getTargetConnectionFactory("[foo, bar]");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString(
                "Cannot use LocalizedQueueConnectionFactory with more than one queue: [foo, bar]"));
    }
}