- 创建maven项目,导入依赖
-
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-version>4.2.2.RELEASE</spring-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-version}</version>
</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.14.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
</dependencies>
-
- 配置文件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:context="http://www.springframework.org/schema/context"
xmlns:spring="http://camel.apache.org/schema/spring"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.2.xsd
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-4.2.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring-2.0-M1.xsd">
<context:component-scan base-package="com.hand.spring_jms"></context:component-scan>
<!-- 真正产生Connection的ConnectionFactory -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"></property>
</bean>
<!-- ActiveMQ为我们提供了一个PooledConnectionFactory -->
<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
<property name="connectionFactory" ref="targetConnectionFactory"></property>
<property name="maxConnections" value="10"></property>
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="pooledConnectionFactory"></property>
</bean>
<!-- 配置生产者 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"></property>
</bean>
<!-- 队列的目的地,点对点的关系(P2P)-->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>queue</value>
</constructor-arg>
</bean>
<!-- 消息监听器 -->
<bean id="consumerMessageListener" class="com.hand.spring_jms.service.ConsumerMessageListener"></bean>
<!-- 消息监听容器 -->
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="destination" ref="queueDestination"></property>
<property name="messageListener" ref="consumerMessageListener"></property>
</bean>
<!-- 主题的目的地,是一对多的关系(发布/订阅) -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg>
<value>topic</value>
</constructor-arg>
</bean>
</beans>
-
- 创建类ProducerService,用于发布消息
- 在sendMessage方法体里面我们是通过jmsTemplate来发送消息到对应的Destination的。
-
package com.hand.spring_jms.service;
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 ProducerService{
@Autowired
@Qualifier("jmsTemplate")
private JmsTemplate jmsTemplate;
public void sendMessage(Destination destination, final String message){
System.out.println("------生产者发送的消息-----");
System.out.println("生产者发送的一条消息: "+ message);
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
// TODO Auto-generated method stub
System.out.println("发送成功---");
return session.createTextMessage(message);
}
});
}
public JmsTemplate getJmsTemplate(){
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
}
- 创建ConsumerMessageListener类,用作消息的接收,这里继承MessageListener,用于监听队列中是否有消息,接收到消息后的进一步处理也可在此方法中实现
-
package com.hand.spring_jms.service;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class ConsumerMessageListener implements MessageListener {
public void onMessage(Message message) {
TextMessage msg = (TextMessage)message;
System.out.println("接收到一条纯文本消息");
try {
System.out.println("消息的内容是: "+ msg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
-
- 创建测试类
-
package com.hand.spring_jms.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.hand.spring_jms.service.ProducerService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext.xml")
public class ProducerCusumerTest {
@Autowired
private ProducerService producerService;
@Autowired
@Qualifier("queueDestination")
private Destination destination;
@Test
public void testSend(){
for (int i = 0; i < 2; i++) {
producerService.sendMessage(destination, "Hello World!!!");
}
}
}
-
- 实现中的一些注意的细节部分
- 使用ActiveMQ提供的ActiveMQConnectionFactory,用于产生ConnectionFactory,Spring中的SingleConnectionFactory只是一种spring提供的管理方式,不是真正产生Connection的ConnectionFactory。
- ActiveMQ为我们提供了一个PooledConnectionFactory,通过往里面注入一个ActiveMQConnectionFactory可以用来将Connection、Session和MessageProducer池化,这样可以大大的减少我们的资源消耗。
- 在配置一个MessageListenerContainer的时候有三个属性必须指定,一个是表示从哪里监听的ConnectionFactory;一个是表示监听什么的Destination;一个是接收到消息以后进行消息处理的MessageListener。
Spring+JMS实现消息的发送与接收
最新推荐文章于 2024-04-10 13:22:04 发布