我有两个不同的发送者和收件人应用程序。
发件人:
@SpringBootApplication
public class RabbitJmsApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(RabbitJmsApplication.class, args);
}
@Autowired
private JmsTemplate template;
@Autowired
private JmsTemplate topicTemplate;
@Override
public void run(String... arg0) throws Exception {
for (int i = 0; i < 10; i++) {
template.convertAndSend("my_queue", "msg_" + i);
Thread.sleep(100);
}
for (int i = 0; i < 10; i++) {
topicTemplate.convertAndSend("my_topic", "topic_msg_" + i);
Thread.sleep(100);
}
}
@Bean
public RMQConnectionFactory connectionFactory() {
return new RMQConnectionFactory();
}
@Bean
public JmsTemplate template() {
return new JmsTemplate(connectionFactory());
}
@Bean
public JmsTemplate topicTemplate() {
final JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory());
jmsTemplate.setPubSubDomain(true);
return jmsTemplate;
}
}
和接收方:
@Component
public class Listener {
@JmsListener(destination = "my_queue")
public void receive(String str){
System.out.println(str);
}
@JmsListener(destination = "my_topic")
public void receiveTopic(String str){
System.out.println(str);
}
}
我看到了
msg_1
msg_2
...
在接收器上,但我没有看到主题消息。
我做错了什么?
管理控制台:
答案 0 :(得分:0)
默认情况下,主题的订阅不会持久 - 您可能在收听者开始之前发送邮件。
尝试在向主题发送消息之前添加Thread.sleep()
。
答案 1 :(得分:0)
我的接收器在将以下bean添加到上下文后变为接收mesagges:
@Bean
public JmsListenerContainerFactory<?> myFactory(DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
// This provides all boot's default to this factory, including the message converter
configurer.configure(factory, connectionFactory());
// You could still override some of Boot's default if necessary.
factory.setPubSubDomain(true);
return factory;
}