activemq系列(4)-activemq集成spring

1、新建maven项目,以下是需要依赖的pom文件:

<properties>
		<org.springframework.version>4.3.7.RELEASE</org.springframework.version>
		<springjunit.version>4.12</springjunit.version>
		<junit.version>4.9</junit.version>
		<activemq.version>5.14.4</activemq.version>
	</properties>

	<dependencies>
		<!-- spring核心配置 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<!--spring test 结合 junit 进行测试 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>3.2.4.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
		</dependency>

		<!-- activemq -->
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-all</artifactId>
			<version>${activemq.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-pool</artifactId>
			<version>5.11.1</version>
		</dependency>

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
			<version>2.4.2</version>
		</dependency>
	</dependencies>


	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

 

 

 

2、activemq.properties

 

## ActiveMQ Config
activemq.brokerURL=tcp\://127.0.0.1\:61616
activemq.pool.maxConnections=10
#queueName
activemq.queueName=myspringqueue


3、spring-activemq.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 第三方MQ工厂: ConnectionFactory -->
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<!-- ActiveMQ Address -->
		<property name="brokerURL" value="${activemq.brokerURL}" />
	</bean>

	<!-- ActiveMQ为我们提供了一个PooledConnectionFactory,通过往里面注入一个ActiveMQConnectionFactory 
		可以用来将Connection、Session和MessageProducer池化,这样可以大大的减少我们的资源消耗,要依赖于 activemq-pool包 -->
	<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
		<property name="connectionFactory" ref="targetConnectionFactory" />
		<property name="maxConnections" value="${activemq.pool.maxConnections}" />
	</bean>

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

	<!--这个是目的地 -->
	<bean id="msgQueue" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg>
			<value>${activemq.queueName}</value>
		</constructor-arg>
	</bean>

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

	<!-- 配置自定义监听:MessageListener -->
	<bean id="msgQueueMessageListener" class="activemq.consumer.MsgQueueMessageListener"></bean>

	<!-- 将连接工厂、目标对了、自定义监听注入jms模板 -->
	<bean id="sessionAwareListenerContainer"
		class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="destination" ref="msgQueue" />
		<property name="messageListener" ref="msgQueueMessageListener" />
	</bean>

</beans>


4、spring-context.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"
       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"
       default-autowire="byName">

       <!-- 读入配置属性文件 -->
       <context:property-placeholder location="classpath:activemq.properties" />

       <!-- 注释配置 -->
       <context:annotation-config />

       <!-- 扫描包起始位置 -->
       <context:component-scan base-package="activemq" />

       <import resource="classpath:spring-activemq.xml" />
</beans>


5、ActiveMQProducer.java

 

 

 

 

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

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

/****
 * 
 * @author liuzhihu
 *
 */
@Service("activeMQProducer")
public class ActiveMQProducer {

	private JmsTemplate jmsTemplate;

	public JmsTemplate getJmsTemplate() {
		return jmsTemplate;
	}

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

	public void sendMessage(final String info) {
		jmsTemplate.send(new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(info);
			}
		});
	}
}


6、MsgQueueMessageListener.java

 

 

import org.springframework.jms.listener.SessionAwareMessageListener;

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

/***
 * 
 * @author liuzhihu
 *
 */
public class MsgQueueMessageListener implements SessionAwareMessageListener<Message> {

	@Override
	public void onMessage(Message message, Session session) throws JMSException {

		if (message instanceof TextMessage) {

			System.out.println("consumer the msg : " + ((TextMessage) message).getText());

		}

	}

}


编写单元测试代码:

 

TestProducer.java

 

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;

/***
 * 
 * @author liuzhihu
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring-context.xml" })
public class TestProducer {

	@Autowired
	private ActiveMQProducer activeMQProducer;

	@Test
	public void send() {
		this.activeMQProducer.sendMessage("the message come from Spring!");
	}
}

 

 

运行后如下图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值