List of usage examples for org.springframework.amqp.rabbit.listener SimpleMessageListenerContainer afterPropertiesSet
@Override public final void afterPropertiesSet()
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerIntegration2Tests.java
@Test public void testRestartConsumerOnConnectionLossDuringQueueDeclare() throws Exception { this.template.convertAndSend(queue.getName(), "foo"); ConnectionFactory connectionFactory = new CachingConnectionFactory("localhost", BrokerTestUtils.getPort()); final AtomicBoolean networkGlitch = new AtomicBoolean(); class MockChannel extends PublisherCallbackChannelImpl { MockChannel(Channel delegate) { super(delegate); }/*from ww w . ja va 2s . c om*/ @Override public DeclareOk queueDeclarePassive(String queue) throws IOException { if (networkGlitch.compareAndSet(false, true)) { getConnection().close(); throw new IOException("Intentional connection reset"); } return super.queueDeclarePassive(queue); } } Connection connection = spy(connectionFactory.createConnection()); when(connection.createChannel(anyBoolean())) .then(invocation -> new MockChannel((Channel) invocation.callRealMethod())); DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory); dfa.setPropertyValue("connection", connection); CountDownLatch latch = new CountDownLatch(1); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setMessageListener(new MessageListenerAdapter(new PojoListener(latch))); container.setQueueNames(queue.getName()); container.setRecoveryInterval(500); container.afterPropertiesSet(); container.start(); assertTrue(latch.await(10, TimeUnit.SECONDS)); assertTrue(networkGlitch.get()); container.stop(); ((DisposableBean) connectionFactory).destroy(); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerIntegration2Tests.java
@Test public void testRestartConsumerMissingQueue() throws Exception { Queue queue = new AnonymousQueue(); this.template.convertAndSend(queue.getName(), "foo"); ConnectionFactory connectionFactory = new CachingConnectionFactory("localhost", BrokerTestUtils.getPort()); CountDownLatch latch = new CountDownLatch(1); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setMessageListener(new MessageListenerAdapter(new PojoListener(latch))); container.setQueues(queue);//from w ww . ja v a 2 s. c o m container.setRecoveryInterval(500); container.setMissingQueuesFatal(false); container.setDeclarationRetries(1); container.setFailedDeclarationRetryInterval(100); container.setRetryDeclarationInterval(30000); container.afterPropertiesSet(); container.start(); new RabbitAdmin(connectionFactory).declareQueue(queue); this.template.convertAndSend(queue.getName(), "foo"); assertTrue(latch.await(10, TimeUnit.SECONDS)); // verify properties propagated to consumer BlockingQueueConsumer consumer = (BlockingQueueConsumer) TestUtils .getPropertyValue(container, "consumers", Map.class).keySet().iterator().next(); assertEquals(1, TestUtils.getPropertyValue(consumer, "declarationRetries")); assertEquals(100L, TestUtils.getPropertyValue(consumer, "failedDeclarationRetryInterval")); assertEquals(30000L, TestUtils.getPropertyValue(consumer, "retryDeclarationInterval")); container.stop(); ((DisposableBean) connectionFactory).destroy(); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerIntegration2Tests.java
private SimpleMessageListenerContainer createContainer(Object listener, String... queueNames) { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer( template.getConnectionFactory()); if (listener != null) { container.setMessageListener(listener); }/*from w w w . j a v a2 s .co m*/ container.setQueueNames(queueNames); container.afterPropertiesSet(); container.start(); return container; }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerLongTests.java
private void testChangeConsumerCountGuts(boolean transacted) throws Exception { final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost"); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory); try {/* w w w . jav a2 s.c om*/ container.setMessageListener(new MessageListenerAdapter(this)); container.setQueueNames("foo"); container.setAutoStartup(false); container.setConcurrentConsumers(2); container.setChannelTransacted(transacted); container.afterPropertiesSet(); assertEquals(2, ReflectionTestUtils.getField(container, "concurrentConsumers")); container.start(); waitForNConsumers(container, 2); container.setConcurrentConsumers(1); waitForNConsumers(container, 1); container.setMaxConcurrentConsumers(3); RabbitTemplate template = new RabbitTemplate(singleConnectionFactory); for (int i = 0; i < 20; i++) { template.convertAndSend("foo", "foo"); } waitForNConsumers(container, 2); // increased consumers due to work waitForNConsumers(container, 1, 20000); // should stop the extra consumer after 10 seconds idle container.setConcurrentConsumers(3); waitForNConsumers(container, 3); container.stop(); waitForNConsumers(container, 0); singleConnectionFactory.destroy(); } finally { container.stop(); } }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerLongTests.java
@Test public void testAddQueuesAndStartInCycle() throws Exception { final SingleConnectionFactory connectionFactory = new SingleConnectionFactory("localhost"); final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setMessageListener((MessageListener) message -> { });/* ww w . java 2 s . c o m*/ container.setConcurrentConsumers(2); container.afterPropertiesSet(); RabbitAdmin admin = new RabbitAdmin(connectionFactory); for (int i = 0; i < 20; i++) { AnonymousQueue anonymousQueue = new AnonymousQueue(); admin.declareQueue(anonymousQueue); container.addQueueNames(anonymousQueue.getName()); if (!container.isRunning()) { container.start(); } } int n = 0; while (n++ < 100 && container.getActiveConsumerCount() != 2) { Thread.sleep(100); } assertEquals(2, container.getActiveConsumerCount()); container.stop(); connectionFactory.destroy(); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java
@Test public void testChannelTransactedOverriddenWhenTxManager() throws Exception { final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost"); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory); container.setMessageListener(new MessageListenerAdapter(this)); container.setQueueNames("foo"); container.setChannelTransacted(false); container.setTransactionManager(new TestTransactionManager()); container.afterPropertiesSet(); assertTrue(TestUtils.getPropertyValue(container, "transactional", Boolean.class)); container.stop();//from ww w . ja v a2 s . com singleConnectionFactory.destroy(); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java
@Test public void testInconsistentTransactionConfiguration() throws Exception { final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost"); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory); container.setMessageListener(new MessageListenerAdapter(this)); container.setQueueNames("foo"); container.setChannelTransacted(false); container.setAcknowledgeMode(AcknowledgeMode.NONE); container.setTransactionManager(new TestTransactionManager()); expectedException.expect(IllegalStateException.class); container.afterPropertiesSet(); container.stop();/*from w ww .ja v a 2 s.c o m*/ singleConnectionFactory.destroy(); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java
@Test public void testInconsistentAcknowledgeConfiguration() throws Exception { final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost"); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory); container.setMessageListener(new MessageListenerAdapter(this)); container.setQueueNames("foo"); container.setChannelTransacted(true); container.setAcknowledgeMode(AcknowledgeMode.NONE); expectedException.expect(IllegalStateException.class); container.afterPropertiesSet(); container.stop();/*from ww w .j av a 2s .c o m*/ singleConnectionFactory.destroy(); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java
@Test public void testDefaultConsumerCount() throws Exception { final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost"); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory); container.setMessageListener(new MessageListenerAdapter(this)); container.setQueueNames("foo"); container.setAutoStartup(false);/*w w w .j a va 2s. co m*/ container.afterPropertiesSet(); assertEquals(1, ReflectionTestUtils.getField(container, "concurrentConsumers")); container.stop(); singleConnectionFactory.destroy(); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java
@SuppressWarnings("unchecked") @Test/*from w w w. ja v a2 s . c o m*/ public void testConsumerArgs() throws Exception { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); when(connectionFactory.createConnection()).thenReturn(connection); when(connection.createChannel(false)).thenReturn(channel); final AtomicReference<Consumer> consumer = new AtomicReference<Consumer>(); final AtomicReference<Map<?, ?>> args = new AtomicReference<Map<?, ?>>(); doAnswer(invocation -> { consumer.set((Consumer) invocation.getArguments()[6]); consumer.get().handleConsumeOk("foo"); args.set((Map<?, ?>) invocation.getArguments()[5]); return "foo"; }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), any(Map.class), any(Consumer.class)); final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setQueueNames("foo"); container.setMessageListener((MessageListener) message -> { }); container.setConsumerArguments(Collections.<String, Object>singletonMap("x-priority", Integer.valueOf(10))); container.afterPropertiesSet(); container.start(); verify(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), any(Map.class), any(Consumer.class)); assertTrue(args.get() != null); assertEquals(10, args.get().get("x-priority")); consumer.get().handleCancelOk("foo"); container.stop(); }