Spring2.5整合ActiveMQ 5.2(P2P文本消息)

简介:
想找个Spring整合ActiveMQ可运行的实例,快速看看效果,可是很失望,网上例子都是抄来抄去,没源码(或者有源码运行不了),真TM没意思,看文档我自己实现个,不能运行你砍我!
 
不想把例子搞的太复杂,越简单越好!
 
 
项目环境:
JDK1.5
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" > 

        <!--  配置JMS连接工厂 --> 
         < bean  id ="connectionFactory"  class ="org.apache.activemq.spring.ActiveMQConnectionFactory" > 
                 < property  name ="brokerURL"  value ="tcp://localhost:61616" /> 
         </ bean > 

        <!--  配置JMS模版 --> 
         < 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!
 
继续测试发现,接收端接收一条消息后不退出程序,而是继续等待,一旦有消息发送过来,就获取到,然后输出!
 
发一张图看看:
 
 

本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/191470,如需转载请自行联系原作者
相关文章
|
消息中间件 网络协议 Java
ActiveMQ系列:结合Spring,基于配置文件的使用ActiveMQ
从activemq脚本可以看出启动ActiveMQ实际是启动,bin文件夹下的其实activemq.jar 包中有一个类为Main,这就是active的启动入口,Main主要是加载lib目录和ClassPath,初始化 类加载器,委托给ShellCommand,由ShellCommand根据命令描述去执行,如果是Version和HELP, 则打印信息,若是启动命令,则通过XBeanBrokerFactory创建BrokerService
127 0
ActiveMQ系列:结合Spring,基于配置文件的使用ActiveMQ
|
消息中间件 Java 中间件
24、springboot集成ActiveMQ
目前使用较多的消息队列有ActiveMQ、RabbitMQ、Kafka、RocketMQ、MetaMQ等。spring boot提供了对JMS系统的支持;springboot很方便就可以集成这些消息中间件。
261 0
24、springboot集成ActiveMQ
|
消息中间件 存储 安全
SpringBoot集成ActiveMQ实例详解
SpringBoot集成ActiveMQ实例详解
349 0
SpringBoot集成ActiveMQ实例详解
|
消息中间件 Java Maven
ActiveMQ整合Spring框架
前面文章介绍了ActiveMQ的相关内容,本文介绍ActiveMQ和Spring的整合开发
ActiveMQ整合Spring框架
|
消息中间件 Java Spring
Spring Boot整合Activemq及其使用
什么是ActiveMQ? ActiveMQ是一种开源的,实现了JMS规范的,面向消息(MOM)的中间件,为应用程序提供高效的、可扩展的、稳定的和安全的企业级消息通信。 什么是JMS? Java消息服务指的是两个应用程序之间进行异步通信的API,它为标准消息协议和消息服务提供了一组通用接口,包括创建、发送、读取消息等,用于支持JAVA应用程序开发。
2094 0
|
消息中间件 Java Spring
Spring Cloud 2.x系列之springboot集成ActiveMQ
消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量削锋等问题。实现高性能,高可用,可伸缩和最终一致性架构;是大型分布式系统不可缺少的中间件。目前使用较多的消息队列有ActiveMQ、RabbitMQ、Kafka、RocketMQ、MetaMQ等。
4091 0
|
消息中间件 Java Spring
【源码阅读】看Spring Boot如何自动装配ActiveMQ收发组件
源于好奇,我研究了一下Spring Boot中ActiveMQ相关组件是如何自动装配的。记录如下。 源码路径 本文以Spring Boot 1.5.10.RELEASE版本为例。
2115 0
|
消息中间件 Java API
springboot与activemq的使用
1、springboot和activemq的使用相对来说比较方便了,我在网上看了很多其他的资料,但是自己写出来总是有点问题所以,这里重点描述一下遇到的一些问题。 2、至于activemq的搭建和springmvc的搭建可以参考:http://www.
1349 0