我有两个接收者,应该从一个主题中阅读。但是只有一个随机接收者会收到消息,就像它实际上是从队列中读取而不是主题一样。我已经读过这个答案,但是似乎没有用。这是我的代码。
Application.java:
package hello; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.jms.annotation.EnableJms; import org.springframework.jms.config.DefaultJmsListenerContainerFactory; import org.springframework.jms.config.JmsListenerContainerFactory; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.support.converter.MappingJackson2MessageConverter; import org.springframework.jms.support.converter.MessageConverter; import org.springframework.jms.support.converter.MessageType; import javax.jms.ConnectionFactory; @SpringBootApplication @EnableJms public class Application { public static final String MAILBOX_TOPIC = "inbox.topic"; public static void main(String[] args) { // Launch the application ConfigurableApplicationContext context = SpringApplication.run(Application.class, args); JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); // Send a message with a POJO - the template reuse the message converter System.out.println("Sending an email message."); jmsTemplate.convertAndSend(MAILBOX_TOPIC, new Email("info@example.com", "Hello")); } @Bean public JmsListenerContainerFactory> topicListenerFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); factory.setPubSubDomain(true); configurer.configure(factory, connectionFactory); return factory; } @Bean // Serialize message content to json using TextMessage public MessageConverter jacksonJmsMessageConverter() { MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); converter.setTargetType(MessageType.TEXT); converter.setTypeIdPropertyName("_type"); return converter; } }
FirstReceiver.java:
package hello; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; import static hello.Application.MAILBOX_TOPIC; @Component public class FirstReceiver { @JmsListener(destination = MAILBOX_TOPIC, containerFactory = "topicListenerFactory") public void receiveMessage(Email email) { System.out.println("FirstReceiver <" + email + ">"); } }
SecondReceiver.java:
package hello; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; import static hello.Application.MAILBOX_TOPIC; @Component public class SecondReceiver { @JmsListener(destination = MAILBOX_TOPIC, containerFactory = "topicListenerFactory") public void receiveMessage(Email email) { System.out.println("SecondReceiver <" + email + ">"); } }
Email.java:
package hello; public class Email { private String to; private String body; public Email() { } public Email(String to, String body) { this.to = to; this.body = body; } public String getTo() { return to; } public void setTo(String to) { this.to = to; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } @Override public String toString() { return String.format("Email{to=%s, body=%s}", getTo(), getBody()); } }
pom.xml:
4.0.0 org.springframework gs-messaging-jms 0.1.0 org.springframework.boot spring-boot-starter-parent 1.4.3.RELEASE 1.8 org.springframework.boot spring-boot-starter-activemq org.apache.activemq activemq-broker com.fasterxml.jackson.core jackson-databind org.springframework.boot spring-boot-maven-plugin
请帮我
添加spring.jms.pub-sub-domain=true
到application.properties
更新:
您需要更改2件事:
1。
@Bean public JmsListenerContainerFactory> topicListenerFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); // the configurer will use PubSubDomain from application.properties if defined or false if not //so setting it on the factory level need to be set after this configurer.configure(factory, connectionFactory); factory.setPubSubDomain(true); return factory; }
2。
jmsTemplate.convertAndSend( new ActiveMQTopic(MAILBOX_TOPIC), new Email("info@example.com", "Hello"));
或注入这个豆