/** * @param consumersPerQueue the consumersPerQueue. * @return the spec. * @see DirectMessageListenerContainer#setConsumersPerQueue(int) */ public DirectMessageListenerContainerSpec consumersPerQueue(int consumersPerQueue) { this.listenerContainer.setConsumersPerQueue(consumersPerQueue); return this; }
/** * @param consumersPerQueue the consumersPerQueue. * @return the spec. * @see DirectMessageListenerContainer#setConsumersPerQueue(int) */ public DirectMessageListenerContainerSpec consumersPerQueue(int consumersPerQueue) { this.listenerContainer.setConsumersPerQueue(consumersPerQueue); return this; }
dmlc.setConsumersPerQueue(this.consumersPerQueue); container = dmlc;
@Override protected void processMonitorTask() { long now = System.currentTimeMillis(); synchronized (this.consumersMonitor) { long reduce = this.consumers.stream() .filter(c -> this.whenUsed.containsKey(c) && !this.inUseConsumerChannels.containsValue(c) && this.whenUsed.get(c) < now - getIdleEventInterval()) .count(); if (reduce > 0) { if (logger.isDebugEnabled()) { logger.debug("Reducing idle consumes by " + reduce); } this.consumerCount = (int) Math.max(0, this.consumerCount - reduce); super.setConsumersPerQueue(this.consumerCount); } } }
@Override protected void processMonitorTask() { long now = System.currentTimeMillis(); synchronized (this.consumersMonitor) { long reduce = this.consumers.stream() .filter(c -> this.whenUsed.containsKey(c) && !this.inUseConsumerChannels.containsValue(c) && this.whenUsed.get(c) < now - getIdleEventInterval()) .count(); if (reduce > 0) { if (logger.isDebugEnabled()) { logger.debug("Reducing idle consumes by " + reduce); } this.consumerCount = (int) Math.max(0, this.consumerCount - reduce); super.setConsumersPerQueue(this.consumerCount); } } }
@Override protected void doStart() { if (!isRunning()) { this.consumerCount = 0; super.setConsumersPerQueue(0); super.doStart(); } }
@Override protected void doStart() { if (!isRunning()) { this.consumerCount = 0; super.setConsumersPerQueue(0); super.doStart(); } }
@Override protected void initializeContainer(DirectMessageListenerContainer instance, RabbitListenerEndpoint endpoint) { super.initializeContainer(instance, endpoint); if (this.taskScheduler != null) { instance.setTaskScheduler(this.taskScheduler); } if (this.monitorInterval != null) { instance.setMonitorInterval(this.monitorInterval); } if (endpoint != null && endpoint.getConcurrency() != null) { try { instance.setConsumersPerQueue(Integer.parseInt(endpoint.getConcurrency())); } catch (NumberFormatException e) { throw new IllegalStateException("Failed to parse concurrency: " + e.getMessage(), e); } } else if (this.consumersPerQueue != null) { instance.setConsumersPerQueue(this.consumersPerQueue); } }
public DirectReplyToMessageListenerContainer(ConnectionFactory connectionFactory) { super(connectionFactory); super.setQueueNames(Address.AMQ_RABBITMQ_REPLY_TO); setAcknowledgeMode(AcknowledgeMode.NONE); super.setConsumersPerQueue(0); super.setIdleEventInterval(DEFAULT_IDLE); }
public DirectReplyToMessageListenerContainer(ConnectionFactory connectionFactory) { super(connectionFactory); super.setQueueNames(Address.AMQ_RABBITMQ_REPLY_TO); setAcknowledgeMode(AcknowledgeMode.NONE); super.setConsumersPerQueue(0); super.setIdleEventInterval(DEFAULT_IDLE); }
/** * Get the channel holder associated with a direct reply-to consumer; contains a * consumer epoch to prevent inappropriate releases. * @return the channel holder. */ public ChannelHolder getChannelHolder() { synchronized (this.consumersMonitor) { ChannelHolder channelHolder = null; while (channelHolder == null) { if (!isRunning()) { throw new IllegalStateException("Direct reply-to container is not running"); } for (SimpleConsumer consumer : this.consumers) { Channel candidate = consumer.getChannel(); if (candidate.isOpen() && this.inUseConsumerChannels.putIfAbsent(candidate, consumer) == null) { channelHolder = new ChannelHolder(candidate, consumer.incrementAndGetEpoch()); this.whenUsed.put(consumer, System.currentTimeMillis()); break; } } if (channelHolder == null) { this.consumerCount++; super.setConsumersPerQueue(this.consumerCount); } } return channelHolder; } }
@Override protected void initializeContainer(DirectMessageListenerContainer instance, RabbitListenerEndpoint endpoint) { super.initializeContainer(instance, endpoint); if (this.taskScheduler != null) { instance.setTaskScheduler(this.taskScheduler); } if (this.monitorInterval != null) { instance.setMonitorInterval(this.monitorInterval); } if (endpoint != null && endpoint.getConcurrency() != null) { try { instance.setConsumersPerQueue(Integer.parseInt(endpoint.getConcurrency())); } catch (NumberFormatException e) { throw new IllegalStateException("Failed to parse concurrency: " + e.getMessage(), e); } } else if (this.consumersPerQueue != null) { instance.setConsumersPerQueue(this.consumersPerQueue); } if (this.messagesPerAck != null) { instance.setMessagesPerAck(this.messagesPerAck); } if (this.ackTimeout != null) { instance.setAckTimeout(this.ackTimeout); } }
/** * Get the channel holder associated with a direct reply-to consumer; contains a * consumer epoch to prevent inappropriate releases. * @return the channel holder. */ public ChannelHolder getChannelHolder() { synchronized (this.consumersMonitor) { ChannelHolder channelHolder = null; while (channelHolder == null) { if (!isRunning()) { throw new IllegalStateException("Direct reply-to container is not running"); } for (SimpleConsumer consumer : this.consumers) { Channel candidate = consumer.getChannel(); if (candidate.isOpen() && this.inUseConsumerChannels.putIfAbsent(candidate, consumer) == null) { channelHolder = new ChannelHolder(candidate, consumer.incrementAndGetEpoch()); this.whenUsed.put(consumer, System.currentTimeMillis()); break; } } if (channelHolder == null) { this.consumerCount++; super.setConsumersPerQueue(this.consumerCount); } } return channelHolder; } }
@Test public void testAdvice() throws Exception { CachingConnectionFactory cf = new CachingConnectionFactory("localhost"); DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf); container.setQueueNames(Q1, Q2); container.setConsumersPerQueue(2); final CountDownLatch latch = new CountDownLatch(2); container.setMessageListener(m -> latch.countDown()); final CountDownLatch adviceLatch = new CountDownLatch(2); MethodInterceptor advice = i -> { adviceLatch.countDown(); return i.proceed(); }; container.setAdviceChain(advice); container.setBeanName("advice"); container.setConsumerTagStrategy(new Tag()); container.afterPropertiesSet(); container.start(); RabbitTemplate template = new RabbitTemplate(cf); template.convertAndSend(Q1, "foo"); template.convertAndSend(Q1, "bar"); assertTrue(latch.await(10, TimeUnit.SECONDS)); assertTrue(adviceLatch.await(10, TimeUnit.SECONDS)); container.stop(); assertTrue(consumersOnQueue(Q1, 0)); assertTrue(consumersOnQueue(Q2, 0)); assertTrue(activeConsumerCount(container, 0)); assertEquals(0, TestUtils.getPropertyValue(container, "consumersByQueue", MultiValueMap.class).size()); cf.destroy(); }
DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf); container.setQueueNames(Q1, Q2); container.setConsumersPerQueue(4); container.setMessageListener(new MessageListenerAdapter((ReplyingMessageListener<String, String>) in -> { if ("foo".equals(in) || "bar".equals(in)) { assertTrue(consumersOnQueue(Q1, 4)); assertTrue(consumersOnQueue(Q2, 4)); container.setConsumersPerQueue(1); assertTrue(consumersOnQueue(Q1, 1)); assertTrue(consumersOnQueue(Q2, 1)); container.setConsumersPerQueue(2); assertTrue(consumersOnQueue(Q1, 2)); assertTrue(consumersOnQueue(Q2, 2));
DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf); container.setQueueNames(Q1, Q2); container.setConsumersPerQueue(2); container.setMessageListener(new MessageListenerAdapter((ReplyingMessageListener<String, String>) in -> { if ("foo".equals(in) || "bar".equals(in)) {
DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf); container.setQueueNames(Q1); container.setConsumersPerQueue(2); final AtomicReference<Channel> channel = new AtomicReference<>(); container.setMessageListener((ChannelAwareMessageListener) (m, c) -> {
container.setConsumersPerQueue(2); container.setConsumersPerQueue(2); container.setMessageListener(m -> { }); container.setFailedDeclarationRetryInterval(500);
cf.setExecutor(executor); DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf); container.setConsumersPerQueue(2); container.setMessageListener(new MessageListenerAdapter((ReplyingMessageListener<String, String>) in -> { if ("foo".equals(in) || "bar".equals(in)) {
cf.setExecutor(executor); DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf); container.setConsumersPerQueue(2); container.setMessageListener(new MessageListenerAdapter((ReplyingMessageListener<String, String>) in -> { if ("foo".equals(in) || "bar".equals(in)) {