首先来认识一下spring封装好的JMS的接口:ConnectionFactory、JmsTemplate、MessageListener。其中ConnectionFactory是spring提供的,并不是JMS规范中那个的ConnectionFactory。
1)、ConnectionFactory(用于管理连接的连接工厂):
- spring提供的连接池,之所以有这个连接池存在,因为在于JmsTemplate每次发送消息都会重新建立连接、回话、producer,在大量数据产生的时候很耗费性能
- 此外,spring还提供了SingleConnectingFactory和CachingConnectionFactory,前者在每次连接时只会返回同一个Connection,也就是整个应用只会使用这一个连接,后者集成了前者,不止拥有前者所有功能,还增加了缓存功能来回村回话、producer、消费者、生产者
2)、JmsTemplate(用于接收和发送消息的模板类):
- spring提供,只需向spring容器注册这个类就可以直接使用其来方便操作JMS
- 线程安全,可以在整个应用范围内使用
- 在应用中可以使用多个这样的模板
3)、MessageListener(消息监听器):
- 一个接口,只有一个onMessage方法需要实现,且该方法只接收一个Message参数
项目演示环节:
1、搭建Maven项目、POM文件引进相关依赖:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
2、首先演示队列模式:
- 创建提供者(生产者)者统一接口:
IProducerService.java
package jms.producer;
public interface IProducerService {
void sendMessage(String message);
}
- 创建公共配置文件(供生产者和消费者共同使用):
common.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">
<!-- 添加注解使用支持 -->
<context:annotation-config/>
<!-- 这里使用ActiveMQ提供的ConnectionFactory,而不是使用spring提供的 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://66.112.220.202:61616"/>
</bean>
<!-- spring JMS 提供的连接池 -->
<bean id="singleConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="targetConnectionFactory"/>
</bean>
<!-- 队列模式目的地,点对点式的 -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="queue" />
</bean>
<!-- 主题模式目的地,点对点式的 -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic" />
</bean>
</beans>
- 生产者对应配置文件:
<?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">
<import resource="common.xml"/>
<!-- spring提供的模板类,用于发送消息,添加到容器进行管理,可以在应用中直接注入使用 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="singleConnectionFactory"/>
</bean>
<bean class="jms.producer.impl.ProducerServiceImpl"/>
</beans>
- 提供者服务的实现类:
ProducerServiceImpl.java
package jms.producer.impl;
import jms.producer.IProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import javax.annotation.Resource;
import javax.jms.*;
public class ProducerServiceImpl implements IProducerService{
@Autowired
private JmsTemplate jmsTemplate;
@Resource(name = "queueDestination")
private Destination destination;
public void sendMessage(final String message) {
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
// 封装message,创建消息
TextMessage textMessage = session.createTextMessage(message);
return textMessage;
}
});
System.err.println("消息已发送:" + message);
}
}
- 创建测试类,调用IProducerService的sendMessage方法发送消息到指定的activeMQ目标:
package jms.test;
import jms.producer.IProducerService;
import jms.producer.impl.ProducerServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestProducer {
public static void main(String[] args) {
// 该类没有close方法,所以就是用ClassPathXmlApplicationContext类
// ApplicationContext context = new ClassPathXmlApplicationContext("classpath:producer.xml");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:producer.xml");
IProducerService service = context.getBean(ProducerServiceImpl.class);
for (int i = 0; i < 20; i++) {
service.sendMessage("this message number is: " + i);
}
context.close();
}
}
- 测试运行结果:
九月 11, 2017 3:17:41 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4b85612c: startup date [Mon Sep 11 15:17:41 CST 2017]; root of context hierarchy
九月 11, 2017 3:17:41 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [producer.xml]
九月 11, 2017 3:17:41 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [common.xml]
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
九月 11, 2017 3:17:42 下午 org.springframework.jms.connection.SingleConnectionFactory initConnection
信息: Established shared JMS Connection: ActiveMQConnection {id=ID:Sanho-64309-1505114261712-1:1,clientId=null,started=false}
消息已发送:this message number is: 0
消息已发送:this message number is: 1
消息已发送:this message number is: 2
消息已发送:this message number is: 3
消息已发送:this message number is: 4
消息已发送:this message number is: 5
消息已发送:this message number is: 6
消息已发送:this message number is: 7
消息已发送:this message number is: 8
消息已发送:this message number is: 9
消息已发送:this message number is: 10
消息已发送:this message number is: 11
消息已发送:this message number is: 12
消息已发送:this message number is: 13
消息已发送:this message number is: 14
消息已发送:this message number is: 15
消息已发送:this message number is: 16
消息已发送:this message number is: 17
消息已发送:this message number is: 18
消息已发送:this message number is: 19
九月 11, 2017 3:17:48 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@4b85612c: startup date [Mon Sep 11 15:17:41 CST 2017]; root of context hierarchy
- 创建消费者:
1)、首先创建一个消息监听者:
ConsumerMessageListener.java
package jms.consumer;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class ConsumerMessageListner implements MessageListener{
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
String text = textMessage.getText();
System.out.println(text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
2)、接着创建监听容器配置文件:
consumer.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">
<!-- 引入公共配置 -->
<import resource="common.xml"/>
<!-- 自主定义的消息监听器 -->
<bean id="consumerMessageListner" class="jms.consumer.ConsumerMessageListner"/>
<!-- spring为我们提供的消息监听容器,其作用是管理容器使其自动连接消息工厂、消息目的地、消息监听者 -->
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<!-- spring提供的连接工厂 -->
<property name="connectionFactory" ref="singleConnectionFactory"/>
<!-- 消息监听目的地 -->
<property name="destination" ref="queueDestination"/>
<!-- 引入自定义的消息监听器 -->
<property name="messageListener" ref="consumerMessageListner"/>
</bean>
</beans>
3)、创建消费者测试类:
package jms.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestConsumer {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:consumer.xml");
}
}
4)、运行测试:
九月 11, 2017 3:34:29 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4b85612c: startup date [Mon Sep 11 15:34:29 CST 2017]; root of context hierarchy
九月 11, 2017 3:34:29 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [consumer.xml]
九月 11, 2017 3:34:29 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [common.xml]
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
九月 11, 2017 3:34:29 下午 org.springframework.context.support.DefaultLifecycleProcessor start
信息: Starting beans in phase 2147483647
九月 11, 2017 3:34:30 下午 org.springframework.jms.connection.SingleConnectionFactory initConnection
信息: Established shared JMS Connection: ActiveMQConnection {id=ID:Sanho-65054-1505115270052-1:1,clientId=null,started=false}
this message number is: 0
this message number is: 1
this message number is: 2
this message number is: 3
this message number is: 4
this message number is: 5
this message number is: 6
this message number is: 7
this message number is: 8
this message number is: 9
this message number is: 10
this message number is: 11
this message number is: 12
this message number is: 13
this message number is: 14
this message number is: 15
this message number is: 16
this message number is: 17
this message number is: 18
this message number is: 19
如果打开两个消费者测试类进行测试,效果如上节测试结果一致,见:java消息中间件之一。
再者就是测试主题模式,这里针对上述代码仅需修改两处:
1)、消息提供类服务中:
@Resource(name = "queueDestination")
private Destination destination;
修改成:
@Resource(name = "topicDestination")
private Destination destination;
<!-- spring为我们提供的消息监听容器,其作用是管理容器使其自动连接消息工厂、消息目的地、消息监听者 -->
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<!-- spring提供的连接工厂 -->
<property name="connectionFactory" ref="singleConnectionFactory"/>
<!-- 消息监听目的地 -->
<property name="destination" ref="queueDestination"/>
<!-- 引入自定义的消息监听器 -->
<property name="messageListener" ref="consumerMessageListner"/>
</bean>
修改成:
好了,到这里spring集成JMS连接ActiveMQ的的简单演示就结束了,如果对ActiveMQ集群配置使用感兴趣的可以参考我的下篇博客内容:java消息中间件之三。