二、 JMS-activeMQ 与spring的整合

本文详细介绍了Spring框架如何整合JMS实现消息传递,包括点对点模式和发布/订阅模式的配置与使用,涵盖工程搭建、依赖引入、配置文件设置、消息生产与消费流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring整合JMS

一、点对点模式

1.1消息生产者

 1、创建工程springjms_producer引入依赖:

<properties>
  	<spring.version>4.2.4.RELEASE</spring.version>
  </properties>
  <dependencies>
  	<!--引入sping整合jms的依赖和activeMQ的依赖 、以及单元测试 -->
  	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-jms</artifactId>
		<version>${spring.version}</version>
	</dependency>
	<dependency>
		<groupId>org.apache.activemq</groupId>
		<artifactId>activemq-client</artifactId>
		<version>5.13.4</version>
	 </dependency>
  	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-test</artifactId>
		<version>${spring.version}</version>
	</dependency>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.9</version>
	</dependency>
	
  
  </dependencies>

2、创建配置文件applicationContext-jms-producer.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.xsd
		http://www.springframework.org/schema/context   
		http://www.springframework.org/schema/context/spring-context.xsd">
		
		
	<context:component-scan base-package="com.trg.springJMS.queue.demo"></context:component-scan>     
	
	   
    <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->  
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
	    <property name="brokerURL" value="tcp://192.168.25.146:61616"/>  
	</bean>
	   
    <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->  
	<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">  
	<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->  
	    <property name="targetConnectionFactory" ref="targetConnectionFactory"/>  
	</bean>  
		   
    <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->  
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
	    <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->  
	    <property name="connectionFactory" ref="connectionFactory"/>  
	</bean>      
    <!--这个是队列目的地,点对点的  文本信息-->  
	<bean id="queueTextDestination" class="org.apache.activemq.command.ActiveMQQueue">  
	    <constructor-arg value="springqueue_text"/>  
	</bean>    
	
	<!--这个是订阅模式  文本信息-->  
	<!-- <bean id="topicTextDestination" class="org.apache.activemq.command.ActiveMQTopic">  
	    <constructor-arg value="topic_text"/>  
	</bean>   -->
	
</beans>

3、创建消息生产者类

package com.trg.springJMS.queue.demo;

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;

/**
 *  
 * @FileName:   QueueProducer
 * @Description:  TODO spring整合JMS,点对点模式之生产者
 * @author 唐瑞国
 * @date 2019-8-17下午3:50:02
 */
@Component
public class QueueProducer {

	//将applicationContext-jms-producer.xml文件中注册的两个bean引入进来
	@Autowired
	private JmsTemplate  jmsTemplate;
	
	@Autowired
	private Destination queueTextDestination;
	
