24、springboot集成ActiveMQ

简介: 目前使用较多的消息队列有ActiveMQ、RabbitMQ、Kafka、RocketMQ、MetaMQ等。spring boot提供了对JMS系统的支持;springboot很方便就可以集成这些消息中间件。

消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量削锋等问题。实现高性能,高可用,可伸缩和最终一致性架构;是大型分布式系统不可缺少的中间件。目前使用较多的消息队列有ActiveMQ、RabbitMQ、Kafka、RocketMQ、MetaMQ等。spring boot提供了对JMS系统的支持springboot很方便就可以集成这些消息中间件。


对于异步消息在实际的应用之中会有两类:


JMS:代表作就是 ActiveMQ,但是其性能不高,因为其是用 java 程序实现的

AMQP:直接利用协议实现的消息组件,其大众代表作为RabbitMQ,高性能代表作为Kafka

 

1、 新建项目,对应的pom.xml文件如下


<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>spring-cloud</groupId>
<artifactId>sc-activeMQ</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sc-activeMQ</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>


2、 新建springboot启动类ActiveMqApplication.java


package sc.activemq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ActiveMqApplication {
public static void main(String[] args) {
SpringApplication.run(ActiveMqApplication.class, args);
}
}


3、 新建配置文件application.yml


server:
  port: 9080
spring:
  appliction:
   name: sc-activemq
  activemq:
    broker-url: tcp://localhost:61616
    in-memory: true  
    user: admin
    password: admin
    pool:
      enabled: true
      max-connections: 50
      expiry-timeout: 10000
      idle-timeout: 30000
  jms:
    pub-sub-domain: false #默认情况下activemq提供的是queue模式,若要使用topic模式需要配置pub-sub-domain为true


说明:默认情况下activemq提供的是queue模式,若要使用topic模式需要配置spring.jms.pub-sub-domaintrue

 

4、 新建消费生产者


package sc.activemq.service.impl;
import javax.jms.Destination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import sc.activemq.service.ProductService;
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Override
public void sendMessage(Destination destination, String message) {
jmsMessagingTemplate.convertAndSend(destination, message);
}
}


5、 新建消息消费者


队列模式:


package sc.activemq.service.consumer;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class ConsumerQueue {
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = "jms-queue")
public void receiveQueue(String text) {
System.out.println("ConsumerQueue收到:" + text);
}
}


订阅模式:


package sc.activemq.service.consumer;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class ConsumerTopic {
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = "jms-topic")
public void receiveQueue(String text) {
System.out.println("ConsumerTopic收到:" + text);
}
}


6、 新建测试类


package sc.activemq;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
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;
import sc.activemq.service.ProductService;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ActiveMqTest {
@Autowired
private ActiveMQQueue queue;
@Autowired
private ActiveMQTopic topic;
@Autowired
private ProductService productService;
@Test
public void testJms() {
String msgQueue = "send 黄金 ";
for(int i=0; i<5; i++){
productService.sendMessage(this.queue, msgQueue+i);
}
String msgTopic = "send 白银 ";
for(int i=0; i<5; i++){
productService.sendMessage(this.topic, msgTopic+i);
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
}
}


7、 进行测试


先登录ActiveMq管理平台:http://localhost:8161/


 队列模式:


1)配置spring.jms.pub-sub-domainfalse


微信图片_20220501214543.png


2)注释测试类的如下代码


微信图片_20220501214547.png


(3)运行测试类


微信图片_20220501214550.png


订阅模式:


(1) 配置spring.jms.pub-sub-domaintrue


微信图片_20220501214554.png


(2) 注释测试类的如下代码


微信图片_20220501214557.png


(3) 运行测试类


微信图片_20220501214602.png


https://gitee.com/hjj520/spring-cloud-2.x


相关文章
|
6月前
|
消息中间件 存储 Java
第15课: Spring Boot中集成ActiveMQ
第15课: Spring Boot中集成ActiveMQ
541 0
|
10月前
|
消息中间件 存储 Java
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——ActiveMQ安装
本教程介绍ActiveMQ的安装与基本使用。首先从官网下载apache-activemq-5.15.3版本,解压后即可完成安装,非常便捷。启动时进入解压目录下的bin文件夹,根据系统选择win32或win64,运行activemq.bat启动服务。通过浏览器访问`http://127.0.0.1:8161/admin/`可进入管理界面,默认用户名密码为admin/admin。ActiveMQ支持两种消息模式:点对点(Queue)和发布/订阅(Topic)。前者确保每条消息仅被一个消费者消费,后者允许多个消费者同时接收相同消息。
372 0
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——ActiveMQ安装
|
10月前
|
消息中间件 Java 微服务
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——发布/订阅消息的生产和消费
本文详细讲解了Spring Boot中ActiveMQ的发布/订阅消息机制,包括消息生产和消费的具体实现方式。生产端通过`sendMessage`方法发送订阅消息,消费端则需配置`application.yml`或自定义工厂以支持topic消息监听。为解决点对点与发布/订阅消息兼容问题,可通过设置`containerFactory`实现两者共存。最后,文章还提供了测试方法及总结,帮助读者掌握ActiveMQ在异步消息处理中的应用。
477 0
|
10月前
|
消息中间件 网络协议 Java
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——ActiveMQ集成
本文介绍了在 Spring Boot 中集成 ActiveMQ 的详细步骤。首先通过引入 `spring-boot-starter-activemq` 依赖并配置 `application.yml` 文件实现基本设置。接着,创建 Queue 和 Topic 消息类型,分别使用 `ActiveMQQueue` 和 `ActiveMQTopic` 类完成配置。随后,利用 `JmsMessagingTemplate` 实现消息发送功能,并通过 Controller 和监听器实现点对点消息的生产和消费。最后,通过浏览器访问测试接口验证消息传递的成功性。
715 0
|
10月前
|
消息中间件 Java API
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ—— JMS 和 ActiveMQ 介绍
本文介绍如何在Spring Boot中集成ActiveMQ,首先阐述了JMS(Java消息服务)的概念及其作为与具体平台无关的API在异步通信中的作用。接着说明了JMS的主要对象模型,如连接工厂、会话、生产者和消费者等,并指出JMS支持点对点和发布/订阅两种消息类型。随后重点讲解了ActiveMQ,作为Apache开源的消息总线,它完全支持JMS规范,适用于异步消息处理。最后,文章探讨了在Spring Boot中使用队列(Queue)和主题(Topic)这两种消息通信形式的方法。
370 0
|
消息中间件 Java 测试技术
53分布式电商项目 - Spring集成ActiveMQ
53分布式电商项目 - Spring集成ActiveMQ
98 0
|
消息中间件 Java Spring
Spring Cloud 2.x系列之springboot集成ActiveMQ
消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量削锋等问题。实现高性能,高可用,可伸缩和最终一致性架构;是大型分布式系统不可缺少的中间件。目前使用较多的消息队列有ActiveMQ、RabbitMQ、Kafka、RocketMQ、MetaMQ等。
4322 0
|
消息中间件 Java Spring
|
消息中间件 网络协议 Java