1 .SpringBoot整合ActiveMQ
1.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yzx</groupId>
<artifactId>activemqdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>activemqdemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- activemq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
<!-- activemq -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.40</version>
</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>
1.2 application.properties
# failover:(tcp://localhost:61616,tcp://localhost:61617)
# tcp://localhost:61616
spring.activemq.broker-url=tcp://localhost:61616
#true 表示使用內置的MQ,false則連接服務器
spring.activemq.in-memory=false
#true表示使用連接池;false時,每發送一條數據創建一個連接
spring.activemq.pool.enabled=true
#連接池最大連接數
spring.activemq.pool.max-connections=10
#空閒的連接過期時間,默認爲30秒
spring.activemq.pool.idle-timeout=30000
#強制的連接過期時間,與idleTimeout的區別在於:idleTimeout是在連接空閒一段時間失效,而expiryTimeout不管當前連接的情況,只要達到指定時間就失效。默認爲0,never
spring.activemq.pool.expiry-timeout=0
1.3 MessageProduce
package com.ybjt.produce;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
/**
* @author zx_h
* @create 2019-05-27 9:33
* 消息的生產者
*/
@Service
public class MessageProduce {
@Autowired
private JmsTemplate jmsTemplate;
public void sendMessage(String destination,String message){
jmsTemplate.convertAndSend(new ActiveMQQueue(destination), message);
}
}
1.4 MessageConsumer
package com.ybjt.consumer;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/**
* @author zx_h
* @create 2019-05-27 9:40
*/
@Component
public class MessageConsumer {
// 使用JmsListener配置消費者監聽的隊列,其中text是接收到的消息
@JmsListener(destination = "java.queue")
public void receiveQueue(String text) {
System.out.println("Consumer收到的報文爲:"+text);
}
}
1.5 MessageController
package com.ybjt.controller;
import com.ybjt.produce.MessageProduce;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
/**
* @author zx_h
* @create 2019-05-27 10:05
*/
@Controller
public class MessageController {
@Resource
MessageProduce messageProduce;
@RequestMapping("/send")
@ResponseBody
public String send(String quene,String message){
messageProduce.sendMessage(quene,message);
return "success";
}
}
1.6 啓動
package com.ybjt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author zx_h
* @create 2019-05-27 10:08
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
1.7 驗證