详解Springboot整合ActiveMQ(Queue和Topic两种模式)

网友投稿 910 2022-12-09


详解Springboot整合ActiveMQ(Queue和Topic两种模式)

写在前面: 从2018年底开始学习SpringBoot,也用SpringBoot写过一些项目。这里对学习Springboot的一些知识总结记录一下。如果你也在学习SpringBoot,可以关注我,一起学习,一起进步。

ActiveMQ简介

1、ActiveMQ简介

Apache ActiveMQ是Apache软件基金会所研发的开放源代码消息中间件;由于ActiveMQ是一个纯java程序,因此只需要操作系统支持Java虚拟机,ActiveMQ便可执行。

2、ActiveMQ下载

下载地址:http://activemq.apache.org/components/classic/download/

下载完成后解压双击activemq.bat文件打开(不用安装,直接使用),目录和打开后效果如下:

运行后,浏览器访问http://localhost:8161/地址进入一下界面。

点击Manage ActiveMQ broker登录到ActiveMQ管理页面,默认账号和密码都是admin。管理页面如下:

SpringBoot整合ActiveMQ

1、新建SpringBoot项目

新建Springboot项目,添加对应的依赖。项目完整的pom.xml文件如下:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.2.5.RELEASE

com.mcy

springboot-mq

0.0.1-SNAPSHOT

springboot-mq

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-activemq

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.2.5.RELEASE

com.mcy

springboot-mq

0.0.1-SNAPSHOT

springboot-mq

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-activemq

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.boot

spring-boot-maven-plugin

2、项目结构

3、相关配置信息

在application.properties类中添加ActiveMQ相关的配置信息

server.port=8080

server.servlet.context-path=/mq

#MQ服务器地址

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

#用户名

spring.activemq.user=admin

#密码

spring.activemq.password=admin

#设置是Queue队列还是Topic,false为Queue,true为Topic,默认false-Queue

spring.jms.pub-sub-domain=false

#spring.jms.pub-sub-domain=true

#变量,定义队列和topic的名称

myqueue: activemq-queue

mytopic: activemq-topic

4、ActiveMQ配置类

ActiveMQ配置类ConfigBean,配置了Queue队列和topic两种模式,代码如下:

import org.apache.activemq.command.ActiveMQQueue;

import org.apache.activemq.command.ActiveMQTopic;

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

import org.springframework.context.annotation.Bean;

import org.springframework.jms.annotation.EnableJms;

import org.springframework.stereotype.Component;

import javax.jms.Topic;

/**

1. MQ配置类

*/

@Component

@EnableJms

public class ConfigBean {

@Value("${myqueue}")

private String myQueue;

@Value("${mytopic}")

private String topicName;

//队列

@Bean

public ActiveMQQueue queue(){

return new ActiveMQQueue(myQueue);

}

//topic

@Bean

public Topic topic(){

return new ActiveMQTopic(topicName);

}

}

Queue队列模式

队列模式即点对点传输。

点对点消息传递域的特点如下:

每个消息只能有一个消费者,类似于1对1的关系。好比个人快递自己领自己的。

消息的生产者和消费者之间没有时间上的相关性。无论消费者在生产者发送消息的时候是否处于运行状态,消费者都可以提取消息。好比我们的发送短信,发送者发送后不见得接收者会即收即看。

消息被消费后队列中不会再存储,所以消费者不会消费到已经被消费掉的消息。

1、队列生产者

QueueProducerController类为队列生产者控制器,主要向消息队列中发送消息。代码如下:

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

import org.springframework.jms.core.JmsMessagingTemplate;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.jms.Queue;

/*

* 队列消息生产者

*/

@RestController

public class QueueProducerController {

@Autowired

private JmsMessagingTemplate jmsMessagingTemplate;

@Autowired

private Queue queue;

/*

* 消息生产者

*/

@RequestMapping("/sendmsg")

public void sendmsg(String msg) {

System.out.println("发送消息到队列:" + msg);

// 指定消息发送的目的地及内容

this.jmsMessagingTemplate.convertAndSend(this.queue, msg);

}

}

2、队列消费者

QueueConsumerController类为队列消费者控制器,具体代码如下:

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

import org.springframework.jms.annotation.JmsListener;

import org.springframework.web.bind.annotation.RestController;

/*

1. 队列queue消费者控制器

*/

@RestController

public class QueueConsumerController {

/*

* 消费者接收消息

*/

@JmsListener(destination="${myqueue}")

public void readActiveQueue(String message) {

System.out.println("接受到:" + message);

}

}

3、测试效果

运行项目在浏览器中访问http://localhost:8080/mq/sendmsg?msg=123。向消息队列中发送123。控制台输出效果:

ActiveMQ控制台显示:

Number Of Pending Messages:消息队列中待处理的消息

Number Of Consumers:消费者的数量

Messages Enqueued:累计进入过消息队列的总量

Messages Dequeued:累计消费过的消息总量

【注】队列模式时,配置文件application.properties中spring.jms.pub-sub-domain属性必须设置为false。

Topic模式

topic模式基于发布/订阅模式的传输。

基于发布/订阅模式的传输的特点如下:

生产者将消息发布到topic中,每个消息可以有多个消费者,属于1:N的关系;

生产者和消费者之间有时间上的相关性。订阅某一个主题的消费者只能消费自它订阅之后发布的消息。

生产者生产时,topic不保存消息它是无状态的不落地,假如无人订阅就去生产,那就是一条废消息。

1、topic生产者

TopicProducerController类为topic生产者控制器,主要向消息队列中发送消息。代码如下:

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

import org.springframework.jms.core.JmsMessagingTemplate;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.jms.Queue;

import javax.jms.Topic;

/*

* topic消息生产者

*/

@RestController

public class TopicProducerController {

@Autowired

private JmsMessagingTemplate jmsMessagingTemplate;

@Autowired

private Topic topic;

/*

* 消息生产者

*/

@RequestMapping("/topicsendmsg")

public void sendmsg(String msg) {

System.out.println("发送消息到MQ:" + msg);

// 指定消息发送的目的地及内容

this.jmsMessagingTemplate.convertAndSend(this.topic, msg);

}

}

2、topic消费者

TopicConsumerController类为topic消费者控制器,其中写了两个消费者方法,可以理解为有lpTHrckow两个用户订阅。具体代码如下:

import org.springframework.jms.annotation.JmsListener;

import org.springframework.web.bind.annotation.RestController;

/*

1. topic消费者控制器

*/

@RestController

public class TopicConsumerController {

/*

* 消费者接收消息

*/

@JmsListener(destination="${mytopic}")

public void readActiveQueue(String message) {

System.out.println("接受到:" + message);

}

@JmsListener(destination="${mytopic}")

public void readActiveQueue1(String message) {

System.out.println("接受到:" + message);

}

}

3、测试效果

运行项目在浏览器中访问http://localhost:8080/mq/topicsendmsg?msg=123。向消息队列中发送123。控制台输出效果(有两个消费者方法):

ActiveMQ控制台显示:

Number Of Consumers:消费者的数量

Messages Enqueued:累计进入过消息队列的总量

Messages Dequeued:累计消费过的消息总量

【注】Topic模式时,配置文件application.properties中spring.jms.pub-sub-domain属性必须设置为true。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java中functional interface的分类和使用详解
下一篇:java实现响应重定向发送post请求操作示例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~