Spring + ActiveMQ实现jms发送消息
1. 概述:Spring提供了一个用于简化JMS API使用的抽象框架,并且对用户屏蔽了JMS API中1.0.2和1.1版本的差异。
JMS的功能大致上分为两块,叫做消息制造和消息消耗。JmsTemplate 用于制造消息和同步消息接收。我们今天就用JmsTemplate实现同步的消息接受。
使用JMS发(接)消息的步骤:
1)创建连接工厂
2)使用连接工厂创建连接
3)使用连接创建会话
4)获取一个目的地
5)使用会话和目的地创建消息生产者(消息消费者)
6)使用连接创建一个需要发送的消息类型实例
7)使用连接的一个队列发送器或主题公布器,使用发送器或者主题器发送消息(接受消息)
spring中的JmsTemplate实现了对jms的一些封装,内部提供了很多的方法,我们只需要实现定义的回调接口即可。JmsTemplate继承自JmsAccessor,在JmsAccessor中有ConnectionFactory的定义,而JmsTemplate本身的构造方法也有对ConnectionFactory的封装:
public JmsTemplate(ConnectionFactory connectionFactory) {this();setConnectionFactory(connectionFactory);afterPropertiesSet();}
<?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-3.0.xsd"><!-- 这里我们用构造方法注入 connectionFactory--><bean id = "jmsTemplate" class = "org.springframework.jms.core.JmsTemplate"><constructor-arg ref="connectionFactory"></constructor-arg></bean><!-- 使用activemq中的连接工厂,提供一个brokerUrl,这里表示本地 --><bean id = "connectionFactory" class = "org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL" value="vm://localhost" /></bean><!-- 使用activemq中的点对点消息模型,随意指定一个地址 --><bean id="destination" name="code">public interface MessageCreator {Message createMessage(Session session) throws JMSException;}
public class DummyJms {public static void main(String[] args) throws Exception{ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");JmsTemplate jmsTemplate = (JmsTemplate)context.getBean("jmsTemplate");Destination destination = (Destination)context.getBean("destination");jmsTemplate.send(destination, new MessageCreator(){public Message createMessage(Session session)throws JMSException {return session.createTextMessage("send message ");}});TextMessage msg = (TextMessage)jmsTemplate.receive(destination);System.out.println("receive message = " + msg.getText());}}
public void send(final Destination destination, final MessageCreator messageCreator) throws JmsException {execute(new SessionCallback<Object>() {public Object doInJms(Session session) throws JMSException {doSend(session, destination, messageCreator);return null;}}, false);}
<T> T execute(SessionCallback<T> action) throws JmsException;
public interface SessionCallback<T> {T doInJms(Session session) throws JMSException;}
protected void doSend(Session session, Destination destination, MessageCreator messageCreator)throws JMSException {Assert.notNull(messageCreator, "MessageCreator must not be null");MessageProducer producer = createProducer(session, destination);try {Message message = messageCreator.createMessage(session);if (logger.isDebugEnabled()) {logger.debug("Sending created message: " + message);}doSend(producer, message);// Check commit - avoid commit call within a JTA transaction.if (session.getTransacted() && isSessionLocallyTransacted(session)) {// Transacted session created by this template -> commit.JmsUtils.commitIfNecessary(session);}}finally {JmsUtils.closeMessageProducer(producer);}}
protected MessageProducer doCreateProducer(Session session, Destination destination) throws JMSException {return session.createProducer(destination);}