使用主题和队列处理@JmsListener 的更简单方法
Simpler way to handle @JmsListener with topics and queues
我正在设计一个应用程序原型,以测试 spring 未来应用程序的消息传递功能。
我知道我们需要做的一件事是在同一应用程序中处理来自 activemq 的主题和队列。因此,在同一个 bean 中,我应该有一个由 @JmsListener 注释的方法,它会听到一个队列,另一个方法会听到一个主题。那可能吗?
更简单的方法是什么?我看到了一些将主题与 spring-jms 一起使用的答案,例如 ,但在那种情况下,我想我将需要创建两个 DefaultMessageListenerContainer,一个用于主题,另一个用于队列。这是最好的解决方案?
这个问题有注释的方法吗?
框架将负责为每个 @JmsListener
创建一个容器;你只需要通过 containerFactory
属性.
告诉它使用哪个容器工厂
这是一个完整的示例,说明如何使用 spring 启动为主题设置第二个容器工厂:
JmsDemoApplication.java:
package net.lenthe;
import javax.jms.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
@SpringBootApplication
public class JmsDemoApplication {
@Autowired
private ConnectionFactory connectionFactory;
@Bean(name = "topicJmsListenerContainerFactory")
public DefaultJmsListenerContainerFactory getTopicFactory() {
DefaultJmsListenerContainerFactory f = new DefaultJmsListenerContainerFactory();
f.setConnectionFactory(connectionFactory);
f.setSessionTransacted(true);
f.setPubSubDomain(true);
return f;
}
public static void main(String[] args) {
SpringApplication.run(JmsDemoApplication.class, args);
}
}
MessageListenerBean.java:
package net.lenthe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class MessageListenerBean {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@JmsListener(destination = "myMessageTopic", containerFactory = "topicJmsListenerContainerFactory")
public void processTopicMessage(String content) {
logger.info("Received topic message. Content is " + content);
}
@JmsListener(destination = "myMessageQueue")
public void processQueueMessage(String content) {
logger.info("Received queue message. Content is " + content);
}
}
我正在设计一个应用程序原型,以测试 spring 未来应用程序的消息传递功能。
我知道我们需要做的一件事是在同一应用程序中处理来自 activemq 的主题和队列。因此,在同一个 bean 中,我应该有一个由 @JmsListener 注释的方法,它会听到一个队列,另一个方法会听到一个主题。那可能吗?
更简单的方法是什么?我看到了一些将主题与 spring-jms 一起使用的答案,例如
这个问题有注释的方法吗?
框架将负责为每个 @JmsListener
创建一个容器;你只需要通过 containerFactory
属性.
这是一个完整的示例,说明如何使用 spring 启动为主题设置第二个容器工厂:
JmsDemoApplication.java:
package net.lenthe;
import javax.jms.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
@SpringBootApplication
public class JmsDemoApplication {
@Autowired
private ConnectionFactory connectionFactory;
@Bean(name = "topicJmsListenerContainerFactory")
public DefaultJmsListenerContainerFactory getTopicFactory() {
DefaultJmsListenerContainerFactory f = new DefaultJmsListenerContainerFactory();
f.setConnectionFactory(connectionFactory);
f.setSessionTransacted(true);
f.setPubSubDomain(true);
return f;
}
public static void main(String[] args) {
SpringApplication.run(JmsDemoApplication.class, args);
}
}
MessageListenerBean.java:
package net.lenthe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class MessageListenerBean {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@JmsListener(destination = "myMessageTopic", containerFactory = "topicJmsListenerContainerFactory")
public void processTopicMessage(String content) {
logger.info("Received topic message. Content is " + content);
}
@JmsListener(destination = "myMessageQueue")
public void processQueueMessage(String content) {
logger.info("Received queue message. Content is " + content);
}
}