一.ActiveMQ 下载
Windows 下安装
二.ActiveMQ 安装
JDK21 已安装并正确配置环境变量
解压上面下载的压缩包,64位电脑单击如图位置打开控制台,并输入命令
activemq.bat start
控制台
查看启动结果
netstat -aon | findstr 61616
三.代码测试
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.example</groupId>
<artifactId>flowable-test</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>mq</artifactId>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>6.0.1</version>
</dependency>
</dependencies>
</project>
2.YML配置
server:
port: 8085
spring:
activemq:
broker-url: tcp://localhost:61616
3.启动类
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MqApp {
public static void main(String[] args) {
SpringApplication.run(MqApp.class, args);
}
}
4.MQ 配置类
package org.example.config;
import jakarta.jms.Queue;
import jakarta.jms.Topic;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
/**
* @Author zhx & moon
* @Since 1.8
* @Date 2024-01-09 上午 10:55
*/
@Configuration
public class MqConfig {
@Value("${spring.activemq.broker-url}")
String brokerUrl;
static final String key = "flowable-test";
@Bean
public ActiveMQConnectionFactory connectionFactory() {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl);
return factory;
}
/**
* 点对点队列
* @return
*/
@Bean
public Queue queue(){
return new ActiveMQQueue(key);
}
/**
* 主题
* @return
*/
@Bean
public Topic topic(){
return new ActiveMQTopic(key);
}
/**
* queue 模式连接点
* @param connectionFactory
* @return
*/
@Bean("jmsListenerContainerQueue")
public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ActiveMQConnectionFactory connectionFactory){
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
return factory;
}
/**
* topic 模式连接点
* @param connectionFactory
* @return
*/
@Bean("jmsListenerContainerTopic")
public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory){
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setPubSubDomain(true);
factory.setConnectionFactory(connectionFactory);
return factory;
}
}
5.MQ 消费者
package org.example.config;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/**
* @Author zhx & moon
* @Since 1.8
* @Date 2024-01-09 上午 11:33
*/
@Component
public class MqConsumer {
static final String key = "flowable-test";
@JmsListener(destination = key,containerFactory = "jmsListenerContainerQueue")
public void consumerQ(String msg){
System.out.println("queue: " + msg);
}
@JmsListener(destination = key,containerFactory = "jmsListenerContainerTopic")
public void consumerT(String msg){
System.out.println("topic: " + msg);
}
}
6.发数测试类
package org.example.controller;
import jakarta.jms.Queue;
import jakarta.jms.Topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author zhx & moon
* @Since 1.8
* @Date 2024-01-09 上午 11:29
*/
@RestController
@RequestMapping("/test")
public class Test {
@Autowired
Queue queue;
@Autowired
Topic topic;
@Autowired
JmsMessagingTemplate template;
@GetMapping("/queue")
public void producerQ(@RequestParam("msg") String msg){
template.convertAndSend(queue,msg);
}
@GetMapping("/topic")
public void producerT(@RequestParam("msg") String msg){
template.convertAndSend(topic,msg);
}
}
四.测试
127.0.0.1:8085/test/topic?msg=topic test