gpt4 book ai didi

spring - DefaultJmsListenerContainerFactory 和并发连接未关闭

转载 作者:行者123 更新时间:2023-12-01 22:42:09 28 4
gpt4 key购买 nike

我使用 Spring 4.x 的 DefaultJmsListenerContainerFactory 来连接到 ActiveMQ 队列,使用 @JmsListener 处理来自该队列的消息,然后将消息推送到同一 ActiveMQ 代理上的主题。

我为消费者/监听器和生产者使用单个缓存连接工厂,并将缓存消费者设置为 false,以便我可以缓存生产者,但不能缓存消费者。我还将并发度设置为 1-3,我预计应用程序启动时队列中至少有 1 个消费者,并且随着消息的增加,消费者的数量将达到 3。消息减少了,我预计消费者的数量也会回落到 1。但是,如果我查看线程(defaultmessagelistenercontainer-2/3),它们处于等待状态,并且不会关闭。当负载减弱时,消费者的数量也会关闭,这不是预期的行为吗?请参阅下面的配置,并让我知道此行为是否不是开箱即用的,以及我是否需要添加一些内容才能使其按我上面所述工作。

ApplicationContext.java

    @Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() throws Throwable {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();

factory.setConnectionFactory(connectionFactory());
factory.setConcurrency(environment.getProperty("jms.connections.concurrent"));
factory.setSessionTransacted(environment.getProperty("jms.connections.transacted", Boolean.class));
return factory;
}

@Bean
public CachingConnectionFactory connectionFactory(){
RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
redeliveryPolicy.setInitialRedeliveryDelay(environment.getProperty("jms.redelivery.initial-delay", Long.class));
redeliveryPolicy.setRedeliveryDelay(environment.getProperty("jms.redelivery.delay", Long.class));
redeliveryPolicy.setMaximumRedeliveries(environment.getProperty("jms.redelivery.maximum", Integer.class));
redeliveryPolicy.setUseExponentialBackOff(environment.getProperty("jms.redelivery.use-exponential-back-off", Boolean.class));
redeliveryPolicy.setBackOffMultiplier(environment.getProperty("jms.redelivery.back-off-multiplier", Double.class));

ActiveMQConnectionFactory activeMQ = new ActiveMQConnectionFactory(environment.getProperty("jms.queue.username"), environment.getProperty("jms.queue.password"), environment.getProperty("jms.broker.endpoint"));
activeMQ.setRedeliveryPolicy(redeliveryPolicy);
activeMQ.setPrefetchPolicy(prefetchPolicy());

CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(activeMQ);
cachingConnectionFactory.setCacheConsumers(environment.getProperty("jms.connections.cache.consumers", Boolean.class));
cachingConnectionFactory.setSessionCacheSize(environment.getProperty("jms.cache.size", Integer.class));
return cachingConnectionFactory;
}

@Bean
public JmsMessagingTemplate jmsMessagingTemplate(){
ActiveMQTopic activeMQ = new ActiveMQTopic(environment.getProperty("jms.queue.out"));

JmsMessagingTemplate jmsMessagingTemplate = new JmsMessagingTemplate(connectionFactory());
jmsMessagingTemplate.setDefaultDestination(activeMQ);

return jmsMessagingTemplate;
}

application.properties

jms.connections.concurrent=1-3
jms.connections.prefetch=1000
jms.connections.transacted=true
jms.connections.cache.consumers=false
jms.redelivery.initial-delay=1000
jms.redelivery.delay=1000
jms.redelivery.maximum=5
jms.redelivery.use-exponential-back-off=true
jms.redelivery.back-off-multiplier=2
jms.cache.size=3
jms.queue.in=in.queue
jms.queue.out=out.queue
jms.broker.endpoint=failover:(tcp://localhost:61616)

最佳答案

尝试设置maxMessagesPerTask > 0

@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() throws Throwable {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();

factory.setConnectionFactory(connectionFactory());
factory.setMaxMessagesPerTask(1);
factory.setConcurrency(environment.getProperty("jms.connections.concurrent"));
factory.setSessionTransacted(environment.getProperty("jms.connections.transacted", Boolean.class));
return factory;
}

您可以引用文档http://docs.spring.io/spring-framework/docs/4.3.x/javadoc-api/org/springframework/jms/listener/DefaultMessageListenerContainer.html#setMaxMessagesPerTask-int-

jms.connections.prefetch=1000意味着如果您有 1000 条消息在 Q 上等待,您将只启动 1 个线程来处理这 1000 条消息。

例如jms.connections.prefetch=1意味着消息将被平等地分派(dispatch)到所有可用线程,但最好设置 maxMessagesPerTask < 0因为长生命周期任务避免了频繁的线程上下文切换。 http://activemq.apache.org/what-is-the-prefetch-limit-for.html

关于spring - DefaultJmsListenerContainerFactory 和并发连接未关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39420769/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com