栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用Spring Boot JMS收听主题

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用Spring Boot JMS收听主题

我只是从以下网址获取了完整的Spring引导示例: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@EnableJmspublic 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());}@Beanpublic 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;}@Beanpublic 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 TextMessagepublic 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("info@example.com", "Hello"));    jmsTemplate.convertAndSend("mailbox.queue", new Email("info@example.com", "Hello"));    }}

听众

package org.springboot.jms;import org.springframework.jms.annotation.JmsListener;import org.springframework.stereotype.Component;@Componentpublic 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提供一个bean映射,当您根据队列或主题需要它们时可以使用每种bean。模板和bean可以用您选择的本SO问题中讨论的方法实例化。希望能帮助到你



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/421777.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号