Rap*_*ale 2 java spring jms spring-jms
我正在为一个应用程序制作原型,以测试将来的应用程序的Spring消息传递功能。
我知道我们需要做的一件事是在同一应用程序中处理来自activemq的主题和队列。因此,在同一个bean中,我应该有一个@JmsListener注释的方法,该方法将听到一个队列,而另一个将听到一个主题。那可能吗?
什么是更简单的方法?我看到一些答案使用带有弹簧JMS主题像这一个,但在这种情况下我想我需要创建两个使用DefaultMessageListenerContainer,一个是话题,另一个用于队列。这是最好的解决方案?
有没有针对此问题的注释方法?
这是一个完整的示例,说明如何使用Spring Boot为主题设置第二个容器工厂:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2508 次 |
| 最近记录: |