Spring与ActiveMQ整合

maven依赖:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-jms -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>4.3.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.xbean/xbean-spring -->
<dependency>
    <groupId>org.apache.xbean</groupId>
    <artifactId>xbean-spring</artifactId>
    <version>4.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-core -->
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-core</artifactId>
    <version>5.7.0</version>
</dependency>
activemq.properties内容:
brokerURL=tcp://127.0.0.1:61616
activemq.userName=admin
activemq.password=admin
queue.address=queue
topic.address=topic
spring与activemq整合的文件,由于此文件我是直接在spring与spring MVC整合的文件spring-mvc.xml中引入的,我已经在spring-mvc.xml中全局扫描含有注解的文件了,所以在这不需要扫描有注解的文件了:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"
	xmlns:jms="http://www.springframework.org/schema/jms"
	xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd   
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms-4.2.xsd
        http://activemq.apache.org/schema/core
        http://activemq.apache.org/schema/core/activemq-core-5.7.0.xsd">

    <!-- 引入配置文件 -->
    <bean id="activemqPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="order" value="3" />
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
			<list>
				<value>classpath:activemq.properties</value>
			</list>
		</property>
    </bean>
    
    <!-- ActiveMQ 连接工厂 -->
    <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
    <!-- <amq:connectionFactory id="amqConnectionFactory"
        brokerURL="tcp://127.0.0.1:61616" userName="admin" password="admin"  /> -->

	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
	   <property name="brokerURL" value="${brokerURL}"/>
	   <property name="userName" value="${activemq.userName}"/>
	   <property name="password" value="${activemq.password}"/>
	</bean>
	
    <!-- Spring 连接工厂 -->
    <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->  
    <!-- Spring为我们提供了多个ConnectionFactory,有SingleConnectionFactory和CachingConnectionFactory。
         SingleConnectionFactory对于建立JMS服务器链接的请求会一直返回同一个链接,并且会忽略Connection的close方法调用。
         CachingConnectionFactory继承了SingleConnectionFactory,所以它拥有SingleConnectionFactory的所有功能,
                           同时它还新增了缓存功能,它可以缓存Session、MessageProducer和MessageConsumer。-->
    <bean id="jmsconnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->  
        <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
        <!-- Session缓存数量 -->
        <property name="sessionCacheSize" value="20" />
    </bean>
    
    <!-- 定义消息的目的地 -->
    <!--点对点消息队列目的地-->
	<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
	    <constructor-arg value="${queue.address}"/>
	</bean>
	<!--发布订阅主题目的地-->
	<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
	    <constructor-arg value="${topic.address}"/>
	</bean>
    
    <!-- 消息生产者 start -->
    <!-- 定义JmsTemplate的Queue类型 -->
    <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
        <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->   
        <property name="connectionFactory" ref="jmsconnectionFactory"/>
        <property name="defaultDestination" ref="queueDestination" />
        <!-- 如果不用defaultDestination而是用defaultDestinationName则不需要在前面配置目的地了 -->
        <!-- <property name="defaultDestinationName" value="${queue.address}"/> -->
        <!-- 非pub/sub模型(发布/订阅),即队列模式 -->
        <property name="pubSubDomain" value="false" />
    </bean>

    <!-- 定义JmsTemplate的Topic类型 -->
    <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
         <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->  
        <property name="connectionFactory" ref="jmsconnectionFactory"/>
        <property name="defaultDestination" ref="topicDestination" />
        <!-- 如果不用defaultDestination而是用defaultDestinationName则不需要在前面配置目的地了 -->
        <!-- <property name="defaultDestinationName" value="${topic.address}"/> -->
        <!-- pub/sub模型(发布/订阅) -->
        <property name="pubSubDomain" value="true" />
    </bean>
    <!--消息生产者 end-->
    
    <!-- 消息消费者 start-->
    <!-- 定义Queue监听器 -->
    <jms:listener-container destination-type="queue" container-type="default" connection-factory="jmsconnectionFactory" acknowledge="auto">
        <!-- 在这里可以有很多的消息消费者,ref后是spring扫描文件时注入的bean -->
        <jms:listener destination="${queue.address}" ref="queueConsumer1"/>
        <jms:listener destination="${queue.address}" ref="queueConsumer2"/>
    </jms:listener-container>

    <!-- 定义Topic监听器 -->
    <jms:listener-container destination-type="topic" container-type="default" connection-factory="jmsconnectionFactory" acknowledge="auto">
        <!-- 在这里可以有很多的消息消费者,ref后是spring扫描文件时注入的bean -->
        <jms:listener destination="${topic.address}" ref="topicConsumer1"/>
        <jms:listener destination="${topic.address}" ref="topicConsumer2"/>
    </jms:listener-container>
    <!-- 消息消费者 end -->
