【发布时间】:2023-03-13 00:24:01
【问题描述】:
我找到了许多代码示例来创建 JMS 队列项目,但没有找到名为 PUB-SUB(Publisher-Subscriber) 的 activemq(topic) 的代码示例。
对于 Spring,我在下面找到了代码,但正在寻找 Spring Boot 完整代码。
Topic topic = topicConsumerSession.createTopic("customerTopic");
// Consumer1 subscribes to customerTopic
MessageConsumer consumer1 = topicConsumerSession.createSubscriber(topic);
consumer1.setMessageListener(new ConsumerMessageListener(
"Consumer1"));
// Consumer2 subscribes to customerTopic
MessageConsumer consumer2 = topicConsumerSession.createSubscriber(topic);
consumer2.setMessageListener(new ConsumerMessageListener(
"Consumer2"));
首先非常感谢@Gary Russell。
这是我从@Gary Russell 建议中实现的。有什么好的做法可以分离关注点和更具可扩展性的方式。
应用程序.properties
spring.jms.pub-sub-domain=true
#spring.jms.template.default-destination=testTopic
Publisher.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
@Component
public class Publisher implements CommandLineRunner{
@Autowired
private JmsTemplate jmsTemplate ;
@Autowired
private Topic topic1;
@Autowired
private Topic topic2;
@Override
public void run(String... arg0) throws Exception {
// TODO Auto-generated method stub
Thread.sleep(5000); // wait for subscriptions, unless they are durable
this.jmsTemplate.convertAndSend(this.topic1,"-----> 1st message from publisher -- topic 1");
Thread.sleep(5000);
this.jmsTemplate.convertAndSend(this.topic1,"-----> 2nd message from publisher -- topic 1");
/**
* for topic2
*/
// TODO Auto-generated method stub
Thread.sleep(5000); // wait for subscriptions, unless they are durable
this.jmsTemplate.convertAndSend(this.topic2,"-----> 1st message from publisher -- topic 2");
Thread.sleep(5000);
this.jmsTemplate.convertAndSend(this.topic2,"-----> 2nd message from publisher -- topic 2");
}
}
订阅者.java
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class Subscriber {
@JmsListener(destination = "Topic1")
public void listener1(String in) {
System.out.println("Listener1: " + in);
}
@JmsListener(destination = "Topic1,Topic2")
public void listener2(String in) {
System.out.println("Listener2: " + in);
}
@JmsListener(destination = "Topic2")
public void listener3(String in) {
System.out.println("Listener3: " + in+"\n listener 3 is just ");
}
}
主类:springBootApplication
PubSubJmsBootApplication.java
import javax.jms.Topic;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
@SpringBootApplication
@EnableJms
public class PubSubJmsBootApplication {
@Bean
public Topic topic1() {
return new ActiveMQTopic("Topic1");
}
@Bean
public Topic topic2() {
return new ActiveMQTopic("Topic2");
}
public static void main(String[] args) {
SpringApplication.run(PubSubJmsBootApplication.class, args);
}
}
【问题讨论】:
标签: spring-boot jms activemq spring-jms jms-topic