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,如需转载请自行联系原作者
相关文章
|
4月前
|
消息中间件 存储 Java
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——ActiveMQ安装
本教程介绍ActiveMQ的安装与基本使用。首先从官网下载apache-activemq-5.15.3版本,解压后即可完成安装,非常便捷。启动时进入解压目录下的bin文件夹,根据系统选择win32或win64,运行activemq.bat启动服务。通过浏览器访问`http://127.0.0.1:8161/admin/`可进入管理界面,默认用户名密码为admin/admin。ActiveMQ支持两种消息模式:点对点(Queue)和发布/订阅(Topic)。前者确保每条消息仅被一个消费者消费,后者允许多个消费者同时接收相同消息。
111 0
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——ActiveMQ安装
|
4月前
|
消息中间件 Java 微服务
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——发布/订阅消息的生产和消费
本文详细讲解了Spring Boot中ActiveMQ的发布/订阅消息机制,包括消息生产和消费的具体实现方式。生产端通过`sendMessage`方法发送订阅消息,消费端则需配置`application.yml`或自定义工厂以支持topic消息监听。为解决点对点与发布/订阅消息兼容问题,可通过设置`containerFactory`实现两者共存。最后,文章还提供了测试方法及总结,帮助读者掌握ActiveMQ在异步消息处理中的应用。
141 0
|
4月前
|
消息中间件 网络协议 Java
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——ActiveMQ集成
本文介绍了在 Spring Boot 中集成 ActiveMQ 的详细步骤。首先通过引入 `spring-boot-starter-activemq` 依赖并配置 `application.yml` 文件实现基本设置。接着,创建 Queue 和 Topic 消息类型,分别使用 `ActiveMQQueue` 和 `ActiveMQTopic` 类完成配置。随后,利用 `JmsMessagingTemplate` 实现消息发送功能,并通过 Controller 和监听器实现点对点消息的生产和消费。最后,通过浏览器访问测试接口验证消息传递的成功性。
130 0
|
4月前
|
消息中间件 Java API
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ—— JMS 和 ActiveMQ 介绍
本文介绍如何在Spring Boot中集成ActiveMQ,首先阐述了JMS(Java消息服务)的概念及其作为与具体平台无关的API在异步通信中的作用。接着说明了JMS的主要对象模型,如连接工厂、会话、生产者和消费者等,并指出JMS支持点对点和发布/订阅两种消息类型。随后重点讲解了ActiveMQ,作为Apache开源的消息总线,它完全支持JMS规范,适用于异步消息处理。最后,文章探讨了在Spring Boot中使用队列(Queue)和主题(Topic)这两种消息通信形式的方法。
98 0
|
9月前
|
Java BI API
spring boot 整合 itextpdf 导出 PDF,写入大文本,写入HTML代码,分析当下导出PDF的几个工具
这篇文章介绍了如何在Spring Boot项目中整合iTextPDF库来导出PDF文件,包括写入大文本和HTML代码,并分析了几种常用的Java PDF导出工具。
1852 0
spring boot 整合 itextpdf 导出 PDF,写入大文本,写入HTML代码,分析当下导出PDF的几个工具
|
8月前
|
消息中间件 监控 Java
您是否已集成 Spring Boot 与 ActiveMQ?
您是否已集成 Spring Boot 与 ActiveMQ?
267 0
|
消息中间件 Java Kafka
SpringBoot实用开发篇第六章(整合第三方技术,ActiveMQ,RabbitMQ,RocketMQ,Kafka)
SpringBoot实用开发篇第六章(整合第三方技术,ActiveMQ,RabbitMQ,RocketMQ,Kafka)
|
消息中间件 监控 Java
使用Spring Boot结合ActiveMQ和MQTT实现消息的发送和接收
使用Spring Boot结合ActiveMQ和MQTT实现消息的发送和接收
958 3
|
消息中间件 Java Apache
使用Spring Boot实现与ActiveMQ的消息队列集成
使用Spring Boot实现与ActiveMQ的消息队列集成
|
消息中间件 Java Apache
使用Spring Boot实现与ActiveMQ的消息队列集成
使用Spring Boot实现与ActiveMQ的消息队列集成

热门文章

最新文章