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;
/*
在這裏插入圖片描述