springboot整合activemq

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文件如下:

<?xml version="1.0" encoding="UTF-8"?>


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

org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-activemq org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin

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;

/**

  1. MQ配置類
    */
    @Component
    @EnableJms
    public class ConfigBean {
    @Value(" m y q u e u e " ) p r i v a t e S t r i n g m y Q u e u e ; @ V a l u e ( " {myqueue}") private String myQueue; @Value(" myqueue")privateStringmyQueue;@Value("{mytopic}")
    private String topicName;

//隊列
@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;

/*

  • 隊列消息生產者
    */
    @RestController
    public class QueueProducerController {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

@Autowired
private Queue queue;

/*

  • 消息生產者
    */
    @RequestMapping("/sendmsg")
    public void sendmsg(String msg) {
    System.out.println(「發送消息到隊列:」 + msg);
    // 指定消息發送的目的地及內容
    this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
    }
    }

隊列消費者
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.web.bind.annotation.RestController;

/*

  1. 隊列queue消費者控制器
    /
    @RestController
    public class QueueConsumerController {
    /
  • 消費者接收消息
    */
    @JmsListener(destination="${myqueue}")
    public void readActiveQueue(String message) {
    System.out.println(「接受到:」 + message);
    }
    }
    測試效果
    運行項目在瀏覽器中訪問http://localhost:8080/mq/sendmsg?msg=123。向消息隊列中發送123。控制檯輸出效果:

在這裏插入圖片描述