Spring整合activeMQ(1)

1.点对点模式

1.1消息的生产者

(1)创建普通的maven工程 springjms_producer,在 POM 文件中引入 SpringJms 、activeMQ 以及单元测
试相关依赖

<spring.version>4.2.4.RELEASE</spring.version>



org.springframework
spring-jms
s p r i n g . v e r s i o n &lt; / v e r s i o n &gt; &lt; / d e p e n d e n c y &gt; &lt; d e p e n d e n c y &gt; &lt; g r o u p I d &gt; o r g . s p r i n g f r a m e w o r k &lt; / g r o u p I d &gt; &lt; a r t i f a c t I d &gt; s p r i n g − t e s t &lt; / a r t i f a c t I d &gt; &lt; v e r s i o n &gt; {spring.version}&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-test&lt;/artifactId&gt; &lt;version&gt; spring.version</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>springtest</artifactId><version>{spring.version}


junit
junit
4.9


org.apache.activemq
activemq-client
5.13.4

activemq-client这个依赖jar包我自己手打不出来,复制就可以0.0......

(2)在 src/main/resources 下创建 spring 配置文件 applicationContext-jms-producer.xml

<?xml version="1.0" encoding="UTF-8"?>

<!--刚复制后这里会报错,因为我们还没有创建"com.study.demo"包-->
<context:component-scan base-package="com.study.demo"></context:component-scan>


<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <!--这里的tcp连接地址根据你自己的来写-->
    <property name="brokerURL" value="tcp://192.168.25.128:61616"/>
</bean>

<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
    <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
    <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
</bean>

<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
    <property name="connectionFactory" ref="connectionFactory"/>
</bean>
<!--这个是队列目的地,点对点的  文本信息-->
<bean id="activeMQQueue" class="org.apache.activemq.command.ActiveMQQueue">
    <constructor-arg value="queue"/>
</bean>

我的xmlns:amq="http://activemq.apache.org/schema/core"这部分在idea中一直报红(URI is not registered),实际上并不影响程序的运行.
如果看着比较烦人的话,当然我们也可以解决这个问题,如下图解决方法.
配置URI报红的解决方法
( 3 )在com.study.demo包下面创建QueueProducer生产者

package com.study.demo;

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

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

@Component
public class QueueProducer {
    @Autowired
    private JmsTemplate jmsTemplate;
    @Autowired
    private Destination activeMQQueue;//对应于配置文件中的队列目的地id
    /**
     * 发送文本消息
     * @param text
     */
    public void sendTextMessage(final String text){
        jmsTemplate.send(activeMQQueue, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(text);
            }
        });
    }

}

( 4 )单元测试
在 src/test/java 创建测试类

import com.study.demo.QueueProducer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-jms-producer.xml")
public class TestQueue {
    @Autowired
    private QueueProducer queueProducer;
    @Test
    public void testSend(){
        queueProducer.sendTextMessage("SpringJms-点对点模式");
    }
}
1.2消息的消费者

( 1 )创建普通的maven工程 springjms_consumer,在 POM 文件中引入依赖,和上面一样
(2)创建配置文件 applicationContext-jms-consumer-queue.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="tcp://192.168.25.128:61616"/>
</bean>

<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
    <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
    <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
</bean>

<!--这个是队列目的地,点对点的  文本信息-->
<bean id="activeMQQueue" class="org.apache.activemq.command.ActiveMQQueue">
    <!--注意这里的value一定要和生产者的一样,这样才能监听到消息-->
    <constructor-arg value="queue"/>
</bean>

<!-- 我的监听类 -->
<bean id="myMessageListener" class="com.study.demo.MyMessageListener"></bean>


<!-- 消息监听容器 -->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref="connectionFactory" />
    <property name="destination" ref="activeMQQueue" />
    <property name="messageListener" ref="myMessageListener" />
</bean>

( 3 )编写监听类

package com.study.demo;

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

public class MyMessageListener implements MessageListener{

    public void onMessage(Message message) {

        TextMessage textMessage = (TextMessage)message;
        try {
            System.out.println("接收到消息:"+textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

}

( 4 )单元测试

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.IOException;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-jms-consumer-queue.xml")
public class TestQueue {
    @Test
    public void testQueue() {
        try {
            //此处为了方便测试,让程序不往下面执行
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

当你生产者单元测试执行了一次后,消费者也执行单元测试会看到如下的结果.
在这里插入图片描述
则说明消费者成功的接受到了消息.

写到这里为了不让篇幅过长,所以发布订阅模式下篇文章再写.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值