您的位置:首页 > 运维架构 > Apache

Apache-ActiveMQ整合Spring

2016-03-21 09:11 751 查看

1.1关于activemq

在J2EE规范中,JMS有一席之地,但JMS只是一种规范一种概念,落地地实施JMS有很多产品,其中apache-activemq是一种比较常用的产品.ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。

2 准备工作

2.1 下载并安装activemq

下载apache-activemq:http://activemq.apache.org/download.html

这里下载linux版本:apache-activemq-5.7.0-bin.tar.gz




把apache-activemq-5.7.0-bin.tar.gz解压到一个目录中,会有一个apache-activemq-5.7.0的文件夹,进入该文件夹的bin目录,将看到activemq的脚本文件,执行该文件: ./activemq

若无法执行,可能是权限不够,需要修改权限: chmod 755 activemq

执行该文件后在终端如果看到下面信息,者启动成功



此时activemq线程已经运行,这时候不要关闭终端,否则activemq也会终止,如果想在后台运行activemq,执行一下命令:

nohup ./activemq start

3 实施项目

3.1 创建项目

新建maven项目,并声明项目依赖:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>ActivityMQ_Spring</groupId>
<artifactId>ActivityMQ_Spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>ActivityMQ_Spring</name>
<description/>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<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>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.6.RELEASE</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>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<version>3.0</version>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>


配置Spring框架:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ActivityMQ_Spring</display-name>

<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 框架配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>
</web-app>


3.2 创建消息发送者:

package com.lin.activity.intf;

public interface ProductService {

public void sendMessage(javax.jms.Destination destination, final String message);

}


package com.lin.activity.service;

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

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import com.lin.activity.intf.ProductService;

/**
* 消息发送组件是一个POJO,通过借助Spring的JmsTemplate封装发送目的destination和消息发送消息
* @author lin
*
*/
public class ProductServiceImpl implements ProductService {

private JmsTemplate jmsTemplate;

public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}

@Override
public void sendMessage(javax.jms.Destination destination,final String message) {

/**
* 发送消息,这里使用一个内部类实现,内部类引用的外层类的属性值需要被定义为final
*/
jmsTemplate.send(destination, new MessageCreator() {

@Override
public Message createMessage(Session session) throws JMSException {

return session.createTextMessage(message);
}
});
}
}


3.3 创建消息接受者:

package com.lin.activity.service;

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

/**
* 定义处理消息的MessageListener
* 要定义处理消息的MessageListener我们只需要实现JMS规范中的MessageListener接口就可以了。
* MessageListener接口中只有一个方法onMessage方法,当接收到消息的时候会自动调用该方法。
* @author lin
*
*/
public class CostumerListener implements MessageListener {

/**
* 该方法监听发送方是否发送消息,若发送者处理消息
*/
@Override
public void onMessage(Message message) {

TextMessage textMsg = (TextMessage) message;
System.out.println("接受者接受到一个文本消息......");
try {
System.out.println("消息内容:" + textMsg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}

}


3.4 实现测试组件

package com.lin.activity.test;

import javax.jms.Destination;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.lin.activity.intf.ProductService;

/**
* Spring组件测试类
* @author lin
*/
//配置Spring组件运行时
@RunWith(SpringJUnit4ClassRunner.class)
//配置测试组件的配置文件路径,该路径位于class或maven项目的resource文件夹下
@ContextConfiguration("/applicationContext-jms.xml")
public class ActiveMQTest {

//属性自动绑定
@Autowired
@Qualifier("queueDestination")
private Destination destination;
@Autowired
private ProductService productService;

public Destination getDestination() {
return destination;
}

public void setDestination(Destination destination) {
this.destination = destination;
}

public ProductService getProductService() {
return productService;
}

public void setProductService(ProductService productService) {
this.productService = productService;
}

//测试方法
@org.junit.Test
public void testSendMessage()
{
for(int i=0;i<10;i++)
{
productService.sendMessage(destination, "发送第 "+i+" 条消息......");
}
}

}


3.5 配置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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 使用注解方式注入 -->
<context:component-scan base-package="com.lin" />

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

<!-- 消息发送组件 -->
<bean id="productServiceImpl" class="com.lin.activity.service.ProductServiceImpl">
<property name="jmsTemplate" ref="jmsTemplate"></property>
</bean>

<!-- 真正可以产生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>

<!--这个是队列目的地 -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>queue</value>
</constructor-arg>
</bean>
<!-- 消息监听器 -->
<bean id="costumerListener"
class="com.lin.activity.service.CostumerListener" />
<!-- 消息监听容器 -->
<bean id="jmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="queueDestination" />
<property name="messageListener" ref="costumerListener" />
</bean>

<!-- 单元测试类 -->
<!--  -
<bean id="activeMQTest" class="com.lin.activity.test.ActiveMQTest">
<property name="destination" ref="queueDestination"></property>
<property name="productService" ref="productServiceImpl"></property>
</bean>
-->
</beans>


在上面配置中,在配置一个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环境。

4 测试

以单元测试运行ActiveMQTest的方法

关闭activemq: ./activemq stop

该实例的目录结构:

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