ActiveMQ的简单例子应用

ActiveMQ是一种消息中间件,它实现了JMS规范,提供点对点和订阅-发布两种模式。下面介绍下ActiveMQ的使用;

一、环境的搭建

首先我们需要下载ActiveMQ的安装包,下载地址http://activemq.apache.org/activemq-510-release.html。

直接解压并拷贝到C盘中。最终的目录为:C:Program FilesActiveMQ。

下面就是启动ActiveMQ了,方法是进入到bin目录下,win32目录下,找到activemq.bat并双击;

然后便可以启动ActiveMQ了,启动的界面如下图所示;

然后在浏览器中输入如下地址http://localhost:8161/admin/ (用户名:admin,密码:damin)便可以进入到ActiveMQ的控制台中了。

下面就是代码的创建了。

 二、项目代码

1、maven依赖包:

<!-- ActiveMQ的依赖 -->
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-core</artifactId>
    <version>5.5.0</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-client</artifactId>
    <version>5.13.1</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-pool</artifactId>
    <version>5.13.1</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-broker</artifactId>
    <version>5.13.1</version>
</dependency>

<!-- ActiveMQ+Spring的依赖 -->
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-spring</artifactId>
    <version>5.12.2</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>4.1.9.RELEASE</version>
</dependency>

2、ActiveMQ和Spring的配置文件:

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-2.5.xsd"
    default-autowire="byName">

    <!--第一步: 配置connectionFactory -->  
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
    	<property name="brokerURL" value="tcp://localhost:61616"/> 
    	<property name="userName"  value="admin"/>
    	<property name="password"  value="admin"/>  
	</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>  

    <!--第二步:配置JmsTemplate之消息生产者 -->
 	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
    	<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->  
    	<property name="connectionFactory" ref="connectionFactory"/>  
	</bean>  
	<!--队列目的地,点对点-->  
	<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">  
    	<constructor-arg value="queue"/>  
	</bean>  
	<!--主题目的地,订阅发布-->  
	<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">  
    	<constructor-arg value="topic"/> 
     </bean>  
    <!--第三步:配置JmsTemplate之消息消费者 -->
      <!-- 消息监听器 -->  
    <bean id="consumerMessageListener" class="com.zte.ems.springjms.listener.ConsumerMessageListener"/> <!-- com.zte.ems.springjms.listener.ConsumerMessageListener这个类是自己创建的,实现MessageListener 接口 --> 
    <!-- 消息监听容器 -->  
    <bean id="jmsContainer"  class="org.springframework.jms.listener.DefaultMessageListenerContainer">  
        <property name="connectionFactory" ref="connectionFactory" />  
        <property name="queue_destination" ref="queueDestination" />
        <property name="topic_destination" ref="topicDestination" />  
        <property name="messageListener" ref="consumerMessageListener" />  
    </bean>  
</beans>

 说明:

1)第一步:配置ConnectionFactory

  ConnectionFactory是用于产生到JMS服务器的链接的,Spring为我们提供了多个ConnectionFactory,有SingleConnectionFactory和CachingConnectionFactory。SingleConnectionFactory对于建立JMS服务器链接的请求会一直返回同一个链接,并且会忽略Connection的close方法调用。CachingConnectionFactory继承了SingleConnectionFactory,所以它拥有SingleConnectionFactory的所有功能,同时它还新增了缓存功能,它可以缓存Session、MessageProducer和MessageConsumer。这里我们使用SingleConnectionFactory来作为示例。Spring提供的ConnectionFactory只是Spring用于管理ConnectionFactory的,真正产生到JMS服务器链接的ConnectionFactory还得是由JMS服务厂商提供,并且需要把它注入到Spring提供的ConnectionFactory中。我们这里使用的是ActiveMQ实现的JMS,所以在我们这里真正的可以产生Connection的就应该是由ActiveMQ提供的ConnectionFactory。 ActiveMQ为我们提供了一个PooledConnectionFactory,通过往里面注入一个ActiveMQConnectionFactory可以用来将Connection、Session和MessageProducer池化,这样可以大大的减少我们的资源消耗。

2)第二步:配置消息生成者

  配置好ConnectionFactory之后我们就需要配置生产者。生产者负责产生消息并发送到JMS服务器,这通常对应的是我们的一个业务逻辑服务实现类。但是我们的服务实现类是怎么进行消息的发送的呢?这通常是利用Spring为我们提供的JmsTemplate类来实现的,所以配置生产者其实最核心的就是配置进行消息发送的JmsTemplate。对于消息发送者而言,它在发送消息的时候要知道自己该往哪里发,为此,我们在定义JmsTemplate的时候需要往里面注入一个Spring提供的ConnectionFactory对象。在真正利用JmsTemplate进行消息发送的时候,我们需要知道消息发送的目的地,即destination。在Jms中有一个用来表示目的地的Destination接口,它里面没有任何方法定义,只是用来做一个标识而已。当我们在使用JmsTemplate进行消息发送时没有指定destination的时候将使用默认的Destination。默认Destination可以通过在定义jmsTemplate bean对象时通过属性defaultDestination或defaultDestinationName来进行注入,defaultDestinationName对应的就是一个普通字符串。在ActiveMQ中实现了两种类型的Destination,一个是点对点的ActiveMQQueue,另一个就是支持订阅/发布模式的ActiveMQTopic。在定义这两种类型的Destination时我们都可以通过一个name属性来进行构造。

3)配置消息消费者

  生产者往指定目的地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环境。

3.1)定义处理消息的MessageLisenter

package com.zte.ems.springjim.listener;
import javax.jms.Message;
import javax.jms.MessageListener;
public class ConsumerMessageListener implements MessageListener {
    public void onMessage(Message arg0) {
        // 这里我们就是监听生产者发送过来的消息,然后进行处理
    }
}

 3.2)消息监听器容器:

  消息监听器容器(message listener container)是一个特殊的bean,它可以监控JMS目的地并等待消息到达。一旦有消息到达,它就取出消息,然后把消息传递给任意一个对此消息感兴趣的消息监听器。

原文地址:https://www.cnblogs.com/wrong5566/p/6033094.html