</beans>
消息生产者文件的内容:

QueueProducer.java文件:

package com.cn.common.jmsProducer;

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.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;
@Component
public class QueueProducer {

	@Autowired
	@Qualifier("jmsQueueTemplate")
	private JmsTemplate jmsTemplate; //注入配置文件中的bean
	
	@Autowired
	@Qualifier("queueDestination")  
    private Destination destination;
	
	public void send(final String message){
		/*jmsTemplate.send(destination, new MessageCreator() {
			@Override
			public Message createMessage(Session paramSession) throws JMSException {
				return paramSession.createTextMessage(message);
			}
		});*/
		jmsTemplate.send(new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(message);
			}
		});;
	}
}
TopicProducer.java文件:
package com.cn.common.jmsProducer;

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.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;
@Component
public class TopicProducer {

	@Autowired
	@Qualifier("jmsTopicTemplate")
	private JmsTemplate jmsTemplate; //注入配置文件中的bean
	
	@Autowired
	@Qualifier("topicDestination")
    private Destination destination;
	
	public void send(final String message){
		/*jmsTemplate.send(destination, new MessageCreator() {
			@Override
			public Message createMessage(Session paramSession) throws JMSException {
				return paramSession.createTextMessage(message);
			}
		});*/
		jmsTemplate.send(new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(message);
			}
		});;
	}
}
消息消费者文件的内容:

QueueConsumer1.java文件:

package com.cn.common.jmsConsumer;

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

import org.springframework.stereotype.Component;

@Component
public class QueueConsumer1 implements MessageListener{

	@Override
	public void onMessage(Message message) {
		try {
			TextMessage text = null;
			if(message instanceof TextMessage){
				text = (TextMessage) message;
				System.out.println("QueueConsumer1接收到消息:"+text.getText());
			}
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}
QueueConsumer2.java文件:
package com.cn.common.jmsConsumer;

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

import org.springframework.stereotype.Component;

@Component
public class QueueConsumer2 implements MessageListener{

	@Override
	public void onMessage(Message message) {
		try {
			TextMessage text = null;
			if(message instanceof TextMessage){
				text = (TextMessage) message;
				System.out.println("QueueConsumer2接收到消息:"+text.getText());
			}
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}
TopicConsumer1.java文件:
package com.cn.common.jmsConsumer;

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

import org.springframework.stereotype.Component;
@Component
public class TopicConsumer1 implements MessageListener{

	@Override
	public void onMessage(Message message) {
		try {
			TextMessage text = null;
			if(message instanceof TextMessage){
				text = (TextMessage) message;
				System.out.println("TopicConsumer1接收到消息:"+text.getText());
			}
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}
TopicConsumer2.java文件:
package com.cn.common.jmsConsumer;

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

import org.springframework.stereotype.Component;
@Component
public class TopicConsumer2 implements MessageListener{

	@Override
	public void onMessage(Message message) {
		try {
			TextMessage text = null;
			if(message instanceof TextMessage){
				text = (TextMessage) message;
				System.out.println("TopicConsumer2接收到消息:"+text.getText());
			}
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}
下面就是在我们需要发送消息的java文件中依赖注入QueueProducer发送点对点消息或TopicProducer发送发布订阅主题消息了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值