第一步

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


        <!-- 主题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>

</beans>

列队和订阅不同之处只需在applicationContext.xml配置文件中修改一下即可
pring整合ActiveMQ点对点模式和主题模式的区别.png

第三步

编写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);
    }
}

第四步

编写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 发送消息完毕!");

    }

}
Last modification:July 19, 2019
如果觉得这篇技术文章对你有用,请随意赞赏