ActiveMQ —— Spring 整合 ActiveMQ

前文

消息中间件 —— 简介

ActiveMQ 下载、安装

ActiveMQ —— Java 连接 ActiveMQ(点对点)

ActiveMQ —— Java 连接 ActiveMQ(发布订阅 Topic)

ActiveMQ —— Broker

添加依赖

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.9.5</version>
</dependency>

<!-- ActiveMQ 对 JMS 支持,整合 Spring 和 ActiveMQ -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jms</artifactId>
  <version>4.3.23.RELEASE</version>
</dependency>

<!-- ActiveMQ 所需要的 pool 包配置 -->
<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-pool</artifactId>
  <version>5.15.9</version>
</dependency>

<!-- Spring AOP 等相关 Jar 包 -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>4.3.25.RELEASE</version>
</dependency>

这里的 Spring-core 有版本问题,使用 5.0+ 的整合 ActiveMQ 会报错,使用 4.0 + 的就不会

Spring 配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/beans">

    <!-- 开启包的自动扫描 -->
    <context:component-scan base-package="com.java.elasticsearch.activemq"/>

    <!-- 配置生产者 -->
    <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
        <property name="connectionFactory">
            <!-- 真正可以产生 Connection 的 ConnectionFactory,由对应的 JMS 服务厂商提供 -->
            <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL" value="tcp://localhost:61616"/>
            </bean>
        </property>
        <!-- 最大连接数 -->
        <property name="maxConnections" value="100"/>
    </bean>

    <!-- 这个是队列的目的地,点对点的 -->
    <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
    	<!-- 队列名称 -->
        <constructor-arg index="0" value="spring-active-queue"/>
    </bean>

	<!-- Spring 提供的 JMS 工具类,它可以进行消息发送,接收等 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="jmsFactory"/>
        <property name="defaultDestination" ref="destinationQueue"/>
        <property name="messageConverter">
            <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
        </property>
     </bean>

</beans>

队列

生产者

package com.java.elasticsearch.activemq.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * @author Woo_home
 * @create 2020/5/22 14:09
 */

@Service
public class SpringMQ_Produce {

    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 相当于 new 一个 SpringMQ_Produce,但是这里用的是 Spring,所以这里就不用 new 了
        SpringMQ_Produce produce = (SpringMQ_Produce) ctx.getBean("springMQ_Produce");

        // 普通写法
        /*produce.jmsTemplate.send(new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage("****** Spring 整合 ActiveMQ ******");
                return textMessage;
            }
        });*/

        // lambda 写法
        produce.jmsTemplate.send((session) -> {
            TextMessage textMessage = session.createTextMessage("****** Spring 整合 ActiveMQ ******");
            return textMessage;
        });
    }
}

执行程序之前先启动 ActiveMQ
在这里插入图片描述
执行主程序
在这里插入图片描述
刷新 admin 页面,队列已成功添加进来
在这里插入图片描述

消费者

消费者端代码非常简单,只需要接收就行了

package com.java.elasticsearch.activemq.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import javax.jms.TextMessage;

/**
 * @author Woo_home
 * @create 2020/5/22 14:09
 */

@Service
public class SpringMQ_Consumer {

    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 相当于 new 一个 SpringMQ_Consumer,但是这里用的是 Spring,所以这里就不用 new 了
        SpringMQ_Consumer consumer = (SpringMQ_Consumer) ctx.getBean("springMQ_Consumer");

        String retValue  = (String) consumer.jmsTemplate.receiveAndConvert();

        System.out.println("****** 消费者收到消息 : " + retValue);
    }
}

执行程序
在这里插入图片描述
刷新 admin 页面
在这里插入图片描述

主题

修改 applicationContext.xml 文件

只需要将以下三处修改为 Topic 即可
在这里插入图片描述
生产者与消费者的代码不用修改,直接运行,要是你想修改的话,可以修改如下内容
在这里插入图片描述
在这里插入图片描述
因为这里使用的是 Topic,我们先启动消费端,此时的消费端还没收到信息
在这里插入图片描述
在这里插入图片描述
再启动生产端
在这里插入图片描述
在这里插入图片描述
此时,消费端接收到生产者发来的信息
在这里插入图片描述

在 Spring 里面实现消费者不启动,直接通过配置监听完成

修改 applicationContext.xml 文件

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/beans">

    <!-- 开启包的自动扫描 -->
    <context:component-scan base-package="com.java.elasticsearch.activemq"/>

    <!-- 配置生产者 -->
    <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
        <property name="connectionFactory">
            <!-- 真正可以产生 Connection 的 ConnectionFactory,由对应的 JMS 服务厂商提供 -->
            <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL" value="tcp://localhost:61616"/>
            </bean>
        </property>
        <!-- 最大连接数 -->
        <property name="maxConnections" value="100"/>
    </bean>

    <!-- 这个是队列的目的地,点对点的 -->
    <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
        <!-- 队列名称 -->
        <constructor-arg index="0" value="spring-active-queue"/>
    </bean>

    <!-- 这个是队列的目的地,主题的 -->
    <bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
        <!-- 队列名称 -->
        <constructor-arg index="0" value="spring-active-topic"/>
    </bean>

    <!-- Spring 提供的 JMS 工具类,它可以进行消息发送,接收等 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="jmsFactory"/>
        <property name="defaultDestination" ref="destinationTopic"/>
        <property name="messageConverter">
            <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
        </property>
     </bean>

    <!-- 配置监听程序 -->
    <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="jmsFactory"/>
        <property name="destination" ref="destinationTopic"/>
        <!-- public class MyMessageListener implements MessageListener {} -->
        <property name="messageListener" ref="myMessageListener"/>
    </bean>

</beans>

配置监听
在这里插入图片描述
这里还需要一个组件

package com.java.elasticsearch.activemq.service;

import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * @author Woo_home
 * @create 2020/5/22 14:59
 */

@Component
public class MyMessageListener implements MessageListener {
    @Override
    public void onMessage(Message message) {
        if (null != message && message instanceof TextMessage) {
            TextMessage textMessage = (TextMessage) message;
            try {
                System.out.println(textMessage.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
}

先删除 Topic
在这里插入图片描述
启动生产者,可以发现,消费者马上就可以监听到消费者发送的消息
在这里插入图片描述
在这里插入图片描述


完整代码已上传至码云 代码下载地址

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值