Spring Boot的JMS发送和接收队列消息,基于ActiveMQ

x33g5p2x  于2022-02-07 转载在 Spring  
字(3.7k)|赞(0)|评价(0)|浏览(312)

消息队列ActiveMQ是JMS的一种实现,ActiveMQ可以单独作为一个消息队列组件即插即拔,即插即用,参考下面两篇文章:

分布式MQTT消息订阅-发布框架:高可用性ActiveMQ_Zhang Phil-CSDN博客_mqtt分布式架构分布式MQTT消息订阅-发布框架:高可用性ActiveMQActiveMQ是MQTT的一种实现。ActiveMQ基于JMS。ActiveMQ开发包下载地址:http://activemq.apache.org/download.htmlActiveMQ最适合做消息推送。国内很多厂商基于ActiveMQ改造出多种消息推送平台。下载完成ActiveMQ压缩包后解压,解压后直接在Java环境中运...

https://zhangphil.blog.csdn.net/article/details/94759508

ActiveMQ安装配置和使用简例_Zhang Phil-CSDN博客本文作者:Zhang Phil原文链接:ActiveMQ安装配置和使用简例ActiveMQ是一套JMS(Java Message Service)开源消息服务实现的组件。以Windows操作系统为例,本文简述了ActiveMQ的安装配置和使用简例。消息服务是互联网应用的最基本功能,只要是互联网应用,就少不了消息通信。而ActiveMQ是一个相对比较成熟稳定的JMS消息服务组件,

https://blog.csdn.net/zhangphil/article/details/48173665

以上是把ActiveMQ作为单独的组件(系统子模块)拆分出来加载使用。

在Spring boot里面,ActiveMQ作为JMS的实现,Spring boot内嵌、可直接使用。

定义一个POJO的消息类:

public class SimpleMsg {
    private String to;
    private String body;

    public SimpleMsg() {

    }

    public SimpleMsg(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 "SimpleMsg{" +
                "to='" + to + '\'' +
                ", body='" + body + '\'' +
                '}';
    }
}

SimpleMsg将作为spring boot程序中消息的实体传输。

定义一个spring boot的消息接收器:

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class MsgReceiver {
    //destination的值和在application里面发送制定的目的地值需要相同才能收到
    //containerFactory的变量名对应在app里面定义的JmsListenerContainerFactory
    @JmsListener(destination = "@zhangphil", containerFactory = "myAppContainerFactory")
    public void receiveMessage(SimpleMsg msg) {
        System.out.println("收到消息->");
        System.out.println(msg);
        System.out.println("收到消息<-");
    }
}

完成spring boot的应用application:

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 javax.jms.ConnectionFactory;

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;

@SpringBootApplication
@EnableJms
public class SpringJmsApplication {
    @Bean
    public JmsListenerContainerFactory<?> myAppContainerFactory(ConnectionFactory connectionFactory,
                                                             DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }

    @Bean
    public MessageConverter jmsMessageConverter() {
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setTargetType(MessageType.TEXT);
        converter.setTypeIdPropertyName("_type");
        return converter;
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringJmsApplication.class, args);

        JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);

        SimpleMsg msg = new SimpleMsg("zhangfly", "Hello,world!");
        System.out.println("发送消息:" + msg);
        jmsTemplate.convertAndSend("@zhangphil", msg);
        System.out.println("发送结束");
    }
}

以上完成后,运行SpringJmsApplication,如果成功,控制台输出:

发送消息:SimpleMsg{to='zhangfly', body='Hello,world!'}
发送结束
收到消息->
SimpleMsg{to='zhangfly', body='Hello,world!'}
收到消息<-

相关文章

微信公众号

最新文章

更多