美文网首页Javaspring boot
如何使用SpringBoot整合ActiveMQ?我教你

如何使用SpringBoot整合ActiveMQ?我教你

作者: 程序花生 | 来源:发表于2020-10-12 15:23 被阅读0次

一、ActiveMQ 介绍

MQ是消息中间件,是一种在分布式系统中应用程序借以传递消息的媒介,常用的有 ActiveMQ,RabbitMQ,kafka。ActiveMQ 是 Apache下的开源项目,完全支持JMS1.1和J2EE1.4规范的JMS Provider实现。

特点:

(1)支持多种语言编写客户端

(2)对spring的支持,很容易和spring整合

(3)支持多种传输协议:TCP,SSL,NIO,UDP等

(4)支持AJAX

消息形式:

(1)点对点(queue)

(2)一对多(topic)

二、ActiveMQ 安装

首先下载需要的 ActiveMQ:https://activemq.apache.org/activemq-5120-release

然后上传到 Linux 中解压:这里需要确保 linux 中已经装好了 jdk

由于我这里的 jdk 是1.7 的版本所以我下载的 activemq 是5.12.0

解压:tar -zxvf apache-activemq-5.12.0-bin.tar.gz

然后进入解压的目录启动即可:./bin/activemq start

登录验证,注意需要登录:192.168.245.129:8161/admin,用户名和密码均为:admin

不然直接访问会出错:http://192.168.245.129:8161/

三、SpringBoot 整合 ActiveMQ

1. 加入依赖配置

2. 配置 application.properties

# 8161 是管理界面的端口,61616 是通信的端口

spring.activemq.broker-url=tcp://192.168.245.129:61616

#发送的消息可以是个普通的字符串也可以是个对象,如果是对象则需要设置trust-all 为true

spring.activemq.packages.trust-all=true

spring.activemq.user=admin

spring.activemq.password=admin

3. 配置队列

这里由于就一个 Bean,所以在 启动类中对其进行了配置:

package org.yolo.activemq;

import org.apache.activemq.command.ActiveMQQueue;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.Bean;

import javax.jms.Queue;

@SpringBootApplication

public class ActivemqApplication {

    public static void main(String[] args) {

        SpringApplication.run(ActivemqApplication.class, args);

    }

    @Bean

    Queue queue() {

        return new ActiveMQQueue("hello.Yolo");

    }

}

4. 配置 Message 对象

public class Message implements Serializable {

    private String content;

    private Date sendDate;

    public String getContent() {

        return content;

    }

    @Override

    public String toString() {

        return "Message{" +

                "content='" + content + '\'' +

                ", sendDate=" + sendDate +

                '}';

    }

    public void setContent(String content) {

        this.content = content;

    }

    public Date getSendDate() {

        return sendDate;

    }

    public void setSendDate(Date sendDate) {

        this.sendDate = sendDate;

    }

}

5. 配置 JmsComponent

实现消息的接收和发送

package org.yolo.activemq;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jms.annotation.JmsListener;

import org.springframework.jms.core.JmsMessagingTemplate;

import org.springframework.stereotype.Component;

import javax.jms.Queue;

@Component

public class JmsComponent {

    //引入自动化配置之后,定义好的一个JMS发送消息的模板

    @Autowired

    JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired

    Queue queue;

    /**

    * 发送消息

    * @param message

    */

    public void send(Message message) {

        jmsMessagingTemplate.convertAndSend(this.queue, message);

    }

    /**

    * 消费者,接收消息

    * @param msg

    */

    @JmsListener(destination = "hello.Yolo")//destination 表示刚才定义的消息队列的名字

    public void receive(Message msg) {

        System.out.println(msg);

    }

}

6. 测试

@SpringBootTest

class ActivemqApplicationTests {

    @Autowired

    JmsComponent jmsComponent;

    @Test

    public void contextLoads() {

        Message message = new Message();

        message.setContent("hello Yolo!");

        message.setSendDate(new Date());

        jmsComponent.send(message);

    }

}

相关文章

网友评论

    本文标题:如何使用SpringBoot整合ActiveMQ?我教你

    本文链接:https://www.haomeiwen.com/subject/irzupktx.html