24、springboot集成ActiveMQ

絮语闭包
• 阅读 1167

消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量削锋等问题。实现高性能,高可用,可伸缩和最终一致性架构;是大型分布式系统不可缺少的中间件。目前使用较多的消息队列有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-domain为true

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-domain为false
24、springboot集成ActiveMQ
(2)注释测试类的如下代码
24、springboot集成ActiveMQ
(3)运行测试类
24、springboot集成ActiveMQ

订阅模式:
(1)配置spring.jms.pub-sub-domain为true
24、springboot集成ActiveMQ
(2)注释测试类的如下代码
24、springboot集成ActiveMQ
(3)运行测试类
24、springboot集成ActiveMQ

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
6个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Wesley13 Wesley13
3年前
MQ消息中间件,面试能问些什么?
MQ消息中间件,面试能问些什么?为什么使用消息队列?消息队列的优点和缺点?kafka、activemq、rabbitmq、rocketmq都有什么优缺点?面试官角度分析:(1)你知不知道你们系统里为什么要用消息队列这个东西?(2)既然用了消息队列这个东西,你知不知道用了有什么好处?(3
Stella981 Stella981
3年前
RabbitMQ 消息中间件搭建详解
1.RabbitMQ简介消息中间件也可以称消息队列,是指用高效可靠的消息传递机制进行与平台无关的数据交流,并基于数据通信来进行分布式系统的集成。通过提供消息传递和消息队列模型,可以在分布式环境下扩展进程的通信。RabbitMQ是使用Erlang语言开发的开源消息队列系统,基于AMQP协议来实现。AMQP的主要特征是面向消息、队列、路由(包
Stella981 Stella981
3年前
Message Queue消息队列基本原理
消息队列基本原理📦本文已归档到:「blog」消息队列(MessageQueue,简称MQ)技术是分布式应用间交换信息的一种技术。消息队列主要解决应用耦合,异步消息,流量削锋等问题,实现高性能,高可用,可伸缩和最终一致性架构。是大型分布式系统不可缺少的中间件。注意:_为了简便,下文中除了文章标
Wesley13 Wesley13
3年前
activeMQ入门+spring boot整合activeMQ
最近想要学习MOM(消息中间件:MessageOrientedMiddleware),就从比较基础的activeMQ学起,rabbitMQ、zeroMQ、rocketMQ、Kafka等后续再去学习。上面说activeMQ是一种消息中间件,可是为什么要使用activeMQ?在没有使用JMS的时候,很多应用会出现同步通信(客户端发起请求后需要等待服务
专为小白打造—Kafka一篇文章从入门到入土 | 京东云技术团队
一、什么是KafkaMQ消息队列作为最常用的中间件之一,其主要特性有:解耦、异步、限流/削峰。Kafka和传统的消息系统(也称作消息中间件)都具备系统解耦、冗余存储、流量削峰、缓冲、异步通信、扩展性、可恢复性等功能。与此同时,Kafka还提供了大多数消息系