我只是把整個春季啓動例如來自:https://github.com/spring-guides/gs-messaging-jms/
在這個它是從一個隊列中的消息的發送和接收產生。要將其更改爲主題,您必須在Factory實例中設置Pub-Sub屬性。
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
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.connection.CachingConnectionFactory;
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 JmsSampleApplication {
public void registerBeans(ConfigurableApplicationContext context){
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(JmsTemplate.class);
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
builder.addPropertyValue("connectionFactory", cachingConnectionFactory); // set property value
DefaultListableBeanFactory factory = (DefaultListableBeanFactory) context.getAutowireCapableBeanFactory();
factory.registerBeanDefinition("jmsTemplateName", builder.getBeanDefinition());
}
@Bean
public JmsListenerContainerFactory<?> topicListenerFactory(ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setPubSubDomain(true);
// 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.
return factory;
}
@Bean
public JmsListenerContainerFactory<?> queueListenerFactory(ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
//factory.setPubSubDomain(true);
// This provides all boot's default to this factory, including the message converter
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;
}
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(JmsSampleApplication.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("[email protected]", "Hello"));
jmsTemplate.convertAndSend("mailbox.queue", new Email("[email protected]", "Hello"));
}
}
聽者
package org.springboot.jms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/**
* Created by RGOVIND on 10/20/2016.
*/
@Component
public class HelloTopicListener {
@JmsListener(destination = "mailbox.topic", containerFactory = "topicListenerFactory")
public void receiveTopicMessage(Email email) {
System.out.println("Received <" + email + ">");
}
@JmsListener(destination = "mailbox.queue", containerFactory = "queueListenerFactory")
public void receiveQueueMessage(Email email) {
System.out.println("Received <" + email + ">");
}
}
一旦做到這一點,你都設置訂閱選擇的話題。
有多種方法這門課程的,你可以有地圖豆不同jmsTemplates,每一個都可以在需要時根據隊列或主題來使用的。可以使用您在SO Question中討論的選擇喜歡的方法實例化模板&。希望它可以幫助
當我嘗試用** JmsListenerContainerFactory豆**或設置該屬性**春天。 jms.pub-sub-domain = true **在application.properties中,隊列發送者和偵聽者的所有其他代碼都起作用,就好像他們正在讀寫主題而不是隊列一樣。有沒有解決方案可以使隊列和主題的偵聽器共存。 – zikzakjack
你總是可以選擇擁有多個工廠。在代碼的情況下,您可以選擇同時擁有隊列和主題。問題是當你選擇接收時,你將不得不選擇工廠。在這種情況下,你可以有2個工廠,從一個主題中獲得並從隊列中獲得。編輯我的答案以及 –
我試過使用多個工廠,即使那麼這樣創建的主題就像一個隊列只有即創建多個偵聽器消息分佈在自己之間。相反,每個用戶應該得到所有的消息 – anon