SpringBoot整合activeMq之helloWorld
以下内容针对Windows或者linux系统已经安装有activemq并能访问成功的同学。如果还未安装的请参考博主另外一篇博客之linux下安装activemq。。。。。。。。
生产者整合如下---------------
1. 安装好activemq能进行如下访问页面
2. 下面开始进行springboot整合activemq,创建springboot工程,添加activemq依赖,如下:
org.springframework.boot
spring-boot-starter-activemq
3. 创建application.yml文件,进行如下配置:
server:
port: 7777
#配置activemq相关配置
spring:
activemq:
broker-url: tcp://192.168.150.131:61616
user: admin
password: admin
jms:
pub-sub-domain: true #是否订阅模式 true为订阅模式 false为队列模式
#消息目的地名称 写在配置文件中便于修改, 尽量减少硬编码
myTopic: activemqTopic
myTopic1: activemqTopic1
4. 编写activemq配置类如下图,创建订阅消息topic交给spring管理:
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.jms.Topic;
/**
* @Author: LongLingBao
* @Date: 2019/7/27 20:42
*/
@Component
public class ActivemqConfig {
@Value("${myTopic}")
private String topicName;
@Value("${myTopic1}")
private String topicName1;
@Bean
public Topic topic()throws Exception{
return new ActiveMQTopic(topicName);
}
@Bean
public Topic topic1()throws Exception{
return new ActiveMQTopic(topicName1);
}
}
5. 编写发送订阅消息类,也就是我们的service业务层代码:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.jms.Topic;
import java.util.Date;
/**
* @Author: LongLingBao
* @Date: 2019/7/27 20:45
*/
@Service
public class ActivemqTopic {
@Autowired
private Topic topic;
@Autowired
private Topic topic1;
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
private int i = 0;
private int j = 0;
//此注解项目启动后每间隔多少时间就往MQ发一条消息
//标识了此注解 要想起作用就要在主启动类添加@EnableScheduling注解开启此功能
@Scheduled(fixedDelay = 3000)
public void setTopic(){
jmsMessagingTemplate.convertAndSend(topic,"topic消息发送开始了!!!!!"+new Date());
i = ++i;
System.out.println("第一个Topic发送消息开始----第"+i+"条--------------");
}
@Scheduled(fixedDelay = 3000)
public void setTopic1(){
j = ++j;
jmsMessagingTemplate.convertAndSend(topic1,"topic1消息发送开始了!!!!!"+new Date());
System.out.println("第二个Topic1发送消息开始----第"+j+"条--------------");
}
}
- 在启动类添加注解@EnableScheduling并启动程序:
- 出现如下内容消息发布成功,并且打开activemq客户端能看到我们发送的消息:
到此消息就发送成功了!!!!!!!!!!!!!!!!!
注:作为主题订阅topic类型的我们往往都是先启动消费者然后在启动生产者的,这样消息发布才是有用的消息,,,,,,,,切记
消费者整合如下
1. 创建springboot工程,引入Activemq依赖,如下图:
2. 新建application.yml配置文件,添加如下配置:
3. 编写监听主题订阅消息类:
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.jms.TextMessage;
/**
* @Author: LongLingBao
* @Date: 2019/7/27 21:07
*/
@Component
public class ActivemqTopic {
//添加此注解表明需要监听哪个主题下的消息
@JmsListener(destination = "${myTopic}")
public void getTopic(TextMessage textMessage)throws Exception{
System.out.println("收到Topic消息:----------"+textMessage.getText());
}
//添加此注解表明需要监听哪个主题下的消息
@JmsListener(destination = "${myTopic1}")
public void getTopic1(TextMessage textMessage)throws Exception{
System.out.println("收到Topic1消息:----------"+textMessage.getText());
}
}
4. 找到主启动类启动程序:
5. 再启动上面我们编写好了的生产者程序,如下图消息生产成功:
6. 回到消费者程序,看控制台,打印出如下内容说明消息消费成功:
到此springboot整和activemq简单的helloworld已全部完成!!!!!!!!!!!!!!!