Spring与ActiveMQ的整合

1.首先我们需将所需要的依赖写入pom.xml

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>

		<!-- spring基础 -->
		<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-web</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>

		<!-- aspectj的依赖 -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.8.9</version>
			<scope>runtime</scope>
		</dependency>

		<dependency>
			<groupId>javax.annotation</groupId>
			<artifactId>jsr250-api</artifactId>
			<version>1.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-all</artifactId>
			<version>5.9.0</version>
		</dependency>


2.在web.xml配置spring容器生成

  	<!-- spring容器生成配制 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!-- spring配制文件所在的位置, *表示任何长度的任意字符 -->
		<param-value>classpath:spring*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>


3.在配置spring.xml的同时编写对应的代码,便于理解其结构

配置连接工厂

	<!-- 配置ConnectionFactory,这里的只是spring用于管理ConnectionFactory的,真正产生到JMS服务器连接的工厂是由JMS服务厂商提供的-->
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://localhost:61616" />
	</bean>

	<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
	<bean id="connectionFactory"
		class="org.springframework.jms.connection.SingleConnectionFactory">
		<property name="targetConnectionFactory" ref="targetConnectionFactory" />
	</bean>

配置消息目的地,即Destination

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

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

配置JmsTemplate

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

编写Producer生产者类

package com.yc.services.impl;

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

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 com.yc.services.ProducerService;

@Component("producerService")
public class ProducerServiceImpl{
	@Autowired
	private JmsTemplate jmsTemplate;
	//发送消息
	public void sendMessage(Destination destination, final String message) {
		System.out.println("生产者发了一个消息:" + message); 
        jmsTemplate.send(destination, new MessageCreator(){
            public Message createMessage(Session session) throws JMSException {  
                return session.createTextMessage(message);  
            }
        });
	}
}
到这里我们的生产者其实已经配置完毕,那么完成一个消息的流程我们还需要消费者的存在,接下来我们来实现消费者的代码,这里只是写了接收信息的方法,即是消息监听器,消费者的实现我们依旧交给spring来完成

package com.yc.services.impl;

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

public class ConsumerImpl implements MessageListener{
	public void onMessage(Message message) {
		TextMessage content=(TextMessage) message;
		try {
			System.out.println("接收到的消息为:"+content.getText());
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
}

转到spring.xml,配置消息监听器

	<!-- 消息监听器 -->
	<bean id="consumerMessageListener" class="com.yc.services.impl.ConsumerImpl" />

配置消息监听容器,即消费者,所必须给的参数有三个,连接工厂,目的地以及处理消息的监听

	<!-- 消息监听容器,消费者 -->
	<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<!-- 连接工厂 -->
		<property name="connectionFactory" ref="connectionFactory" />
		<!-- 消息目的地,这里是queue -->
		<property name="destination" ref="queueDestination" />
		<!-- 消息监听,消费者 -->
		<property name="messageListener" ref="consumerMessageListener" />
	</bean>

 

4.OK,至此我们的配置已经可以完成ActiveMQ的基本操作了,我们来用junit测试一下所写的代码

package com.yc.test;

import javax.jms.Destination;

import org.junit.Test;
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.yc.services.ProducerService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class ProducerServiceImplTest {

    @Autowired
    private ProducerServiceImpl producerService;
    
    @Autowired  
    @Qualifier("queueDestination")  //使得自动注入从bytype变为byname
    private Destination destination;  
      
    @Test
    public void testSend() { 
    	for (int i = 0; i < 2; i++) {
    		producerService.sendMessage(destination, "你好,ActiveMQ" );  
		}
    }  
}
junit testSend()方法



成功大笑

发布了36 篇原创文章 · 获赞 4 · 访问量 6万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章