本篇文章是實戰篇,有關MQ的基本概念請自行查閱有關資料,網上關於此方面教程也多如牛毛,本章就不贅述了。
有關ActiveMQ的介紹可以看這篇教程:ActiveMQ入門
示例項目需要使用SpringBoot來搭建兩個項目,jms項目作爲消息發送端,jms-client項目作爲消息接收端。
一、安裝ActiveMQ
ActiveMq下載安裝教程,讀者可參照此教程下載安裝ActiveMQ。
二、JMS項目--消息發送端
1、POM文件
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.scb</groupId>
<artifactId>jms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jms</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
其中,spring-boot-starter-activemq就是ActiveMQ需要加載的依賴。
2、配置ActiveMQ
package com.scb.jms.config;
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import javax.jms.ConnectionFactory;
@Slf4j
@Configuration
public class JmsConfig {
//Create ConnectionFactory Bean
@Bean
public ActiveMQConnectionFactory connectionFactory(){
ActiveMQConnectionFactory connectionFactory=new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL("tcp://localhost:61616");
return connectionFactory;
}
// Create cache
@Bean
public CachingConnectionFactory cachingConnectionFactory(ConnectionFactory connectionFactory){
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
cachingConnectionFactory.setTargetConnectionFactory(connectionFactory);
cachingConnectionFactory.setSessionCacheSize(10);
return cachingConnectionFactory;
}
//Create Destination
@Bean
public ActiveMQQueue queue(){
ActiveMQQueue queue=new ActiveMQQueue();
queue.setPhysicalName("jms.queue");
return queue;
}
@Bean
public ActiveMQTopic topic(){
ActiveMQTopic topic = new ActiveMQTopic();
topic.setPhysicalName("jms.topic");
return topic;
}
//Create JmsTemplate Bean queue
@Bean
public JmsTemplate queueJmsTemplate(ConnectionFactory cachingConnectionFactory,ActiveMQQueue queue){
log.info("queueJmsTemplate isQueue :: {}",queue.isQueue());
JmsTemplate jmsTemplate=new JmsTemplate();
jmsTemplate.setConnectionFactory(cachingConnectionFactory);
jmsTemplate.setDefaultDestination(queue);
return jmsTemplate;
}
//Create JmsTemplate Bean topic
@Bean
public JmsTemplate topicJmsTemplate(ConnectionFactory cachingConnectionFactory,ActiveMQTopic topic){
log.info("topicJmsTemplate isTopic :: {}",topic.isTopic());
JmsTemplate jmsTemplate=new JmsTemplate();
jmsTemplate.setConnectionFactory(cachingConnectionFactory);
jmsTemplate.setDefaultDestination(topic);
jmsTemplate.setPubSubDomain(true);
return jmsTemplate;
}
}
首先,需要先配置ActiveMQ的連接工廠(ActiveMQConnectionFactory),通過該連接工廠我們就能創建ActiveMQConnection。但是頻繁的創建銷燬是非常浪費系統資源的,所以這裏我們將ActiveMQConnectionFactory配置到CachingConnectionFactory中,這相當於是一個連接池。
然後,我們創建了兩種Destination:Queue和Topic。最後創建JmsTemplate對象。通過JmsTemplate,我們可以非常簡單地去發送接收消息。
3、Service層
在配置好ActiveMQ後,我們就可以通過JmsTemplate來進行消息的發送了。這裏,爲了解耦,將業務邏輯封裝進Service層。
package com.scb.jms.service;
import javax.jms.JMSException;
public interface ISenderService {
void sendMessage(String message);
void sendMessages() throws JMSException;
}
上面是發送消息的服務接口,下面我們看看,它的兩個實現類。
package com.scb.jms.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import javax.jms.JMSException;
import java.util.Date;
@Slf4j
@Service
public class QueueSenderServiceImpl implements ISenderService {
@Autowired
@Qualifier(value = "queueJmsTemplate")
private JmsTemplate jmsTemplate;
@Override
public void sendMessage(String message) {
jmsTemplate.convertAndSend(message);
}
public void sendMessages() throws JMSException {
StringBuilder payload = null;
for (int i = 0; i < 100; ++i) {
payload = new StringBuilder();
payload.append("Message [").append(i).append("] sent at: ").append(new Date());
jmsTemplate.convertAndSend(payload.toString());
log.info("Sending message number [" + i + "]");
}
}
}
QueueSenderServiceImpl類使用queueJmsTemplate來將消息發送到Queue中。
package com.scb.jms.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import javax.jms.JMSException;
import java.util.Date;
@Slf4j
@Service
public class TopicSenderServiceImpl implements ISenderService {
@Autowired
@Qualifier(value = "topicJmsTemplate")
private JmsTemplate jmsTemplate;
@Override
public void sendMessage(String message) {
jmsTemplate.convertAndSend(message);
}
public void sendMessages() throws JMSException {
StringBuilder payload = null;
for (int i = 0; i < 100; ++i) {
payload = new StringBuilder();
payload.append("Message [").append(i).append("] sent at: ").append(new Date());
jmsTemplate.convertAndSend(payload.toString());
log.info("Sending message number [" + i + "]");
}
}
}
而TopicSenderServiceImpl類使用topicJmsTemplate來將消息發送到Topic中。
通過jmsTemplate的convertAndSend方法,我們可以非常簡單地進行消息發送。
4、Controller層
Service層開發完成後,我們就可以在Controller層進行調用了。
package com.scb.jms.controller;
import com.scb.jms.service.ISenderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.jms.JMSException;
@Controller
@RequestMapping(value = "/jms/topic")
public class JmsTopicController {
@Autowired
@Qualifier(value = "topicSenderServiceImpl")
private ISenderService senderService;
@GetMapping("/{msg}")
public void sendMessage(@PathVariable(name = "msg") String msg) {
senderService.sendMessage(msg);
}
@GetMapping("/")
@ResponseBody
public void sendMessages() {
try {
senderService.sendMessages();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
這裏,也拆分成兩個Controller。
package com.scb.jms.controller;
import com.scb.jms.service.ISenderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.jms.JMSException;
@Controller
@RequestMapping(value = "/jms/queue")
public class JmsQueueController {
@Autowired
@Qualifier(value = "queueSenderServiceImpl")
private ISenderService senderService;
@GetMapping("/{msg}")
public void sendMessage(@PathVariable(name = "msg") String msg){
senderService.sendMessage(msg);
}
@GetMapping("/")
@ResponseBody
public void sendMessages(){
try {
senderService.sendMessages();
}catch (JMSException e){
e.printStackTrace();
}
}
}
OK,到這裏我們jms項目消息發送端已經開發完成。如果你已經把ActiveMQ運行起來了,當你訪問http://localhost:8080/jms/topic/就可以將消息發送到ActiveMQ的Topic中,如下圖所示。(其他同理)
三、jms-client項目--消息接收端
1、pom文件
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.scb</groupId>
<artifactId>jms_client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jms_client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
可以看到依賴包和jms項目是一樣的
2、ActiveMQ配置類
package com.scb.jms_client.config;
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import javax.jms.ConnectionFactory;
import javax.jms.MessageListener;
@Slf4j
@Configuration
public class JmsConfig {
//Create ConnectionFactory Bean
@Bean
public ActiveMQConnectionFactory connectionFactory(){
ActiveMQConnectionFactory connectionFactory=new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL("tcp://localhost:61616");
return connectionFactory;
}
// Create cache
@Bean
public CachingConnectionFactory cachingConnectionFactory(ConnectionFactory connectionFactory){
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
cachingConnectionFactory.setTargetConnectionFactory(connectionFactory);
cachingConnectionFactory.setSessionCacheSize(10);
return cachingConnectionFactory;
}
//Create Destination
@Bean
public ActiveMQQueue queue(){
ActiveMQQueue queue=new ActiveMQQueue();
queue.setPhysicalName("jms.queue");
return queue;
}
@Bean
public ActiveMQTopic topic(){
ActiveMQTopic topic = new ActiveMQTopic();
topic.setPhysicalName("jms.topic");
return topic;
}
//Create JmsTemplate Bean queue
@Bean
public JmsTemplate queueJmsTemplate(ConnectionFactory cachingConnectionFactory,ActiveMQQueue queue){
log.info("jmsTemplate:"+cachingConnectionFactory.getClass());
JmsTemplate jmsTemplate=new JmsTemplate();
jmsTemplate.setConnectionFactory(cachingConnectionFactory);
//jmsTemplate.setDefaultDestinationName("jms.queue");
jmsTemplate.setDefaultDestination(queue);
return jmsTemplate;
}
//Create JmsTemplate Bean topic
@Bean
public JmsTemplate topicJmsTemplate(ConnectionFactory cachingConnectionFactory,ActiveMQTopic topic){
JmsTemplate jmsTemplate=new JmsTemplate();
jmsTemplate.setConnectionFactory(cachingConnectionFactory);
jmsTemplate.setDefaultDestination(topic);
jmsTemplate.setPubSubNoLocal(true);
return jmsTemplate;
}
//配置消息偵聽容器
@Bean
public DefaultMessageListenerContainer queueListenerContainer(ConnectionFactory cachingConnectionFactory, ActiveMQQueue queue, MessageListener messageListener){
DefaultMessageListenerContainer listenerContainer=new DefaultMessageListenerContainer();
listenerContainer.setConnectionFactory(cachingConnectionFactory);
listenerContainer.setDestination(queue);
listenerContainer.setMessageListener(messageListener);
return listenerContainer;
}
//配置消息偵聽容器
@Bean
public DefaultMessageListenerContainer topicListenerContainer(ConnectionFactory cachingConnectionFactory, ActiveMQTopic topic, MessageListener messageListener){
DefaultMessageListenerContainer listenerContainer=new DefaultMessageListenerContainer();
listenerContainer.setConnectionFactory(cachingConnectionFactory);
listenerContainer.setDestination(topic);
listenerContainer.setMessageListener(messageListener);
listenerContainer.setPubSubDomain(true);
return listenerContainer;
}
}
這裏跟jms項目相比,多配置了DefaultMessageListenerContainer Bean。這是個消息監聽容器,當配置好它後,我們就可以異步監聽是否有消息。它依賴於MessageListener對象,下面我們來配置他。
package com.scb.jms_client.pojo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
@Slf4j
@Component
public class JmsMessageListener implements MessageListener {
@Override
public void onMessage(Message message) {
try {
TextMessage msg = (TextMessage) message;
log.info("Consumed message: " + msg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
MessageListener的onMessage定義了接收到消息時採取的措施。
需注意的是:jms-client項目端口要改成8081(避免和jms項目端口衝突)
四、運行
運行兩個項目和ActiveMQ,訪問http://localhost:8080/jms/topic/,將消息發送到topic中。jms項目的輸出如下:
此時,在jms-client項目的控制檯可以看到,如下輸出:
這是因爲jms-client項目配置了DefaultMessageListenerContainer,有消息進入ActiveMQ會自動進行消費。
後記
本章介紹瞭如何在SpringBoot中搭建一個簡單的ActiveMQ應用項目。之後,我將繼續介紹ActiveMQ如何與Zookeeper進行集成,搭建ActiveMQ集羣。