List of usage examples for org.springframework.amqp.rabbit.listener DirectMessageListenerContainer setQueueNames
@Override public void setQueueNames(String... queueName)
From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerIntegrationTests.java
@Test public void testErrorHandler() throws Exception { brokerRunning.deleteQueues(Q1);/*from w ww . j a v a 2 s . co m*/ Queue q1 = new Queue(Q1, true, false, false, new ArgumentBuilder().put("x-dead-letter-exchange", "") .put("x-dead-letter-routing-key", DLQ1).get()); CachingConnectionFactory cf = new CachingConnectionFactory("localhost"); RabbitAdmin admin = new RabbitAdmin(cf); admin.declareQueue(q1); DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf); container.setQueueNames(Q1); container.setConsumersPerQueue(2); final AtomicReference<Channel> channel = new AtomicReference<>(); container.setMessageListener((ChannelAwareMessageListener) (m, c) -> { channel.set(c); throw new MessageConversionException( "intended - should be wrapped in an AmqpRejectAndDontRequeueException"); }); container.setBeanName("errorHandler"); container.setConsumerTagStrategy(new Tag()); container.afterPropertiesSet(); container.start(); RabbitTemplate template = new RabbitTemplate(cf); template.convertAndSend(Q1, "foo"); assertNotNull(template.receive(DLQ1, 10000)); container.stop(); assertTrue(consumersOnQueue(Q1, 0)); assertTrue(consumersOnQueue(Q2, 0)); assertTrue(activeConsumerCount(container, 0)); assertEquals(0, TestUtils.getPropertyValue(container, "consumersByQueue", MultiValueMap.class).size()); assertFalse(channel.get().isOpen()); cf.destroy(); }
From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerIntegrationTests.java
@Test public void testContainerNotRecoveredAfterExhaustingRecoveryBackOff() throws Exception { ConnectionFactory mockCF = mock(ConnectionFactory.class); given(mockCF.createConnection()).willThrow(new RuntimeException("intended - backOff test")); DirectMessageListenerContainer container = new DirectMessageListenerContainer(mockCF); container.setQueueNames("foo"); container.setRecoveryBackOff(new FixedBackOff(100, 3)); container.setMissingQueuesFatal(false); container.setBeanName("backOff"); container.setConsumerTagStrategy(new Tag()); container.afterPropertiesSet();/*from w w w . j a v a2 s . c o m*/ container.start(); // Since backOff exhausting makes listenerContainer as invalid (calls stop()), // it is enough to check the listenerContainer activity int n = 0; while (container.isActive() && n++ < 100) { Thread.sleep(100); } assertFalse(container.isActive()); }
From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerIntegrationTests.java
@Test public void testRecoverBrokerLoss() throws Exception { ConnectionFactory mockCF = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); given(connection.isOpen()).willReturn(true); given(mockCF.createConnection()).willReturn(connection); given(connection.createChannel(false)).willReturn(channel); given(channel.isOpen()).willReturn(true); final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(2); ArgumentCaptor<Consumer> consumerCaptor = ArgumentCaptor.forClass(Consumer.class); final String tag = "tag"; willAnswer(i -> {/*w w w. ja va 2 s.c o m*/ latch1.countDown(); latch2.countDown(); return tag; }).given(channel).basicConsume(eq("foo"), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), consumerCaptor.capture()); DirectMessageListenerContainer container = new DirectMessageListenerContainer(mockCF); container.setQueueNames("foo"); container.setBeanName("brokerLost"); container.setConsumerTagStrategy(q -> "tag"); container.setShutdownTimeout(1); container.setMonitorInterval(200); container.setFailedDeclarationRetryInterval(200); container.afterPropertiesSet(); container.start(); assertTrue(latch1.await(10, TimeUnit.SECONDS)); given(channel.isOpen()).willReturn(false); assertTrue(latch2.await(10, TimeUnit.SECONDS)); container.setShutdownTimeout(1); container.stop(); }
From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerIntegrationTests.java
@Test public void testCancelConsumerBeforeConsumeOk() throws Exception { ConnectionFactory mockCF = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); given(connection.isOpen()).willReturn(true); given(mockCF.createConnection()).willReturn(connection); given(connection.createChannel(false)).willReturn(channel); given(channel.isOpen()).willReturn(true); final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); ArgumentCaptor<Consumer> consumerCaptor = ArgumentCaptor.forClass(Consumer.class); final String tag = "tag"; willAnswer(i -> {/*from ww w. ja v a 2 s. c o m*/ latch1.countDown(); return tag; }).given(channel).basicConsume(eq("foo"), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), consumerCaptor.capture()); DirectMessageListenerContainer container = new DirectMessageListenerContainer(mockCF); container.setQueueNames("foo"); container.setBeanName("backOff"); container.setConsumerTagStrategy(q -> "tag"); container.setShutdownTimeout(1); container.afterPropertiesSet(); container.start(); assertTrue(latch1.await(10, TimeUnit.SECONDS)); Consumer consumer = consumerCaptor.getValue(); Executors.newSingleThreadExecutor().execute(() -> { container.stop(); latch2.countDown(); }); assertTrue(latch2.await(10, TimeUnit.SECONDS)); verify(channel).basicCancel(tag); // canceled properly even without consumeOk consumer.handleCancelOk(tag); }
From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerIntegrationTests.java
@Test public void testNonManagedContainerDoesntStartWhenConnectionFactoryDestroyed() throws Exception { CachingConnectionFactory cf = new CachingConnectionFactory("localhost"); ApplicationContext context = mock(ApplicationContext.class); cf.setApplicationContext(context);/*from w ww . ja v a2s. co m*/ cf.addConnectionListener(connection -> { cf.onApplicationEvent(new ContextClosedEvent(context)); cf.stop(); cf.destroy(); }); DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf); container.setMessageListener(m -> { }); container.setQueueNames(Q1); container.setBeanName("stopAfterDestroyBeforeStart"); container.afterPropertiesSet(); container.start(); int n = 0; while (n++ < 100 && container.isRunning()) { Thread.sleep(100); } assertFalse(container.isRunning()); }
From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerIntegrationTests.java
@Test public void testNonManagedContainerStopsWhenConnectionFactoryDestroyed() throws Exception { CachingConnectionFactory cf = new CachingConnectionFactory("localhost"); ApplicationContext context = mock(ApplicationContext.class); cf.setApplicationContext(context);/*from ww w . ja v a2s . com*/ DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf); final CountDownLatch latch = new CountDownLatch(1); container.setMessageListener(m -> { latch.countDown(); }); container.setQueueNames(Q1); container.setBeanName("stopAfterDestroy"); container.setIdleEventInterval(500); container.setFailedDeclarationRetryInterval(500); container.afterPropertiesSet(); container.start(); new RabbitTemplate(cf).convertAndSend(Q1, "foo"); assertTrue(latch.await(10, TimeUnit.SECONDS)); cf.onApplicationEvent(new ContextClosedEvent(context)); cf.stop(); cf.destroy(); int n = 0; while (n++ < 100 && container.isRunning()) { Thread.sleep(100); } assertFalse(container.isRunning()); }
From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerIntegrationTests.java
@Test public void testDeferredAcks() throws Exception { CachingConnectionFactory cf = new CachingConnectionFactory("localhost"); DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf); final CountDownLatch latch = new CountDownLatch(2); container.setMessageListener(m -> { latch.countDown();/*from w w w . j a v a 2 s .c o m*/ }); container.setQueueNames(Q1); container.setBeanName("deferredAcks"); container.setMessagesPerAck(2); container.afterPropertiesSet(); container.start(); RabbitTemplate rabbitTemplate = new RabbitTemplate(cf); rabbitTemplate.convertAndSend(Q1, "foo"); rabbitTemplate.convertAndSend(Q1, "bar"); assertTrue(latch.await(10, TimeUnit.SECONDS)); container.stop(); cf.stop(); cf.destroy(); }
From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerTests.java
@Test public void testErrorHandler() throws Exception { brokerRunning.getAdmin().deleteQueue(Q1); Queue q1 = new Queue(Q1, true, false, false, new ArgumentBuilder().put("x-dead-letter-exchange", "") .put("x-dead-letter-routing-key", DLQ1).get()); brokerRunning.getAdmin().declareQueue(q1); CachingConnectionFactory cf = new CachingConnectionFactory("localhost"); DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf); container.setQueueNames(Q1); container.setConsumersPerQueue(2);/*from w w w . j av a 2s .com*/ final AtomicReference<Channel> channel = new AtomicReference<>(); container.setMessageListener((ChannelAwareMessageListener) (m, c) -> { channel.set(c); throw new MessageConversionException( "intended - should be wrapped in an AmqpRejectAndDontRequeueException"); }); container.setBeanName("errorHandler"); container.setConsumerTagStrategy(new Tag()); container.afterPropertiesSet(); container.start(); RabbitTemplate template = new RabbitTemplate(cf); template.convertAndSend(Q1, "foo"); assertNotNull(template.receive(DLQ1, 10000)); container.stop(); assertTrue(consumersOnQueue(Q1, 0)); assertTrue(consumersOnQueue(Q2, 0)); assertTrue(activeConsumerCount(container, 0)); assertEquals(0, TestUtils.getPropertyValue(container, "consumersByQueue", MultiValueMap.class).size()); assertFalse(channel.get().isOpen()); cf.destroy(); }
From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerTests.java
@SuppressWarnings("unchecked") @Test/* w ww.ja va2 s . c o m*/ public void testRecoverBrokerLoss() throws Exception { ConnectionFactory mockCF = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); given(connection.isOpen()).willReturn(true); given(mockCF.createConnection()).willReturn(connection); given(connection.createChannel(false)).willReturn(channel); given(channel.isOpen()).willReturn(true); final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(2); ArgumentCaptor<Consumer> consumerCaptor = ArgumentCaptor.forClass(Consumer.class); final String tag = "tag"; willAnswer(i -> { latch1.countDown(); latch2.countDown(); return tag; }).given(channel).basicConsume(eq("foo"), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), consumerCaptor.capture()); DirectMessageListenerContainer container = new DirectMessageListenerContainer(mockCF); container.setQueueNames("foo"); container.setBeanName("brokerLost"); container.setConsumerTagStrategy(q -> "tag"); container.setShutdownTimeout(1); container.setMonitorInterval(200); container.setFailedDeclarationRetryInterval(200); container.afterPropertiesSet(); container.start(); assertTrue(latch1.await(10, TimeUnit.SECONDS)); given(channel.isOpen()).willReturn(false); assertTrue(latch2.await(10, TimeUnit.SECONDS)); container.setShutdownTimeout(1); container.stop(); }
From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerTests.java
@SuppressWarnings("unchecked") @Test//from w ww . j a v a 2 s .c o m public void testCancelConsumerBeforeConsumeOk() throws Exception { ConnectionFactory mockCF = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); given(connection.isOpen()).willReturn(true); given(mockCF.createConnection()).willReturn(connection); given(connection.createChannel(false)).willReturn(channel); given(channel.isOpen()).willReturn(true); final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); ArgumentCaptor<Consumer> consumerCaptor = ArgumentCaptor.forClass(Consumer.class); final String tag = "tag"; willAnswer(i -> { latch1.countDown(); return tag; }).given(channel).basicConsume(eq("foo"), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), consumerCaptor.capture()); DirectMessageListenerContainer container = new DirectMessageListenerContainer(mockCF); container.setQueueNames("foo"); container.setBeanName("backOff"); container.setConsumerTagStrategy(q -> "tag"); container.setShutdownTimeout(1); container.afterPropertiesSet(); container.start(); assertTrue(latch1.await(10, TimeUnit.SECONDS)); Consumer consumer = consumerCaptor.getValue(); Executors.newSingleThreadExecutor().execute(() -> { container.stop(); latch2.countDown(); }); assertTrue(latch2.await(10, TimeUnit.SECONDS)); verify(channel).basicCancel(tag); // canceled properly even without consumeOk consumer.handleCancelOk(tag); }