SpringBoot整合activemq的案例代码
作者:#Hideonbush 时间:2023-11-06 18:41:37
ActiveMQ是什么
ActiveMQ是消息队列技术,为解决高并发问题而生
ActiveMQ生产者消费者模型(生产者和消费者可以跨平台、跨系统)
ActiveMQ支持如下两种消息传输方式
点对点模式,生产者生产了一个消息,只能由一个消费者进行消费
发布/订阅模式,生产者生产了一个消息,可以由多个消费者进行消费
1.安装activemq(linux)
1.下载压缩包,地址链接: https://activemq.apache.org/components/classic/download/.
2.利用xshell上传压缩包到linux上,并且解压
3.进入到bin目录启动,通过./activemq start命令启动activemq
4.在宿主主机范文虚拟机ip地址加上端口号8161,会出现登录弹框叫我们输入用户名和密码,用户名和密码都是admin。如下界面即代表active安装成功
5.要注意的坑!
1)linux主机和宿主主机的防火墙都要关闭
2)linux主机和宿主主机一定要互ping得通
3)如果linux主机和宿主主机互ping得通,但是却访问不了activemq的端口,那就参考这篇文章,把conf目录下的jetty.xml文件中的地址为linux主机的ip地址,就可以访问了。文章链接: https://www.aspxhome.com/LINUXjishu/810589.html.
4)activemq还依赖java环境,所以linux上也要安装java环境
2.SpringBoot整合activemq案例
2.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yl</groupId>
<artifactId>jms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jms</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<artifactId>spring-boot-starter-web</artifactId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2 application.properties
# activemq的配置
# 地址
spring.activemq.broker-url=tcp://192.168.244.135:61616
# 信任所有的包
spring.activemq.packages.trust-all=true
# 用户名
spring.activemq.user=admin
# 密码
spring.activemq.password=admin
2.3 消息实体
package com.yl.jms.model;
import java.io.Serializable;
import java.util.Date;
public class Message implements Serializable {
private String content;
private Date date;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
public Date getDate() {
return date;
public void setDate(Date date) {
this.date = date;
@Override
public String toString() {
return "Message{" +
"content='" + content + '\'' +
", date=" + date +
'}';
}
2.4 主程序
package com.yl.jms;
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 JmsApplication {
public static void main(String[] args) {
SpringApplication.run(JmsApplication.class, args);
}
@Bean
Queue queue() {
return new ActiveMQQueue("yl-queue");
}
2.5 定义消息的发送和接收方法
package com.yl.jms.config;
import com.yl.jms.model.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.EnableJms;
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 {
//消息发送模板
@Autowired
JmsMessagingTemplate jmsMessagingTemplate;
Queue queue;
//发送消息
public void send(Message message) {
jmsMessagingTemplate.convertAndSend(queue,message);
}
//接收消息
@JmsListener(destination = "yl-queue")
public void receive(Message message) {
System.out.println("message:" + message);
}
2.6 测试
来源:https://blog.csdn.net/weixin_41359273/article/details/122865254
标签:SpringBoot,activemq
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C# 4.0 大数的运算--BigInteger的应用详解
2022-02-02 06:40:05
![](https://img.aspxhome.com/file/2023/2/74832_0s.png)
C语言图书管理系统课程设计
2021-10-09 14:09:48
![](https://img.aspxhome.com/file/2023/6/127506_0s.jpg)
Go&java算法之最大数示例详解
2023-10-30 08:54:56
Java二维数组实战案例
2022-08-13 08:59:25
![](https://img.aspxhome.com/file/2023/6/63996_0s.png)
简单理解Java的垃圾回收机制与finalize方法的作用
2023-02-04 01:49:37
![](https://img.aspxhome.com/file/2023/6/128236_0s.jpg)
android的消息处理机制(图文+源码分析)—Looper/Handler/Message
2023-01-04 08:04:15
![](https://img.aspxhome.com/file/2023/6/101856_0s.png)
Android开发者需要知道的8个项目管理技巧
2022-04-11 19:51:02
![](https://img.aspxhome.com/file/2023/1/139661_0s.jpg)
C#关闭指定名字进程的方法
2021-11-23 23:59:42
android在连拍菜单中增加连拍张数选项功能实现代码
2023-05-27 19:08:38
Android滑动拼图验证码控件使用方法详解
2021-10-03 02:56:06
![](https://img.aspxhome.com/file/2023/8/113278_0s.jpg)
java为什么会出现精度丢失这种现象你知道吗
2023-07-04 05:24:38
![](https://img.aspxhome.com/file/2023/9/104029_0s.png)
java 开发中网络编程之IP、URL详解及实例代码
2023-08-06 10:26:29
一文带你学会规则引擎Drools的应用
2022-04-03 08:40:59
![](https://img.aspxhome.com/file/2023/0/66670_0s.png)
Java中多媒体文件上传及页面回显的操作代码
2021-11-21 09:45:31
![](https://img.aspxhome.com/file/2023/2/66202_0s.png)
详解Java的引用类型及使用场景
2021-06-27 11:34:18
C#集合之队列的用法
2022-11-24 16:15:38
![](https://img.aspxhome.com/file/2023/4/108794_0s.jpg)
android读取Assets图片资源保存到SD卡实例
2022-12-18 19:50:24
新手必看Unity2019 2020保姆级安装教程
2023-08-23 07:13:37
![](https://img.aspxhome.com/file/2023/3/102153_0s.png)
C#实现插入排序
2023-07-02 13:16:58
![](https://img.aspxhome.com/file/2023/7/102317_0s.png)
C#中深拷贝和浅拷贝的介绍与用法
2022-11-04 07:42:46
![](https://img.aspxhome.com/file/2023/8/98198_0s.jpg)