List of usage examples for org.springframework.amqp.rabbit.listener ListenerContainerConsumerFailedEvent isFatal
public boolean isFatal()
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerIntegration2Tests.java
@Test public void testDeleteOneQueue() throws Exception { CountDownLatch latch = new CountDownLatch(20); container = createContainer(new MessageListenerAdapter(new PojoListener(latch)), queue.getName(), queue1.getName());/*from ww w .j a v a2 s .co m*/ container.setFailedDeclarationRetryInterval(100); ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class); container.setApplicationEventPublisher(publisher); for (int i = 0; i < 10; i++) { template.convertAndSend(queue.getName(), i + "foo"); template.convertAndSend(queue1.getName(), i + "foo"); } boolean waited = latch.await(10, TimeUnit.SECONDS); assertTrue("Timed out waiting for message", waited); Map<?, ?> consumers = TestUtils.getPropertyValue(container, "consumers", Map.class); BlockingQueueConsumer consumer = (BlockingQueueConsumer) consumers.keySet().iterator().next(); admin.deleteQueue(queue1.getName()); latch = new CountDownLatch(10); container.setMessageListener(new MessageListenerAdapter(new PojoListener(latch))); for (int i = 0; i < 10; i++) { template.convertAndSend(queue.getName(), i + "foo"); } waited = latch.await(10, TimeUnit.SECONDS); assertTrue("Timed out waiting for message", waited); BlockingQueueConsumer newConsumer = consumer; int n = 0; while (n++ < 100 && newConsumer == consumer) { try { newConsumer = (BlockingQueueConsumer) consumers.keySet().iterator().next(); if (newConsumer == consumer) { break; } } catch (NoSuchElementException e) { // race; hasNext() won't help } Thread.sleep(100); } assertTrue("Failed to restart consumer", n < 100); Set<?> missingQueues = TestUtils.getPropertyValue(newConsumer, "missingQueues", Set.class); n = 0; while (n++ < 100 && missingQueues.size() == 0) { Thread.sleep(200); } assertTrue("Failed to detect missing queue", n < 100); ArgumentCaptor<ListenerContainerConsumerFailedEvent> captor = ArgumentCaptor .forClass(ListenerContainerConsumerFailedEvent.class); verify(publisher).publishEvent(captor.capture()); ListenerContainerConsumerFailedEvent event = captor.getValue(); assertThat(event.getThrowable(), instanceOf(ConsumerCancelledException.class)); assertFalse(event.isFatal()); DirectFieldAccessor dfa = new DirectFieldAccessor(newConsumer); dfa.setPropertyValue("lastRetryDeclaration", 0); dfa.setPropertyValue("retryDeclarationInterval", 100); admin.declareQueue(queue1); n = 0; while (n++ < 100 && missingQueues.size() > 0) { Thread.sleep(100); } assertTrue("Failed to redeclare missing queue", n < 100); latch = new CountDownLatch(20); container.setMessageListener(new MessageListenerAdapter(new PojoListener(latch))); for (int i = 0; i < 10; i++) { template.convertAndSend(queue.getName(), i + "foo"); template.convertAndSend(queue1.getName(), i + "foo"); } waited = latch.await(10, TimeUnit.SECONDS); assertTrue("Timed out waiting for message", waited); assertNull(template.receiveAndConvert(queue.getName())); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerIntegration2Tests.java
@Test public void testExclusive() throws Exception { Log logger = spy(TestUtils.getPropertyValue(this.template.getConnectionFactory(), "logger", Log.class)); doReturn(true).when(logger).isInfoEnabled(); new DirectFieldAccessor(this.template.getConnectionFactory()).setPropertyValue("logger", logger); CountDownLatch latch1 = new CountDownLatch(1000); SimpleMessageListenerContainer container1 = new SimpleMessageListenerContainer( template.getConnectionFactory()); container1.setMessageListener(new MessageListenerAdapter(new PojoListener(latch1))); container1.setQueueNames(queue.getName()); GenericApplicationContext context = new GenericApplicationContext(); context.getBeanFactory().registerSingleton("foo", queue); context.refresh();//from ww w .ja v a 2 s .co m container1.setApplicationContext(context); container1.setExclusive(true); container1.afterPropertiesSet(); container1.start(); int n = 0; while (n++ < 100 && container1.getActiveConsumerCount() < 1) { Thread.sleep(100); } assertTrue(n < 100); CountDownLatch latch2 = new CountDownLatch(1000); SimpleMessageListenerContainer container2 = new SimpleMessageListenerContainer( template.getConnectionFactory()); container2.setMessageListener(new MessageListenerAdapter(new PojoListener(latch2))); container2.setQueueNames(queue.getName()); container2.setApplicationContext(context); container2.setRecoveryInterval(1000); container2.setExclusive(true); // not really necessary, but likely people will make all consumers exclusive. ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class); container2.setApplicationEventPublisher(publisher); container2.afterPropertiesSet(); Log containerLogger = spy(TestUtils.getPropertyValue(container2, "logger", Log.class)); doReturn(true).when(containerLogger).isWarnEnabled(); new DirectFieldAccessor(container2).setPropertyValue("logger", containerLogger); container2.start(); for (int i = 0; i < 1000; i++) { template.convertAndSend(queue.getName(), i + "foo"); } assertTrue(latch1.await(10, TimeUnit.SECONDS)); assertEquals(1000, latch2.getCount()); container1.stop(); // container 2 should recover and process the next batch of messages for (int i = 0; i < 1000; i++) { template.convertAndSend(queue.getName(), i + "foo"); } assertTrue(latch2.await(10, TimeUnit.SECONDS)); container2.stop(); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(logger, atLeastOnce()).info(captor.capture()); assertThat(captor.getAllValues(), contains(containsString("exclusive"))); ArgumentCaptor<ListenerContainerConsumerFailedEvent> eventCaptor = ArgumentCaptor .forClass(ListenerContainerConsumerFailedEvent.class); verify(publisher).publishEvent(eventCaptor.capture()); ListenerContainerConsumerFailedEvent event = eventCaptor.getValue(); assertEquals("Consumer raised exception, attempting restart", event.getReason()); assertFalse(event.isFatal()); assertThat(event.getThrowable(), instanceOf(AmqpIOException.class)); verify(containerLogger).warn(any()); }