@Override public void setQueueNames(String... queueName) { Assert.state(!isRunning(), "Cannot set queue names while running, use add/remove"); super.setQueueNames(queueName); }
private void removeQueues(Stream<String> queueNames) { if (isRunning()) { synchronized (this.consumersMonitor) { checkStartState(); queueNames.map(this.consumersByQueue::remove) .filter(Objects::nonNull) .flatMap(Collection::stream) .forEach(this::cancelConsumer); } } }
private void checkStartState() { if (!this.isRunning()) { try { Assert.state(this.startedLatch.await(START_WAIT_TIME, TimeUnit.SECONDS), "Container is not started - cannot adjust queues"); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new AmqpException("Interrupted waiting for start", e); } } }
@Override public void setQueueNames(String... queueName) { Assert.state(!isRunning(), "Cannot set queue names while running, use add/remove"); super.setQueueNames(queueName); }
private void checkStartState() { if (!this.isRunning()) { try { Assert.state(this.startedLatch.await(START_WAIT_TIME, TimeUnit.SECONDS), "Container is not started - cannot adjust queues"); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new AmqpException("Interrupted waiting for start", e); } } }
private void removeQueues(Stream<String> queueNames) { if (isRunning()) { synchronized (this.consumersMonitor) { checkStartState(); queueNames.map(this.consumersByQueue::remove) .filter(Objects::nonNull) .flatMap(Collection::stream) .forEach(this::cancelConsumer); } } }
/** * Each queue runs in its own consumer; set this property to create multiple * consumers for each queue. * If the container is already running, the number of consumers per queue will * be adjusted up or down as necessary. * @param consumersPerQueue the consumers per queue. */ public void setConsumersPerQueue(int consumersPerQueue) { if (isRunning()) { adjustConsumers(consumersPerQueue); } this.consumersPerQueue = consumersPerQueue; }
/** * Each queue runs in its own consumer; set this property to create multiple * consumers for each queue. * If the container is already running, the number of consumers per queue will * be adjusted up or down as necessary. * @param consumersPerQueue the consumers per queue. */ public void setConsumersPerQueue(int consumersPerQueue) { if (isRunning()) { adjustConsumers(consumersPerQueue); } this.consumersPerQueue = consumersPerQueue; }
private void addQueues(Stream<String> queueNameStream) { if (isRunning()) { synchronized (this.consumersMonitor) { checkStartState(); Set<String> current = getQueueNamesAsSet(); queueNameStream.forEach(queue -> { if (current.contains(queue)) { this.logger.warn("Queue " + queue + " is already configured for this container: " + this + ", ignoring add"); } else { consumeFromQueue(queue); } }); } } }
private void addQueues(Stream<String> queueNameStream) { if (isRunning()) { synchronized (this.consumersMonitor) { checkStartState(); Set<String> current = getQueueNamesAsSet(); queueNameStream.forEach(queue -> { if (current.contains(queue)) { this.logger.warn("Queue " + queue + " is already configured for this container: " + this + ", ignoring add"); } else { consumeFromQueue(queue); } }); } } }
while (!DirectMessageListenerContainer.this.started && isRunning()) { this.cancellationLock.reset(); try {
while (!DirectMessageListenerContainer.this.started && isRunning()) { this.cancellationLock.reset(); try {
@Test public void testNonManagedContainerDoesntStartWhenConnectionFactoryDestroyed() throws Exception { CachingConnectionFactory cf = new CachingConnectionFactory("localhost"); ApplicationContext context = mock(ApplicationContext.class); cf.setApplicationContext(context); cf.addConnectionListener(connection -> { cf.onApplicationEvent(new ContextClosedEvent(context)); 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()); }
@Test public void testNonManagedContainerStopsWhenConnectionFactoryDestroyed() throws Exception { CachingConnectionFactory cf = new CachingConnectionFactory("localhost"); ApplicationContext context = mock(ApplicationContext.class); cf.setApplicationContext(context); 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.destroy(); int n = 0; while (n++ < 100 && container.isRunning()) { Thread.sleep(100); } assertFalse(container.isRunning()); }