SpringBoot整合activemq
SpringBoot整合activemq1.安装activemq(linux上)2.SpringBoot整合activemq1.安装activemq(linux上)1.下载压缩包,地址链接: https://activemq.apache.org/components/classic/download/.2.利用xshell上传压缩包到linux上,并且解压3.进入到bin目录启动,通过./act
·
SpringBoot整合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://blog.csdn.net/fengtangjiang/article/details/108463714.
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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</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;
@Autowired
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.5 测试
更多推荐
已为社区贡献1条内容
所有评论(0)