Spring与Jms结合的实例
另见:ActiveMQ5.0实战一: 安装配置ActiveMQ5.0 ActiveMQ5.0实战二: 基本配置 http://www.iteye.com/topic/234101================================================
?
环境:apache-activemq-5.4.2 ,spring2.5,jdk1.6
首先启动消息代理:在apache-activemq-5.4.2\bin下
运行
activemq.bat。
?
example1:
package jms.activemq.myexample.spring;
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 jms.activemq.myexample.spring;
import javax.jms.*;
/**
?* Text消息监听
?*
?* @author xiaochuanyu
?*
?*/
public class TextListener implements MessageListener {
?/**
? * Casts the message to a TextMessage and displays its text.
? *
? * @param message
? *??????????? the incoming message
? */
?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 jms.activemq.myexample.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringJmsTestMain {
?public static void main(String[] args) throws InterruptedException {
??ApplicationContext context = new ClassPathXmlApplicationContext(
????new String[] { "SpringJms/SpringJms.xml" });
??SpringPublisher publisher = (SpringPublisher) context
????.getBean("springPublisher");
??publisher.start();
?}
}
package jms.activemq.myexample.spring;
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));
?}
}
<?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-2.0.xsd">
?
??? <!-- jms 连接工厂 -->
??? <bean id="jmsFactory" />
??????? </property>
??? </bean>
??? -->
??? <!-- jms Topic -->
??? <bean id="myTopic" />
??? </bean>
??? <!-- 消息监听器 -->
??? <bean id="myTextListener" ref="jmsFactory" />
??????? <property name="destination" ref="myTopic" />
??????? <property name="messageListener" ref="myTextListener" />
??? </bean>
??? <!-- jms 模板 -->
??? <bean id="jmsTemplate" />
??????? </property>
??? </bean>
??? <!-- 消息发布器 -->
??? <bean id="springPublisher" />
??????? </property>
??????? <property name="topic">
??????????? <ref local="myTopic" />
??????? </property>
??? </bean>
</beans>
===================================================
example2:
package stujms.p2ptxt;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
/**
* 消息发送者
*
* @author leizhimin 2009-8-13 17:01:48
*/
public class MySender {
??????? public static void main(String[] args) {
??????????????? ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
??????????????? JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
??????????????? Destination destination = (Destination) ctx.getBean("destination");
??????????????? template.send(destination, new MessageCreator() {
??????????????????????? public Message createMessage(Session session) throws JMSException {
??????????????????????????????? return session.createTextMessage("发送消息:Hello ActiveMQ Text Message!");
??????????????????????? }
??????????????? });
??????????????? System.out.println("成功发送了一条JMS消息");
??????? }
}
package stujms.p2ptxt;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;
/**
* 消息接收者
*
* @author leizhimin 2009-8-13 17:02:04
*/
public class MyReceiver {
??????? public static void main(String[] args) throws JMSException {
??????????????? ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
??????????????? JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
??????????????? Destination destination = (Destination) ctx.getBean("destination");
??????????????? while (true) {
??????????????????????? TextMessage txtmsg = (TextMessage) template.receive(destination);
??????????????????????? if (null != txtmsg)
??????????????????????????????? System.out.println("收到消息内容为: " + txtmsg.getText());
??????????????????????? else
??????????????????????????????? break;
??????????????? }
??????? }
}
<?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-2.5.xsd
??????? http://www.springframework.org/schema/context
??????? http://www.springframework.org/schema/context/spring-context-2.5.xsd">
??????? <!-- 配置JMS连接工厂 -->
??????? <bean id="connectionFactory" value="tcp://localhost:61616"/>
??????? </bean>
??????? <!-- 配置JMS模版 -->
??????? <bean id="jmsTemplate" ref="connectionFactory"/>
??????? </bean>
??????? <!-- 发送消息的目的地(一个队列) -->
??????? <bean id="destination" value="HelloWorldQueue"/>
??????? </bean>
</beans>
运行发送端三次:
成功发送了一条JMS消息
Process finished with exit code 0
然后再运行接收端一次:
收到消息内容为: 发送消息:Hello ActiveMQ Text Message!
收到消息内容为: 发送消息:Hello ActiveMQ Text Message!
收到消息内容为: 发送消息:Hello ActiveMQ Text Message!
继续测试发现,接收端接收一条消息后不退出程序,而是继续等待,一旦有消息发送过来,就获取到,然后输出!
?