@Override public synchronized void stop() { if (this.running) { if (this.container != null) { this.container.stop(); } if (this.directReplyToContainer != null) { this.directReplyToContainer.stop(); } for (RabbitFuture<?> future : this.pending.values()) { future.setNackCause("AsyncRabbitTemplate was stopped while waiting for reply"); future.cancel(true); } if (this.internalTaskScheduler) { ((ThreadPoolTaskScheduler) this.taskScheduler).destroy(); this.taskScheduler = null; } } this.running = false; }
@Override public synchronized void stop() { if (this.running) { if (this.container != null) { this.container.stop(); } if (this.directReplyToContainer != null) { this.directReplyToContainer.stop(); } for (RabbitFuture<?> future : this.pending.values()) { future.setNackCause("AsyncRabbitTemplate was stopped while waiting for reply"); future.cancel(true); } if (this.internalTaskScheduler) { ((ThreadPoolTaskScheduler) this.taskScheduler).destroy(); this.taskScheduler = null; } } this.running = false; }
@Test public void testReplyToReleaseWithCancel() throws Exception { CachingConnectionFactory cf = new CachingConnectionFactory("localhost"); DirectReplyToMessageListenerContainer container = new DirectReplyToMessageListenerContainer(cf); container.setBeanName("releaseCancel"); final CountDownLatch consumeLatch = new CountDownLatch(1); final CountDownLatch releaseLatch = new CountDownLatch(1); container.setApplicationEventPublisher(e -> { if (e instanceof ListenerContainerConsumerTerminatedEvent) { releaseLatch.countDown(); } else if (e instanceof ConsumeOkEvent) { consumeLatch.countDown(); } }); container.afterPropertiesSet(); container.start(); ChannelHolder channelHolder = container.getChannelHolder(); assertTrue(consumeLatch.await(10, TimeUnit.SECONDS)); container.releaseConsumerFor(channelHolder, true, "foo"); assertTrue(releaseLatch.await(10, TimeUnit.SECONDS)); container.stop(); cf.destroy(); }
container.releaseConsumerFor(channel2, false, null); assertThat(inUse.size(), equalTo(0)); container.stop(); connectionFactory.destroy();
@Test public void testReplyToConsumersReduced() throws Exception { CachingConnectionFactory cf = new CachingConnectionFactory("localhost"); DirectReplyToMessageListenerContainer container = new DirectReplyToMessageListenerContainer(cf); container.setBeanName("reducing"); container.setIdleEventInterval(100); CountDownLatch latch = new CountDownLatch(5); container.setApplicationEventPublisher(e -> { if (e instanceof ListenerContainerIdleEvent) { latch.countDown(); } }); container.afterPropertiesSet(); container.start(); ChannelHolder channelHolder1 = container.getChannelHolder(); ChannelHolder channelHolder2 = container.getChannelHolder(); assertTrue(activeConsumerCount(container, 2)); container.releaseConsumerFor(channelHolder2, false, null); assertTrue(latch.await(10, TimeUnit.SECONDS)); assertTrue(channelHolder1.getChannel().isOpen()); container.releaseConsumerFor(channelHolder1, false, null); assertTrue(activeConsumerCount(container, 0)); container.stop(); cf.destroy(); }