最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Spring2.5整合ActiveMQ 5.2
时间:2009-08-21 编辑:简简单单 来源:一聚教程网
ActiveMQ5.2
所用的包都是ActiveMQ自带的。引用的包如下图:
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"> <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616"/> bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory"/> bean> <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg index="0" value="HelloWorldQueue"/> bean> beans> |
运行发送端三次:
成功发送了一条JMS消息 Process finished with exit code 0 |
然后再运行接收端一次:
收到消息内容为: 发送消息:Hello ActiveMQ Text Message! 收到消息内容为: 发送消息:Hello ActiveMQ Text Message! 收到消息内容为: 发送消息:Hello ActiveMQ Text Message! |
继续测试发现,接收端接收一条消息后不退出程序,而是继续等待,一旦有消息发送过来,就获取到,然后输出!
发一张图看看:
-
上一个: js在线考试系统代码
-
下一个: js 浏览器环境检查代码
相关文章
- HTML简单购物数量小程序代码展示 10-31
- html canvas实现弹幕功能 10-31
- HTML中空格表示的意义 10-31
- html area标签解读 10-31
- html使用表单标签实现注册页面代码展示 10-31
- 使用HTML截图并保存为本地图片的代码展示 10-31