宋子宪博客

Szx

Spring整合ActiveMQ设置监听器
引入maven 用到的jar包依赖<dependencies> <depend...
扫描右侧二维码阅读全文
19
2019/07

Spring整合ActiveMQ设置监听器

引入maven 用到的jar包依赖

<dependencies>

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

        <!-- ActiveMQ对jms的支持,整合Spring和ActiveMQ -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>5.1.5.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>5.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>

    </dependencies>

编写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: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/tx/spring-tx.xsd">

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

        <!-- 配置生产者 -->
        <bean id="jsmFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
            <!-- 真正可以生成Connection的ConnectionFactory,由对应的JMS服务器产商提供 -->
            <property name="connectionFactory">
                <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                    <property name="brokerURL" value="tcp://192.168.78.138:61616"></property>
                </bean>
            </property>

            <property name="maxConnections" value="100"></property>
        </bean>

        <!-- 这个是队列的目的地 点对点 -->
        <!-- 主题模式中列队不需要配置,配置了也不影响-->
        <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
            <constructor-arg index="0" value="spring-active-queue"/>
        </bean>


        <!-- 主题Topic 发布订阅 -->
        <bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
            <constructor-arg index="0" value="spring-active-topic"/>
        </bean>


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

            <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="jsmFactory"></property>

            <!-- 配置是监听目的地是 列队qoeue还是主题topic -->
            <property name="destination" ref="destinationTopic"></property>

            <!-- 引入myMessageListener类 -->
            <property name="messageListener" ref="myMessageListener"></property>
        </bean>    
</beans>

Spring整合ActiveMQ设置监听器说明图.png

编写监听类

/**
 * @author songzixian
 * @create 2019-07-19 下午 7:03
 * @description
 */
@Component
public class MyMessageListener implements javax.jms.MessageListener {

    @Override
    public void onMessage(Message message) {
        //判断message是否为空,message类型是否为text
        if(message != null && message instanceof Text){
            TextMessage textMessage = (TextMessage) message;
            try {
                System.out.println(textMessage.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
}

编写消息生成者,直接运行消费者即可自动消费

/**
 * @author songzixian
 * @create 2019-07-19 下午 5:39
 * @description Spring整合ActiveMQ消息生产者
 */
@Service
public class SpringMQ_Produce {

    //注入jmsTemplate
    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args) {

        //1.加载配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        //2.获得bean
        SpringMQ_Produce produce = (SpringMQ_Produce) applicationContext.getBean("springMQ_Produce");

        //发送消息
        produce.jmsTemplate.send((session) -> {
            //创建mq文本消息
            TextMessage textMessage = session.createTextMessage("songzixian.com spring整合activemq 监听器");

            return textMessage;

        });

        System.out.println("songzixian.com spring整合activemq 监听器消息 发送消息完毕!");

    }

}

效果图
Spring整合ActiveMQ设置监听器效果图.png

Last modification:July 19th, 2019 at 07:31 pm
如果觉得这篇技术文章对您有帮助,可以请博主喝一杯饮料

Leave a Comment