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。管理页面如下:
1、新建SpringBoot项目
新建Springboot项目,添加对应的依赖。项目完整的pom.xml文件如下:
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
2、相关配置信息
在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
3、ActiveMQ配置类
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;
/**
//队列
@Bean
public ActiveMQQueue queue(){
return new ActiveMQQueue(myQueue);
}
//topic
@Bean
public Topic topic(){
return new ActiveMQTopic(topicName);
}
}
、队列生产者
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;
/*
@Autowired
private Queue queue;
/*
队列消费者
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.web.bind.annotation.RestController;
/*
在这里插入图片描述