JMS接口:
1。ConnectionFactory用于管理连接的连接工厂
2。JmsTemplate用于发送和接收消息的模版类
3。MessageListerner消息监听器
ConnectionFactory
一个Spring提供的连接池,使用连接池是因为 JmsTemplate每次发消息都需要创建连接、会话和productor。spring中提供了SingelConnectionFactory和CachingConnectionFactoy两个连接池实现
SingelConnectionFactory(对于JMS建立连接使用同一Connection)
CachingConnectionFactoy(继承SingelConnectionFactory,增加缓存功能)
JmsTemplate
由Spring提供,只需要向spring容器内注册这个类就可以使用JmsTemplate方便的操作jms
是线程安全的,可以再整个应用范围内使用
MessageListerner
实现一个onMessage方法,该方法只接收一个Message参数
集成步骤:
1.编写生产者接口
package com.spring.jms.producer;
/**
* 生产者
* @author root
*
*/
public interface ProducerService {
void sendMessage(String message);
}
2.添加实现方法
package com.spring.jms.producer;
import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
public class ProduceserServiceImpl implements ProducerService{
@Autowired
JmsTemplate jmsTemplate;
@Resource(name="topicDestination")
Destination destination;
public void sendMessage(final String message) {
// TODO Auto-generated method stub
jmsTemplate.send(destination,new MessageCreator() {
public Message createMessage(Session arg0) throws JMSException {
TextMessage textMessage=arg0.createTextMessage(message);
return textMessage;
}
});
System.out.println("发送消息:"+message);
}
}
3.编写xml文件一个公共的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/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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config />
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost: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="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<!-- 名称 -->
<constructor-arg value="queue"></constructor-arg>
</bean>
<!-- 一个主题目的地 -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<!-- 名称 -->
<constructor-arg value="topic"></constructor-arg>
</bean>
</beans>
4.创建生产者的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/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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<import resource="common.xml"/>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"></property>
</bean>
<bean class="com.spring.jms.producer.ProduceserServiceImpl"></bean>
</beans>
5.创建消费者监听器,监听信息进行处理
package com.spring.jms.consumer;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
/**
* 消息监听者
* @author root
*
*/
public class ConsumerMessageLinstener implements MessageListener {
public void onMessage(Message message) {
// TODO Auto-generated method stub
TextMessage textMessage=(TextMessage) message;
try {
System.out.println("接收到:"+textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
6.编写生产者启动App
package com.spring.jms;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.jms.producer.ProducerService;
public class AppProducer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"producer.xml");
ProducerService producerService = context
.getBean(ProducerService.class);
for (int i = 0; i < 100; i++) {
producerService.sendMessage("test" + i);
}
context.close();//执行后关闭连接
}
}
7.编写消费者
package com.spring.jms;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppConsumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"consumer.xml");//自动加载监听器处理
}
}
如果要修改主题模式和队列模式,需要修改以下两个地方:
1,生产者中注入的模式名称。
package com.spring.jms.producer;
import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
public class ProduceserServiceImpl implements ProducerService{
@Autowired
JmsTemplate jmsTemplate;
@Resource(name="topicDestination")//这里就是要修改的地方,需要和配置文件中的名称对应
Destination destination;
public void sendMessage(final String message) {
// TODO Auto-generated method stub
jmsTemplate.send(destination,new MessageCreator() {
public Message createMessage(Session arg0) throws JMSException {
TextMessage textMessage=arg0.createTextMessage(message);
return textMessage;
}
});
System.out.println("发送消息:"+message);
}
}
2.生产者的模式修改了,消费者的模式也要对应修改,需要修改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/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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 导入公共配置 -->
<import resource="common.xml"/>
<!-- 配置消息监听器 -->
<bean id="consumerMessageLinstener" class="com.spring.jms.consumer.ConsumerMessageLinstener"></bean>
<!-- 配置消息监听容器 -->
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<!-- 配置连接工厂 -->
<property name="connectionFactory" ref="connectionFactory"></property>
<!-- 制定消費目的地 (这里就是要修改的地方,需要和使用的模式相对应)-->
<property name="destination" ref="topicDestination"></property>
<!-- 指定消息监听器 -->
<property name="messageListener" ref="consumerMessageLinstener"></property>
</bean>
</beans>