您的位置:首页 > 编程语言 > Java开发

Spring整合ActiveMQ

2016-11-24 22:22 405 查看

一,加入pom配置

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
</dependencies>


另外,因为在最后运行的时候,意外报了个:java.lang.NoClassDefFoundError: javax/transaction/TransactionManager 。所以,pom里面还需加入:
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>

二,定义生产者

/**
*  * 功能:消息的生产者接口,定义发送消息的规范
* Created by liuhuichao on 2016/11/24.
*/
public interface ProducerService {
void sendMessage(Destination destination, final String message);
}

实现类:
/**
* 功能:消息的生产者实现类
* Created by liuhuichao on 2016/11/24.
*/
public class ProducerServiceImpl implements ProducerService {

@Resource
private JmsTemplate jmsTemplate;

@Override
public void sendMessage(Destination destination, final String message) {
System.out.println("--------生产者发送了一个消息--------");
System.out.println("生产者发送的消息内容为:"+message);

jmsTemplate.send(destination, new MessageCreator() {//通过jmsTemplate发送消息到目的消费者
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(message);
}
});

}
}


三,spring配置及含义

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

<!--
ConnectionFactory是用于产生到JMS服务器的链接的,spring为我们提供了多个ConnectionFactory,有SingleConnectionFactory和CachingConnectionFactory
SingleConnectionFactory对于建立JMS服务链接的请求会一直返回同一个链接;并且忽略Connection的close方法调用。
CachingConnectionFactory继承了SingleConnectionFactory,它拥有SingleConnectionFactory的所有功能;同时它还新增了缓存功能,它可以缓存Session、MessageProducer和MessageConsumer。
-->

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

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

<!--
ActiveMQ为我们提供了一个PooledConnectionFactory,通过往里面注入一个ActiveMQConnectionFactory可以用来将Connection、Session和MessageProducer池化,
这样可以大大的减少我们的资源消耗。当使用PooledConnectionFactory时,我们在定义一个ConnectionFactory时应该是如下定义:
-->
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"/>
</bean>

<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
<property name="connectionFactory" ref="targetConnectionFactory"/>
<property name="maxConnections" value="10"/>
</bean>

<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="pooledConnectionFactory"/>
</bean>

<!--
*******************************配置生产者*************************************
Spring提供的JMS工具类,它可以进行消息发送、接收等
********************************************************************
-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<property name="connectionFactory" ref="connectionFactory"/>
</bean>

<!--

在真正利用JmsTemplate进行消息发送的时候,我们需要知道消息发送的目的地,即destination。在Jms中有一个用来表示目的地的Destination接口,
它里面没有任何方法定义,只是用来做一个标识而已。当我们在使用JmsTemplate进行消息发送时没有指定destination的时候将使用默认的Destination。
默认Destination可以通过在定义jmsTemplate bean对象时通过属性defaultDestination或defaultDestinationName来进行注入,
defaultDestinationName对应的就是一个普通字符串。在ActiveMQ中实现了两种类型的Destination,一个是点对点的ActiveMQQueue,
另一个就是支持订阅/发布模式的ActiveMQTopic。在定义这两种类型的Destination时我们都可以通过一个name属性来进行构造,如:

-->
<!--这个是队列目的地,点对点的-->
<!--<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>queue</value>
</constructor-arg>
</bean>-->
<!--这个是主题目的地,一对多的-->
<!--<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic"/>
</bean>-->

<!--
********************************************************************
配置消息监听器
********************************************************************
-->
<!--这个是队列目的地-->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>queue</value>
</constructor-arg>
</bean>
<!-- 消息监听器 -->
<bean id="consumerMessageListener" class="test.JmsTest.ConsumerMessageListener"/>

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

<!--配置消息生产者-->
<bean id="producerService" class="test.JmsTest.ProducerServiceImpl"></bean>

</beans>


四,定义消费者

生产者往指定目的地Destination发送消息后,接下来就是消费者对指定目的地的消息进行消费了。那么消费者是如何知道有生产者发送消息到指定目的地Destination了呢?这是通过Spring为我们封装的消息监听容器MessageListenerContainer实现的,它负责接收信息,并把接收到的信息分发给真正的MessageListener进行处理。每个消费者对应每个目的地都需要有对应的MessageListenerContainer。对于消息监听容器而言,除了要知道监听哪个目的地之外,还需要知道到哪里去监听,也就是说它还需要知道去监听哪个JMS服务器,这是通过在配置MessageConnectionFactory的时候往里面注入一个ConnectionFactory来实现的。

所以我们在配置一个MessageListenerContainer的时候有三个属性必须指定,一个是表示从哪里监听的ConnectionFactory;一个是表示监听什么的Destination;一个是接收到消息以后进行消息处理的MessageListener。Spring一共为我们提供了两种类型的MessageListenerContainer,SimpleMessageListenerContainer和DefaultMessageListenerContainer。

SimpleMessageListenerContainer会在一开始的时候就创建一个会话session和消费者Consumer,并且会使用标准的JMS MessageConsumer.setMessageListener()方法注册监听器让JMS提供者调用监听器的回调函数。它不会动态的适应运行时需要和参与外部的事务管理。兼容性方面,它非常接近于独立的JMS规范,但一般不兼容Java EE的JMS限制。

大多数情况下我们还是使用的DefaultMessageListenerContainer,跟SimpleMessageListenerContainer相比,DefaultMessageListenerContainer会动态的适应运行时需要,并且能够参与外部的事务管理。它很好的平衡了对JMS提供者要求低、先进功能如事务参与和兼容Java EE环境。

/**
* 功能:定义消息处理的Listener
*   要定义处理消息的MessageListener我们只需要实现JMS规范中的MessageListener接口就可以了。
*   MessageListener接口中只有一个方法onMessage方法,当接收到消息的时候会自动调用该方法。
* Created by liuhuichao on 2016/11/24.
*/
public class ConsumerMessageListener implements MessageListener {
@Override
public void onMessage(Message message) {
TextMessage textMessage=(TextMessage)message;
System.out.println("接收到了一个纯文本的消息");

try {
System.out.println("消息的内容为:"+ textMessage.getText());
} catch (JMSException e) {
System.out.println("消息接收出现异常!");
e.printStackTrace();
}

}
}

五,测试消息生产与消费

/**
* 测试消息队列
* Created by liuhuichao on 2016/11/24.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/applicationContext-activeMQ.xml"})
public class ProducerConsumerTest {

@Autowired
private ProducerService producerService;
@Autowired
@Qualifier("queueDestination")
private Destination destination;

@Test
public void sendMsg(){
for (int i=0;i<2;i++){
producerService.sendMessage(destination,"["+i+"]");
}
}
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: