【发布时间】:2019-09-04 10:14:38
【问题描述】:
我已经参考了SpringBoot application publish and read from ActiveMQ topic,使用 ActiveMQ 发布了一个主题。我创建了两个从主题读取消息的接收者微服务。我还创建了休息端点来发布主题。但是,我必须执行此休息端点两次才能为两个接收者发布消息
1) . REST 端点的第一次执行将向 Receiver1 发送消息
2)。 rest 端点的第二次执行将向 Receiver2 发送消息
因此 2 个接收者无法同时读取主题。
这是我的代码。
PublisherApplication.java
package com.springboot;
//import statements
@SpringBootApplication
@EnableDiscoveryClient
@EnableJms
public class PublisherApplication {
@Bean
public JmsListenerContainerFactory<?> topicListenerFactory(ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
// This provides all boot's default to this factory, including the message converter
configurer.configure(factory, connectionFactory);
//setPubSubDomain identifies Topic in ActiveMQ
factory.setPubSubDomain(true);
return factory;
}
public static void main(String[] args) {
SpringApplication.run(PublisherApplication.class, args);
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
}
PublishMessage.java
[发布Topic的休息点]
package com.springboot.controller;
//import statements
@RestController
@RequestMapping(path = "/schoolDashboard/topic")
class PublishMessage {
public static final String MAILBOX_TOPIC = "mailbox.topic";
@Autowired
private JmsTemplate jmsTemplate;
@GetMapping(path = "/sendEmail")
public void sendStudentById() throws Exception{
System.out.println("Anindya-TopicSendMessage.java :: Publishing Email sent....");
jmsTemplate.convertAndSend(MAILBOX_TOPIC, "Topic - Email Sent");
}
}
ReceiverApplication01
[注意 - Receiver01 是第一个微服务]
package com.springboot;
//import statements
@SpringBootApplication
@EnableJms
public class ReceiverApplication01 {
@Bean
public JmsListenerContainerFactory<?> topicListenerFactory(ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
// This provides all boot's default to this factory, including the message converter
configurer.configure(factory, connectionFactory);
//setPubSubDomain identifies Topic in ActiveMQ
factory.setPubSubDomain(true);
return factory;
}
public static void main(String[] args) {
SpringApplication.run(ReceiverApplication01.class, args);
}
}
TopicMesssgeReceiver01.java
[Receiver01 从主题中读取消息]
package com.springboot.message;
//import statement
@Component
public class TopicMesssgeReceiver01 {
private final SimpleMessageConverter converter = new SimpleMessageConverter();
public static final String MAILBOX_TOPIC = "mailbox.topic";
@JmsListener(destination = MAILBOX_TOPIC, containerFactory = "topicListenerFactory")
public void receiveMessage(final Message message) throws JMSException{
System.out.println("Receiver01 <" + String.valueOf(this.converter.fromMessage(message)) + ">");
}
}
ReceiverApplication02
[注意:-Receiver02 是第二个微服务]
package com.springboot;
//import statement
@SpringBootApplication
@EnableJms
public class ReaderApplication02 {
@Bean
public JmsListenerContainerFactory<?> topicListenerFactory(ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setPubSubDomain(true);
configurer.configure(factory, connectionFactory);
return factory;
}
public static void main(String[] args) {
SpringApplication.run(ReaderApplication02.class, args);
}
}
TopicMesssgeReceiver02
[Receiver02 从主题中读取消息]
package com.springboot.message;
//import statement
@Component
public class TopicMesssgeReceiver02 {
private final SimpleMessageConverter converter = new SimpleMessageConverter();
public static final String MAILBOX_TOPIC = "mailbox.topic";
@JmsListener(destination = MAILBOX_TOPIC, containerFactory = "topicListenerFactory")
public void receiveMessage(final Message message) throws Exception{
System.out.println("Receiver02 <" + String.valueOf(this.converter.fromMessage(message)) + ">");
}
}
【问题讨论】:
-
欢迎来到 StackOverflow。我认为值得看看here 讨论的类似问题
标签: spring-boot activemq