一)ActiveMQ Jar引入和配置
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.oysept</groupId>
<artifactId>activemq_springboot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot activemq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!-- activemq线程池 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
<!-- 在2.X版本,spring.activemq.pool.enabled=true时,需依赖该jar -->
<dependency>
<groupId>org.messaginghub</groupId>
<artifactId>pooled-jms</artifactId>
</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:
broker-url: tcp://localhost:61616
user: admin
password: admin
pool:
enabled: true
max-connections: 50
注:springboot中activemq的配置可以在spring-boot-autoconfigure-2.3.0.RELEASE.jar包的org.springframework.boot.autoconfigure.jms.activemq.ActiveMQProperties类中查找。
二)ActiveMQ Queues点对点模式
QueuesController.java:
package com.oysept.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
@RestController
@RequestMapping(path = "/activemq")
public class QueuesController {
// queues名称
private final static String ACTIVEMQ_QUEUES_NAME = "activemq.queues.ouyangjun";
@Autowired
private JmsTemplate jmsTemplate;
/**
* 消息生产者(点对点)
* 说明: 点对点发送的任意一条信息,只能由一个人接收,也就是说该发送的消息只能被消费一次
* 测试地址: http://localhost:8080/activemq/sendQueuesMsg?queuesMsg=Hello%20ActiveMQ%20Queues
*/
@RequestMapping(path = "/sendQueuesMsg")
public String sendQueuesMsg(@RequestParam("queuesMsg") String queuesMsg) {
jmsTemplate.send(ACTIVEMQ_QUEUES_NAME, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage();
textMessage.setText(queuesMsg);
return textMessage;
}
});
return queuesMsg;
}
/**
* 消息消费者(点对点)
*/
@JmsListener(destination = ACTIVEMQ_QUEUES_NAME)
public void receiveQueues(String queuesMsg) {
System.out.println("==>queues点对点消费者:receive: " + queuesMsg);
}
}
三)ActiveMQ Topics发布订阅模式
第一步:需要先创建一个默认的DefaultJmsListenerContainerFactory,自定义实现该方式
package com.oysept.config;
import javax.jms.ConnectionFactory;
import javax.jms.Topic;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
@Configuration
public class TopicsConfig {
// Topics名称
public final static String ACTIVEMQ_TOPICS_NAME = "activemq.topics.ouyangjun";
/**
* 默认监听工厂配置
* @param connectionFactory
* @return
*/
@Bean(name="activemqJmsListenerContainerTopics")
public JmsListenerContainerFactory<?> jmsListenerContainerTopics(ConnectionFactory connectionFactory) {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setPubSubDomain(true);
bean.setConnectionFactory(connectionFactory);
return bean;
}
@Bean(name="activemqTopics")
public Topic topics() {
return new ActiveMQTopic(ACTIVEMQ_TOPICS_NAME);
}
}
第二步:测试Topics发布订阅功能
package com.oysept.controller;
import com.oysept.config.TopicsConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.jms.Topic;
@RestController
@RequestMapping(path = "/activemq")
public class TopicsController {
@Autowired
private JmsTemplate jmsTemplate;
@Autowired
@Qualifier(value="activemqTopics")
private Topic topics;
/**
* topic消息生产者
* 测试地址: http://localhost:8080/activemq/sendTopicsMsg?topicsMsg=Hello%20ActiveMQ%20Topics
*/
@RequestMapping(value="/sendTopicsMsg")
public String sendTopicMsg(@RequestParam("topicsMsg") String topicsMsg) {
// topics
this.jmsTemplate.convertAndSend(this.topics, topicsMsg);
return topicsMsg;
}
// @JmsListener如果不指定独立的containerFactory的话,就只能消费queues消息
// 消费者1
@JmsListener(destination= TopicsConfig.ACTIVEMQ_TOPICS_NAME, containerFactory="activemqJmsListenerContainerTopics")
public void receive1(String topicsMsg) {
System.out.println("==>topics消费者:receive1: " + topicsMsg);
}
// 消费者2
@JmsListener(destination=TopicsConfig.ACTIVEMQ_TOPICS_NAME, containerFactory="activemqJmsListenerContainerTopics")
public void receive2(String topicsMsg) {
System.out.println("==>topics消费者:receive2: " + topicsMsg);
}
}
四)SpringBoot启动类和项目结构图
ActiveMQApplication.java:
package com.oysept;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ActiveMQApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(ActiveMQApplication.class);
app.run(args);
}
}
项目结构图:
启动ActiveMQApplication后,在通过对应的测试地址在浏览器中访问。
再登录http://localhost:8161/admin界面用账号密码admin/admin查询消息传递的情况。
识别二维码关注个人微信公众号
本章完结,待续,欢迎转载!
本文说明:该文章属于原创,如需转载,请标明文章转载来源!