JMS
目录
- JMS
- 1 什么是JMS
- 2 SpringBoot中使用ActiveMQ中间件
- 2.1 基础配置
- pom.xml
- application.yml
- 2.2 PTP(Point-To-Point 点对点)模式
- 创建消费者(Consumer)
- 创建生产者(producer)
- 生产者生产
- 2.3 发布订阅模式(Publish/Subscribe)
- 创建消费者
- 创建生产者
- 生产者生产
- 2.4 发布订阅模式与点对点模式共存
- 配置configuration
- 消费者
- 生产者
- 生产者生产
- 3. 注意事项
- 3.1 连接池的应用
1 什么是JMS
JMS
全称Java Message Service
,即Java消息服务应用程序接口,是Java平台中一种面向消息中间件(MOM)的API。用在两个应用程序之间,或分布式系统中发送消息,进行异步通信。JMS
是一种与具体语言无关的API,绝大多数MOM提供商都对JMS
提供支持。
2 SpringBoot中使用ActiveMQ中间件
消息中间件的优点:
- 应用解耦。
- 异步处理。
- 流量消峰。
2.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 https://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.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo</groupId>
<artifactId>jms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jms</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</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.messaginghub</groupId>
<artifactId>pooled-jms</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
server:
port: 8080
spring:
activemq:
user: admin
password: admin
broker-url: tcp://localhost:61616
pool:
enabled: true
max-connections: 5
jms:
cache:
session-cache-size: 5
# pub-sub-domain: true
2.2 PTP(Point-To-Point 点对点)模式
注意:如果在
ActiveMQ
中使用PTP,必须保证配置文件中的spring.jms.pub-sub-domain
设为false(即默认值),因为如果为true时只能使用发布/订阅模式
创建消费者(Consumer)
package com.demo.jms.consumer;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class MessageConsumer {
@JmsListener(destination = "queue")
public void receiveMessage(String message) {
System.out.println(this.getClass().getName() + ": " + message);
}
}
创建生产者(producer)
package com.demo.jms.producer;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.jms.Destination;
@Service
public class ProducerService {
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
public ProducerService (JmsMessagingTemplate template) {
this.jmsMessagingTemplate = template;
}
public void sendMessage(String destinationName, String msg) {
Destination destination = new ActiveMQQueue(destinationName);
jmsMessagingTemplate.convertAndSend(destination, msg);
}
}
生产者生产
package com.demo.jms.controller;
import com.demo.jms.producer.ProducerService;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import javax.jms.Destination;
@Controller
public class ProducerController {
private ProducerService service;
public ProducerController(ProducerService service) {
this.service = service;
}
@GetMapping(value="/send/{destinationName}/{msg}")
public void sendMsg( @PathVariable("destinationName") String destinationName, @PathVariable("msg") String msg) {
service.sendMessage(destinationName, msg);
}
}
2.3 发布订阅模式(Publish/Subscribe)
注意:发布订阅模式中必须将
spring.jms.pub-sub-domain
设置为true
创建消费者
package com.demo.jms.consumer;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class TopicMessageConsumer {
@JmsListener(destination = "topic")
public void receiveTopic(String message) {
System.out.println(this.getClass().getName() + ": " + message);
}
@JmsListener(destination = "topic")
public void receiveTopic2(String message) {
System.out.println("发布订阅模式: " + message);
}
}
创建生产者
package com.demo.jms.producer;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.jms.Destination;
@Service
public class TopicProducerService {
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
public TopicProducerService(JmsMessagingTemplate template) {
this.jmsMessagingTemplate = template;
}
public void sendMessageTopic(String topicName, String msg) {
// 此处与点对点模式不同
ActiveMQTopic topic = new ActiveMQTopic(topicName);
jmsMessagingTemplate.convertAndSend(topic, msg);
}
}
生产者生产
package com.demo.jms.controller;
import com.demo.jms.producer.TopicProducerService;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import javax.jms.Destination;
@Controller
public class TopicProducerController {
private TopicProducerService service;
public TopicProducerController(TopicProducerService service) {
this.service = service;
}
@GetMapping(value = "/topic/{topicName}/{msg}")
public void sendTopic(@PathVariable("topicName") String topicName, @PathVariable("msg") String msg) {
service.sendMessageTopic(topicName, msg);
}
}
2.4 发布订阅模式与点对点模式共存
由于
spring.jms.pub-sub-domain
设置为true时仅支持发布/订阅,设置为false时又不会触发发布/订阅。所以需要在代码中主动添加发布/订阅的监听bean
,且spring.jms.pub-sub-domain
必须设置为***false***
配置configuration
package com.demo.jms.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import javax.jms.ConnectionFactory;
@Configuration
public class ActiveMQConfig {
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory pooledJmsConnectionFactory) {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setPubSubDomain(true);
bean.setConnectionFactory(pooledJmsConnectionFactory);
return bean;
}
}
消费者
package com.demo.jms.consumer;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class MessageConsumer {
@JmsListener(destination = "queue")
public void receiveMessage(String msg) {
System.out.println(this.getClass().getName() + ": " + msg);
}
/**
* 注意:发布/订阅中的<code>containerFactory</code>需要设置成配置类<code>ActiveMQConfig</code>中
* 的<code>jmsListenerContainerTopic</code>
*/
@JmsListener(destination = "topic", containerFactory = "jmsListenerContainerTopic")
public void receiveTopic(String msg) {
System.out.println("receiveTopic: " + msg);
}
@JmsListener(destination = "topic", containerFactory = "jmsListenerContainerTopic")
public void receiveTopic2(String msg) {
System.out.println("receiveTopic2: " + msg);
}
}
生产者
package com.demo.jms.producer;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.jms.Destination;
@Service
public class MessageProducer {
private JmsMessagingTemplate jmsMessagingTemplate;
public MessageProducer(){}
@Autowired
public MessageProducer(JmsMessagingTemplate template) {
this.jmsMessagingTemplate = template;
}
/**
* 发送消息:点对点模式。
* @param destinationName 接收者
* @param message 消息内容
*/
public void sendMessage(String destinationName, String message) {
Destination destination = new ActiveMQQueue(destinationName);
jmsMessagingTemplate.convertAndSend(destination, message);
}
/**
* 发送消息:发布/订阅模式
*/
public void sendTopic(String topicName, String message) {
ActiveMQTopic topic = new ActiveMQTopic(topicName);
jmsMessagingTemplate.convertAndSend(topic, message);
}
}
生产者生产
package com.demo.jms.controller;
import com.demo.jms.producer.MessageProducer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProducerController {
private MessageProducer producer;
public ProducerController(MessageProducer producer) {
this.producer = producer;
}
@GetMapping(value = "/send/{destination}/{msg}")
public void sendMessage(@PathVariable("msg") String msg, @PathVariable("destination") String destinationName) {
producer.sendMessage(destinationName, msg);
}
@GetMapping(value = "/topic/{topicName}/{msg}")
public void sendTopic(@PathVariable("msg") String msg, @PathVariable("topicName") String topicName) {
producer.sendTopic(topicName, msg);
}
}
项目启动后再浏览器中输入:
http://localhost:8080/send/queue/这是点对点模式
或
http://localhost:8080/topic/topic/这是发布订阅模式
则在后台分别输出
com.demo.jms.consumer.MessageConsumer: 这是点对点模式
和
receiveTopic2: 这是发布订阅模式
receiveTopic: 这是发布订阅模式
3. 注意事项
3.1 连接池的应用
- 如果在配置文件
application.properties
中使用了spring.activemq.pool.enadble = true
后,需要在pom.xml
中引入连接池相关依赖:
<!-- 在旧版本spring boot中需要引入activemq-pool依赖 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.9.1</version>
</dependency>
<!-- 在spring boot 2.x中需要引入pooled-jms依赖 -->
<dependency>
<groupId>org.messaginghub</groupId>
<artifactId>pooled-jms</artifactId>
</dependency>
如果不引入,则会引发JmsTemplate
注入失败的错误。具体引用哪一个依赖,则需要根据使用的SpringBoot
版本来确定,或者查看类ActiveMQConnectionFactoryConfiguration
引用的是哪一个包下的JmsPoolConnectionFactory
。如SpringBoot 2.2.4.RELEASE
中引用的是:
package org.springframework.boot.autoconfigure.jms.activemq;
import java.util.List;
import java.util.stream.Collectors;
import javax.jms.ConnectionFactory;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.commons.pool2.PooledObject;
import org.messaginghub.pooled.jms.JmsPoolConnectionFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jms.JmsPoolConnectionFactoryFactory;
import org.springframework.boot.autoconfigure.jms.JmsProperties;
import org.springframework.boot.autoconfigure.jms.JmsProperties.Cache;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.connection.CachingConnectionFactory;
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnMissingBean({ConnectionFactory.class})
class ActiveMQConnectionFactoryConfiguration {
// ...
}