	/**
	 * @ClassName: sendTextMessage     
	 * @Description: TODO--主要用来发送消息的
	 * @return: void   
	 * @throws
	 */
	public void sendTextMessage(final String text){
		
		/**
		 * jmsTemplate已经在配置文件中完成了,创建工厂,创建连接,启动连接的操作,我们在这里只需要获取session即可
		 * queueTextDestination 是用来创建队列对象的,所以我们这里只需要获取放进
		 */
		jmsTemplate.send(queueTextDestination, new MessageCreator() {
			
			public Message createMessage(Session session) throws JMSException {

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

}

4、创建测试类

package com.trg.springJMS.queue.test;

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;

import com.trg.springJMS.queue.demo.QueueProducer;

/**
 *  
 * @FileName:   TestQueue
 * @Description:  TODO 测试类,用来测试
 * @author 唐瑞国
 * @date 2019-8-17下午4:05:14
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-jms-producer.xml")
public class TestQueue {

	@Autowired
	private  QueueProducer queueProducer;
	
	@Test
	public void testSend(){
		queueProducer.sendTextMessage("spring 整合JMS之点对点消息生产者开发");
	}
}

 

测试结果如下:

 

1.2消息消费者

 

1、创建工程,引入依赖和上面引入的依赖一致

2、创建配置文件applicationContext-jms-consumer.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.xsd
		http://www.springframework.org/schema/context   
		http://www.springframework.org/schema/context/spring-context.xsd">
	
    <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->  
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
	    <property name="brokerURL" value="tcp://192.168.25.146: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="queueTextDestination" class="org.apache.activemq.command.ActiveMQQueue">  
	    <constructor-arg value="springqueue_text"/>  
	</bean>    
	
	<!-- 我的监听类 -->
	<bean id="myMessageListener" class="com.trg.springJMS.queue.consumer.demo.MyMessageListener"></bean>
	
	<!-- 消息监听容器 -->
	<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="destination" ref="queueTextDestination" />
		<property name="messageListener" ref="myMessageListener" />
	</bean>
	
</beans>

3、创建监听类

package com.trg.springJMS.queue.consumer.demo;

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

/**
 *  
 * @FileName:   MyMessageListener
 * @Description:  TODO 监听类
 * @author 唐瑞国
 * @date 2019-8-17下午4:29:42
 */
public class MyMessageListener  implements MessageListener{

	public void onMessage(Message message) {

		TextMessage textMessage = (TextMessage) message;
		
		try {
			System.out.println("我是消费者,我收到的消息是:" +textMessage.getText());
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

4、测试

package com.trg.springJMS.queue.consumer.test;

import java.io.IOException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.trg.springJMS.queue.consumer.demo.MyMessageListener;

/**
 *  
 * @FileName:   TestQueue
 * @Description:  TODO spring整合JMS之消费者测试类
 * @author 唐瑞国
 * @date 2019-8-17下午4:34:40
 */
//通过注解的方式引入spring的测试单元类和配置文件
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext-jms-consumer.xml")
public class TestQueue {

	 
	
	@Test
	public void testQueue(){
		try {
			System.in.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

测试结果

二、发布/订阅模式

1.1消息生产者

 这里就在不创建工程了,直接利用上面的点对点模式的生产者就可以了

1、依赖之前已经引入好了,就不用再管了

2、修改配置文件如下

修改需要扫描的包,下面我们会重新创建一个包,这个是需要更换的

更换成fabu/订阅模式

<!--这个是订阅模式  文本信息-->  
     <bean id="topicTextDestination" class="org.apache.activemq.command.ActiveMQTopic">  
        <constructor-arg value="springtopic_text"/>  
    </bean>  

<?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.xsd
		http://www.springframework.org/schema/context   
		http://www.springframework.org/schema/context/spring-context.xsd">
		
		
<!-- 	<context:component-scan base-package="com.trg.springJMS.queue.demo"></context:component-scan>     --> 
	<context:component-scan base-package="com.trg.springJMS.topic.demo"></context:component-scan>     
	
	   
    <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->  
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
	    <property name="brokerURL" value="tcp://192.168.25.146:61616"/>  
	</bean>
	   
    <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->  
	<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">  
	<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->  
	    <property name="targetConnectionFactory" ref="targetConnectionFactory"/>  
	</bean>  
		   
    <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->  
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
	    <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->  
	    <property name="connectionFactory" ref="connectionFactory"/>  
	</bean>      
    <!--这个是队列目的地,点对点的  文本信息-->  
<!-- 	<bean id="queueTextDestination" class="org.apache.activemq.command.ActiveMQQueue">  
	    <constructor-arg value="springqueue_text"/>  
	</bean>   -->  
	
	<!--这个是订阅模式  文本信息-->  
	 <bean id="topicTextDestination" class="org.apache.activemq.command.ActiveMQTopic">  
	    <constructor-arg value="springtopic_text"/>  
	</bean>    
	
</beans>

3、创建生产者类

package com.trg.springJMS.topic.demo;

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;

/**
 *  
 * @FileName:   TopicProducer
 * @Description:  TODO spring整合jms之发布/订阅模式-消息生产者
 * @author 唐瑞国
 * @date 2019-8-17下午4:49:47
 */
@Component
public class TopicProducer {

	@Autowired
	private JmsTemplate jmsTemplate;
	
	@Autowired
	private  Destination topicTextDestination;
	
	public void sendTextMessage(final String text){
		jmsTemplate.send(topicTextDestination,new MessageCreator() {
			
			public Message createMessage(Session session) throws JMSException {
				 
				return session.createTextMessage(text);
			}
		});
	}
}

4、创建生产者测试类

package com.trg.springJMS.topic.demo;

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;

/**
 *  
 * @FileName:   TopicProducer
 * @Description:  TODO spring整合jms之发布/订阅模式-消息生产者
 * @author 唐瑞国
 * @date 2019-8-17下午4:49:47
 */
@Component
public class TopicProducer {

	@Autowired
	private JmsTemplate jmsTemplate;
	
	@Autowired
	private  Destination topicTextDestination;
	
	public void sendTextMessage(final String text){
		jmsTemplate.send(topicTextDestination,new MessageCreator() {
			
			public Message createMessage(Session session) throws JMSException {
				 
				return session.createTextMessage(text);
			}
		});
	}
}

测试结果

 

1.2 消息消费者

修改配置文件如下

 <!--这个是队列目的地,发布/订阅模式的  文本信息-->  
    <bean id="topicTextDestination" class="org.apache.activemq.command.ActiveMQTopic">  
        <constructor-arg value="topic_text"/>  
    </bean>    

另外还有一个要注意的点是对监听类要改成topicTextDestination

<!-- 消息监听容器 -->
    <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="topicTextDestination" />
        <property name="messageListener" ref="myMessageListener" />
    </bean>

初了修改这两处配置文件,别的测试方法都是一样的

可以重新去写一个测试类进行测试

注意点:

消费者和生产者里面都会配置一个队列名称,记得一定要一致,不然获取不到内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值