@Override public boolean isRunning() { return container.isRunning(); }
@Override public void resume() { if (!container.isRunning()) { container.start(); } }
@Override public void pause() { if (container.isRunning()) { container.stop(); } }
/** * {@inheritDoc} */ @Override public void start() { if (this.messageListenerContainer.isRunning()) { return; } this.messageListenerContainer.start(); }
/** * {@inheritDoc} */ @Override public void stop() { if (this.messageListenerContainer.isRunning()) { this.messageListenerContainer.stop(); } }
@Override protected void publishConsumerFailedEvent(String reason, boolean fatal, Throwable t) { if (!fatal || !isRunning()) { super.publishConsumerFailedEvent(reason, fatal, t); } else { try { this.abortEvents.put(new ListenerContainerConsumerFailedEvent(this, reason, t, fatal)); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }
@Override protected void publishConsumerFailedEvent(String reason, boolean fatal, Throwable t) { if (!fatal || !isRunning()) { super.publishConsumerFailedEvent(reason, fatal, t); } else { try { this.abortEvents.put(new ListenerContainerConsumerFailedEvent(this, reason, t, fatal)); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }
private boolean containerStoppedForAbortWithBadListener() throws InterruptedException { Log logger = spy(TestUtils.getPropertyValue(container, "logger", Log.class)); new DirectFieldAccessor(container).setPropertyValue("logger", logger); this.template.convertAndSend(queue.getName(), "foo"); int n = 0; while (n++ < 100 && this.container.isRunning()) { Thread.sleep(100); } ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(logger).error(captor.capture()); assertThat(captor.getValue(), containsString("Stopping container from aborted consumer")); return !this.container.isRunning(); }
@Test public void testMismatchedQueueDuringRestart() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config2.class); CountDownLatch[] latches = setUpChannelLatches(context); RabbitAdmin admin = context.getBean(RabbitAdmin.class); admin.deleteQueue(TEST_MISMATCH); assertTrue(latches[0].await(20, TimeUnit.SECONDS)); admin.declareQueue(new Queue(TEST_MISMATCH, false, false, true)); latches[2].countDown(); // let container thread continue to enable restart assertTrue(latches[1].await(20, TimeUnit.SECONDS)); SimpleMessageListenerContainer container = context.getBean(SimpleMessageListenerContainer.class); int n = 0; while (n++ < 200 && container.isRunning()) { Thread.sleep(100); } assertFalse(container.isRunning()); context.close(); }
@Test public void testMismatchedQueueDuringRestartMultiQueue() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config3.class); CountDownLatch[] latches = setUpChannelLatches(context); RabbitAdmin admin = context.getBean(RabbitAdmin.class); admin.deleteQueue(TEST_MISMATCH); assertTrue(latches[0].await(20, TimeUnit.SECONDS)); admin.declareQueue(new Queue(TEST_MISMATCH, false, false, true)); latches[2].countDown(); // let container thread continue to enable restart assertTrue(latches[1].await(20, TimeUnit.SECONDS)); SimpleMessageListenerContainer container = context.getBean(SimpleMessageListenerContainer.class); int n = 0; while (n++ < 200 && container.isRunning()) { Thread.sleep(100); } assertFalse(container.isRunning()); context.close(); }
@Test public void testErrorStopsContainer() throws Exception { this.container = createContainer((m) -> { throw new Error("testError"); }, false, this.queue.getName()); final CountDownLatch latch = new CountDownLatch(1); this.container.setApplicationEventPublisher(event -> { if (event instanceof ListenerContainerConsumerFailedEvent) { latch.countDown(); } }); this.container.setDefaultRequeueRejected(false); this.container.start(); this.template.convertAndSend(this.queue.getName(), "foo"); assertTrue(latch.await(10, TimeUnit.SECONDS)); int n = 0; while (n++ < 100 && this.container.isRunning()) { Thread.sleep(100); } assertFalse(this.container.isRunning()); }
@Test public void testAddQueuesAndStartInCycle() throws Exception { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel1 = mock(Channel.class); when(channel1.isOpen()).thenReturn(true); when(connectionFactory.createConnection()).thenReturn(connection); when(connection.createChannel(false)).thenReturn(channel1); final AtomicInteger count = new AtomicInteger(); doAnswer(invocation -> { Consumer cons = invocation.getArgument(6); String consumerTag = "consFoo" + count.incrementAndGet(); cons.handleConsumeOk(consumerTag); return consumerTag; }).when(channel1).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setMessageListener((MessageListener) message -> { }); container.afterPropertiesSet(); for (int i = 0; i < 10; i++) { container.addQueueNames("foo" + i); if (!container.isRunning()) { container.start(); } } container.stop(); }
@Test public void testTransientBadMessageDoesntStopContainerLambda() throws Exception { final CountDownLatch latch = new CountDownLatch(2); this.container = createContainer(new MessageListenerAdapter( (ReplyingMessageListener<String, Void>) m -> { latch.countDown(); return null; }), this.queue.getName()); this.template.convertAndSend(this.queue.getName(), "foo"); this.template.convertAndSend(this.queue.getName(), new Foo()); this.template.convertAndSend(this.queue.getName(), "foo"); assertTrue(latch.await(10, TimeUnit.SECONDS)); assertTrue(this.container.isRunning()); this.container.stop(); }
@Test public void testTransientBadMessageDoesntStopContainer() throws Exception { CountDownLatch latch = new CountDownLatch(3); this.container = createContainer(new MessageListenerAdapter(new PojoListener(latch, false)), this.queue.getName()); this.template.convertAndSend(this.queue.getName(), "foo"); this.template.convertAndSend(this.queue.getName(), new Foo()); this.template.convertAndSend(this.queue.getName(), new Bar()); this.template.convertAndSend(this.queue.getName(), "foo"); assertTrue(latch.await(10, TimeUnit.SECONDS)); assertTrue(this.container.isRunning()); this.container.stop(); }
@Test public void testPossibleAuthenticationFailureNotFatal() { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); given(connectionFactory.createConnection()) .willThrow(new AmqpAuthenticationException(new PossibleAuthenticationFailureException("intentional"))); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setQueueNames("foo"); container.setPossibleAuthenticationFailureFatal(false); container.start(); assertTrue(container.isActive()); assertTrue(container.isRunning()); container.destroy(); }
@Test public void testAddQueuesAndStartInCycle() throws Exception { final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer( this.connectionFactory); container.setMessageListener((MessageListener) message -> { }); container.setConcurrentConsumers(2); container.afterPropertiesSet(); RabbitAdmin admin = new RabbitAdmin(this.connectionFactory); for (int i = 0; i < 20; i++) { Queue queue = new Queue("testAddQueuesAndStartInCycle" + i); admin.declareQueue(queue); container.addQueueNames(queue.getName()); if (!container.isRunning()) { container.start(); } } int n = 0; while (n++ < 100 && container.getActiveConsumerCount() != 2) { Thread.sleep(100); } assertEquals(2, container.getActiveConsumerCount()); container.stop(); for (int i = 0; i < 20; i++) { admin.deleteQueue("testAddQueuesAndStartInCycle" + i); } connectionFactory.destroy(); }