spring boot整合JMS(ActiveMQ實現)
阿新 • • 發佈:2019-01-07
一、安裝ActiveMQ
具體的安裝步驟,請參考我的另一篇博文:
二、新建spring boot工程,並加入JMS(ActiveMQ)依賴
三、工程結構
pom依賴如下:
四、修改application.properties配置檔案<?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.chhliu.springboot.jms</groupId> <artifactId>springboot-jms</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-jms</name> <description>Demo project for Spring Boot Jms</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.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.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </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>
## URL of the ActiveMQ broker. Auto-generated by default. For instance `tcp://localhost:61616` # failover:(tcp://localhost:61616,tcp://localhost:61617) # tcp://localhost:61616 spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.in-memory=true spring.activemq.pool.enabled=false //如果此處設定為true,需要加如下的依賴包,否則會自動配置失敗,報JmsMessagingTemplate注入失敗
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<!-- <version>5.7.0</version> -->
</dependency>
五、訊息生產者
六、訊息消費者package com.chhliu.springboot.jms; import javax.jms.Destination; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.stereotype.Service; @Service("producer") public class Producer { @Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate對JmsTemplate進行了封裝 private JmsMessagingTemplate jmsTemplate; // 傳送訊息,destination是傳送到的佇列,message是待發送的訊息 public void sendMessage(Destination destination, final String message){ jmsTemplate.convertAndSend(destination, message); } }
package com.chhliu.springboot.jms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class Consumer {
// 使用JmsListener配置消費者監聽的佇列,其中text是接收到的訊息
@JmsListener(destination = "mytest.queue")
public void receiveQueue(String text) {
System.out.println("Consumer收到的報文為:"+text);
}
}
消費者2的程式碼同上,注意,訊息消費者的類上必須加上@Component,或者是@Service,這樣的話,訊息消費者類就會被委派給Listener類,原理類似於使用SessionAwareMessageListener以及MessageListenerAdapter來實現訊息驅動POJO
七、測試
package com.chhliu.springboot.jms;
import javax.jms.Destination;
import org.apache.activemq.command.ActiveMQQueue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootJmsApplicationTests {
@Autowired
private Producer producer;
@Test
public void contextLoads() throws InterruptedException {
Destination destination = new ActiveMQQueue("mytest.queue");
for(int i=0; i<100; i++){
producer.sendMessage(destination, "myname is chhliu!!!");
}
}
}
測試結果如下:
Consumer2收到的報文為:myname is chhliu!!!
Consumer收到的報文為:myname is chhliu!!!
Consumer2收到的報文為:myname is chhliu!!!
Consumer收到的報文為:myname is chhliu!!!
Consumer2收到的報文為:myname is chhliu!!!
Consumer收到的報文為:myname is chhliu!!!
Consumer2收到的報文為:myname is chhliu!!!
Consumer收到的報文為:myname is chhliu!!!
Consumer2收到的報文為:myname is chhliu!!!
Consumer收到的報文為:myname is chhliu!!!
Consumer2收到的報文為:myname is chhliu!!!
Consumer收到的報文為:myname is chhliu!!!
Consumer2收到的報文為:myname is chhliu!!!
經過上面的幾個步驟,spring boot和Jms就基本上整合完成了,是不是使用起來很方便了!
八、實現雙向佇列
1、下面首先來對Consumer2這個消費者來進行下改造,程式碼如下:
package com.chhliu.springboot.jms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;
@Component
public class Consumer2 {
@JmsListener(destination = "mytest.queue")
@SendTo("out.queue")
public String receiveQueue(String text) {
System.out.println("Consumer2收到的報文為:"+text);
return "return message"+text;
}
}
從上面的程式碼可以看出,我們在receiveQueue方法上面多加了一個註解@SendTo("out.queue"),該註解的意思是將return回的值,再發送的"out.queue"佇列中,下面我們再來跑一下前面的測試,在監控頁面中,我們發現,"out.queue"佇列中已經有內容了,如下:
進入Browse介面觀看:
最後看下收到的具體資訊:
我們發現,該佇列中的訊息,就是我們返回的值!
九、對Producer進行改造
通過上面的示例,我們現在對Producer進行改造,使其既能生產報文,又能消費佇列中的報文,程式碼如下:
package com.chhliu.springboot.jms;
import javax.jms.Destination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
@Service("producer")
public class Producer {
@Autowired
private JmsMessagingTemplate jmsTemplate;
public void sendMessage(Destination destination, final String message){
jmsTemplate.convertAndSend(destination, message);
}
@JmsListener(destination="out.queue")
public void consumerMessage(String text){
System.out.println("從out.queue佇列收到的回覆報文為:"+text);
}
}
測試結果如下:
從out.queue佇列收到的回覆報文為:return messagemyname is chhliu!!!
Consumer收到的報文為:myname is chhliu!!!
Consumer2收到的報文為:myname is chhliu!!!
從out.queue佇列收到的回覆報文為:return messagemyname is chhliu!!!
Consumer收到的報文為:myname is chhliu!!!
Consumer2收到的報文為:myname is chhliu!!!
從out.queue佇列收到的回覆報文為:return messagemyname is chhliu!!!
Consumer收到的報文為:myname is chhliu!!!
Consumer2收到的報文為:myname is chhliu!!!
從out.queue佇列收到的回覆報文為:return messagemyname is chhliu!!!