gpt4 book ai didi

java - 如何为多个主题配置多个JmsListener

转载 作者:行者123 更新时间:2023-12-02 06:18:10 25 4
gpt4 key购买 nike

在我的项目中,我添加了两个不同的 JmsListener,但是当我在 ActiveMQ 面板中运行项目时,只有其中一个主题有消费者!

那么我应该为每个 JmsListener 添加单独的 jmsListenerContainerFactory 配置吗??

@JmsListener(destination = "foo1")
public void foo1(final Message jsonMessage) throws JMSException {
...
}

@JmsListener(destination = "foo2")
public void foo12(final Message jsonMessage) throws JMSException {
...
}

编辑:这是来自 JMS 配置文件:

@Configuration
@EnableJms
public class FooJmsConfig {

@Bean
public ActiveMQConnectionFactory connectionFactory() {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL(BROKER_URL);
connectionFactory.setPassword(BROKER_USERNAME);
connectionFactory.setUserName(BROKER_PASSWORD);
connectionFactory.setUseCompression(true);
connectionFactory.setClientID("FPP_API");
connectionFactory.setConnectionIDPrefix("DRR");
connectionFactory.setUseAsyncSend(true);
return connectionFactory;
}

@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
factory.setConcurrency("1-1");
factory.setPubSubDomain(true);
factory.setSubscriptionDurable(true);
return factory;
}
}

最佳答案

我发现,通过setClientID() 从 ActiveMQConnectionFactory 提供程序方法移动到 DefaultJmsListenerContainerFactory 提供程序方法

对于每个 jmsListener,我只能有一个全局 ActiveMQConnectionFactory 提供程序方法和多个 DefaultJmsListenerContainerFactory 提供程序方法:

所以最终的工作代码是:

JMS配置文件:

@Configuration
@EnableJms
public class FooJmsConfig {

@Bean
public ActiveMQConnectionFactory connectionFactory() {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL(BROKER_URL);
connectionFactory.setPassword(BROKER_USERNAME);
connectionFactory.setUserName(BROKER_PASSWORD);
connectionFactory.setUseCompression(true);

connectionFactory.setConnectionIDPrefix("DRR");
connectionFactory.setUseAsyncSend(true);
return connectionFactory;
}

@Bean(name= "foo1")
public DefaultJmsListenerContainerFactory foo1() {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
factory.setConcurrency("1-1");
factory.setPubSubDomain(true);
factory.setSubscriptionDurable(true);

connectionFactory.setClientID("FOO_1");
return factory;
}

@Bean(name= "foo2")
public DefaultJmsListenerContainerFactory foo2() {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
factory.setConcurrency("1-1");
factory.setPubSubDomain(true);
factory.setSubscriptionDurable(true);

connectionFactory.setClientID("FOO_1");
return factory;
}

}

jms 监听器将是

@JmsListener(destination = "foo1", containerFactory="foo1")
public void foo1(final Message jsonMessage) throws JMSException {
...
}

@JmsListener(destination = "foo2", containerFactory="foo2")
public void foo12(final Message jsonMessage) throws JMSException {
...
}

关于java - 如何为多个主题配置多个JmsListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48600321/

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