Spring整合ActiveMQ列队Queue小Demo

2019-07-19T17:27:00

第一步

引入maven 用到的jar包依赖

<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>

第二步

编写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>

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

</beans>

第三步

编写Spring整合ActiveMQ消息生产者类

/**
 * @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("spring整合activemq 发送消息完毕!");

    }

}

控制台输入图

第四步

编写Spring整合ActiveMQ消息消费者类

/**
 * @author songzixian
 * @create 2019-07-19 下午 5:58
 * @description Spring整合ActiveMQ消息消费者
 */
@Service
public class SpringMQ_Consumer {

    @Autowired
    private JmsTemplate jmsTemplate;

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

        SpringMQ_Consumer consumer = (SpringMQ_Consumer) applicationContext.getBean("springMQ_Consumer");

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

        System.out.println("消费者接收到的消息"+reValue);
    }
}

ActiveMQ页面显示图:

当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »