JMS做为一个支持点对点(PTP)和订阅式(pub/sub)式的消息中间件,为不少项目开发者所使用。Spring对JMS提供了很好的支持,能够经过JmsTemplate来方便地实现消息服务,因为JMS对Spring的支持性很好,本文将着重于详细说明如何spring如何整合jms作简单的测试用例:java
spring.jar , activemq-all-5.4.3.jar , commons-logging-api-1.1.jar , commons-io-1.3.2.jar,这些jar包能够去本文最后面的连接里下载。web
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- jms 链接工厂 --> <bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <!-- 配置代理的地址,即配置activeMQ的链接URI, 让jms工厂可以链接到activeMQ服务器(将activeMQ暴露给客户端使用, 负责客户端与activeMQ之间的链接通讯) --> <property name="brokerURL"> <value>tcp://localhost:61616</value><!-- 一种标准URI地址,意思是说标识一个本地的端口号位61616的TCP链接(其中,"61616"是activeMQ默认的链接端口号) --> </property> </bean> <!-- ActiveMQ链接器将这种简单等级结构的URI模式称为低等级的链接器(low-levelconnectors), 并为这些链接器实现了基本的网络通讯协议。低等级链接器URIs使用主题(scheme)标识底层使用的网络协议, 使用路径元素定位网络资源服务(通常为主机名加上端口号),使用查询元素用来肯定链接器附加信息。 --> <!-- jms 链接池 --> <!-- <bean id="pooledJmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"> <property name="connectionFactory"> <ref local="jmsFactory" /> </property> </bean> --> <!-- jms 模板 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory"> <ref local="jmsFactory" /> </property> </bean> <!-- jms Topic --> <bean id="myTopic" class="org.apache.activemq.command.ActiveMQTopic" autowire="constructor"> <constructor-arg value="STOCKS.JAVA" /> </bean> <!-- jms Consumer --> <bean id="javaConsumer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="jmsFactory" /> <property name="destination" ref="myTopic" /> <property name="messageListener" ref="myTextListener" /> </bean> <!-- 消息监听器 --> <bean id="myTextListener" class="demo.TextListener"> </bean> <!-- 消息发布器 --> <bean id="springPublisher" class="demo.SpringPublisher"> <property name="template"> <ref local="jmsTemplate" /> </property> <property name="topic"> <ref local="myTopic" /> </property> </bean> </beans>
package demo; import java.util.Date; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import javax.jms.TextMessage; import org.springframework.jms.core.MessageCreator; public class MyMessageCreator implements MessageCreator { /** * 消息序号 */ private int msgNo; public MyMessageCreator(int no) { this.msgNo = no; } @Override public Message createMessage(Session session) throws JMSException { TextMessage textMsg = session.createTextMessage(); textMsg.setText(new Date() + "第" + this.msgNo + "条消息发出"); return textMsg; } }
package demo; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; public class TextListener implements MessageListener { @Override public void onMessage(Message message) { TextMessage msg = null; try { if (message instanceof TextMessage) { msg = (TextMessage) message; System.out.println("Reading message: " + msg.getText()); } else { System.out.println("Message of wrong type: " + message.getClass().getName()); } } catch (JMSException e) { System.out.println("JMSException in onMessage(): " + e.toString()); } catch (Throwable t) { System.out.println("Exception in onMessage():" + t.getMessage()); } } }
package demo; import javax.jms.Destination; import org.springframework.jms.core.JmsTemplate; public class SpringPublisher { /** * Jms模板 */ private JmsTemplate template; /** * Topic */ private Destination topic; public JmsTemplate getTemplate() { return template; } public void setTemplate(JmsTemplate template) { this.template = template; } public Destination getTopic() { return topic; } public void setTopic(Destination topic) { this.topic = topic; } /** * Start * * @throws InterruptedException */ public void start() throws InterruptedException { int messageCount = 10; while ((--messageCount) > 0) { sendMessage(messageCount); Thread.sleep(1000); } } /** * 消息发送 */ protected void sendMessage(int msgNO) { this.template.send(this.topic, new MyMessageCreator(msgNO)); } }
package test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import demo.SpringPublisher; public class SpringJmsTestMain { /** * @param args */ public static void main(String[] args) throws InterruptedException { ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml" }); SpringPublisher publisher = (SpringPublisher) context .getBean("springPublisher"); publisher.start(); } }
好了,本文主要就是测试基本的spring整合jms。另:下面是我在淘宝上看到的JMS教学视频,若是朋友们想要细致的学习JMS,能够移步到这里下载JMS视频:JMS教学视频-淘宝spring
下篇文章将着重讲解如何开发订阅模式jms,并将阐述如何将ActiveMQ嵌入Spring中。apache