Spring 整合ActiveMQ消息中间件服务

  • pom.xml
<dependencies>
        <!--ActiveMQ对JMS的支持,Spring对ActiveMQ支持-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>4.3.23.RELEASE</version>
        </dependency>
        <!--ActiveMQ依赖-->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.15.9</version>
        </dependency>
    </dependencies>
  • spring-activemq-producer.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:context="http://www.springframework.org/schema/context"
       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">

    <!--Message提供者-->
    <!-- 扫描activemq包 -->
    <context:component-scan base-package="com.hf.activemq.**"/>

    <!-- 1.ActiveMQ的连接工厂 -->
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://192.168.44.113:61616"/>
    </bean>

    <!-- 2.Spring Caching连接工厂 -->
    <bean id="connectionFactory"
          class="org.springframework.jms.connection.CachingConnectionFactory">
        <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
        <property name="sessionCacheSize" value="100"/>
    </bean>

    <!--3 消息配置-->
    <!-- 3.1. Queues消息配置 -->
    <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg index="0" value="hf_queue_02"/>
    </bean>

    <!-- 3.1. topics消息配置 -->
    <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg index="0" value="hf_topic_02"/>
    </bean>
    
    <!-- 4.配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 -->
    <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="receiveTimeout" value="10000"/>
        <property name="sessionTransacted" value="false"/>

        <!--消息发送类型-->
        <property name="sessionAcknowledgeModeName" value="AUTO_ACKNOWLEDGE"/>
    </bean>

</beans>
  • spring-activemq-consumer.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Msg消费者 -->
    <!-- 扫描activemq包 -->
    <context:component-scan base-package="com.hf.activemq.**"/>

    <!-- 1.ActiveMQ的连接工厂 -->
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://192.168.44.121:61616"/>
    </bean>

    <!-- 2.Spring Caching连接工厂 -->
    <bean id="connectionFactory"
          class="org.springframework.jms.connection.CachingConnectionFactory">
        <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
        <property name="sessionCacheSize" value="100"/>
    </bean>

    <!--3 消息配置【queues/topics】-->
    <!-- 3.1. Queues消息配置 -->
    <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg index="0" value="hf_queue_02"/>
    </bean>

    <!-- 3.1. topics消息配置 -->
    <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg index="0" value="hf_topic_02"/>
    </bean>


    <!-- 4.配置消息监听器-->

    <!-- 消息监听容器【queues】 -->
    <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="destination" ref="queueDestination"/>

        <!--queues监听类-->
        <property name="messageListener" ref="isQueueMessageListener"/>
    </bean>


    <!-- 消息监听容器【topics】 -->
    <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="destination" ref="topicDestination"/>

        <!--queues监听类-->
        <property name="messageListener" ref="isTopicMessageListener"/>
    </bean>

</beans>
  • isQueueMessageListener【queues消息监听】
package com.hf.activemq.listener;

import org.springframework.stereotype.Component;

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

/**
 * @Copyright (C), 2016-2019 hf
 * @FileName: IsQueueMessageListener
 * @Author: hf
 * @Date: 2019/10/15 20:24
 * @Description: Spring 整合ActiveMQ queue消息接收
 */
@Component
public class IsQueueMessageListener implements MessageListener {

    @Override
    public void onMessage(Message message) {
        if (message instanceof TextMessage) {

            TextMessage textMessage = (TextMessage) message;

            System.out.println("Queue接收到消息:" + textMessage);
        }
    }
}

  • isTopicMessageListener【topics消息监听】
package com.hf.activemq.listener;

import org.springframework.stereotype.Component;

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

/**
 * @Copyright (C), 2016-2019 hf
 * @FileName: IsQueueMessageListener
 * @Author: hf
 * @Date: 2019/10/15 20:24
 * @Description: Spring 整合ActiveMQ topic消息接收
 */
@Component
public class IsTopicMessageListener implements MessageListener {

    @Override
    public void onMessage(Message message) {
        if (message instanceof TextMessage) {

            TextMessage textMessage = (TextMessage) message;

            System.out.println("topic接收到消息:" + textMessage);
        }
    }
}

  • 消息发送示例(queues/topics)
package com.hf.activemq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import javax.jms.Destination;

/**
 * @Copyright (C), 2016-2019 hf
 * @FileName: SpringMQ_Producer
 * @Author: hf
 * @Date: 2019/10/15 19:52
 * @Description: Spring 整合Queues消息生产者
 */
@Service
public class SpringMQ_Producer {

    @Autowired
    JmsTemplate jmsTemplate;

    @Autowired
    Destination queueDestination, topicDestination;


    public void sendQueueMessage() {
        jmsTemplate.send(queueDestination, session -> session.createTextMessage("Spring整合ActiveMQ 发送queue消息"));
        System.out.println("queues消息已经发送....");
    }

    public void sendTopicMessage() {
        jmsTemplate.send(queueDestination, session -> session.createTextMessage("Spring整合ActiveMQ 发送topics消息"));
        System.out.println("topics消息已经发送....");
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值