程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用Spring Boot JMS收听主题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何使用Spring Boot JMS收听主题?

开发过程中遇到如何使用Spring Boot JMS收听主题的问题如何解决?下面主要结合日常开发的经验,给出你关于如何使用Spring Boot JMS收听主题的解决方法建议,希望对你解决如何使用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
@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("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;

/**
 * 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提供一个bean映射,当您根据队列或主题需要它们时可以使用每种bean。模板和bean可以用您选择的本SO问题中讨论的方法实例化。希望能帮助到你

解决方法

我正在尝试使用以下代码段收听主题。但是,默认情况下,其监听队列。在这种情况下,没有xml配置。我完全依靠注释。此外,我完全依靠Spring
Boot提供的AutoConfiguration。我不确定如何在JmsListener中将目标类型设置为主题。SpringJMS专家请帮忙。

    @Component
    public class myTopicListener {

        @JmsListener(desTination = "${trans.alert.topic}")
        public void receivemessage(transactionAlert alert) {
            logger.info("AlertSubscriberEmail :: Sending Email => <" + alert + ">");
        }
    }

大佬总结

以上是大佬教程为你收集整理的如何使用Spring Boot JMS收听主题全部内容,希望文章能够帮你解决如何使用Spring Boot JMS收听主题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。