Spring2.5整合ActiveMQ 5.2

简介:

项目环境:

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.ApplicationContextimport org.springframework.context.support.ClassPathXmlApplicationContextimport org.springframework.jms.core.JmsTemplateimport javax.jms.Destinationimport javax.jms.JMSExceptionimport 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 = (TextMessagetemplate.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/beansxmlns: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

继续测试发现,接收端接收一条消息后不退出程序,而是继续等待,一旦有消息发送过来,就获取到,然后输出!

 

发一张图看看:


目录
相关文章
|
5月前
|
消息中间件 存储 Java
第15课: Spring Boot中集成ActiveMQ
第15课: Spring Boot中集成ActiveMQ
493 0
|
9月前
|
消息中间件 存储 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)。前者确保每条消息仅被一个消费者消费,后者允许多个消费者同时接收相同消息。
327 0
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——ActiveMQ安装
|
9月前
|
消息中间件 Java 微服务
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——发布/订阅消息的生产和消费
本文详细讲解了Spring Boot中ActiveMQ的发布/订阅消息机制,包括消息生产和消费的具体实现方式。生产端通过`sendMessage`方法发送订阅消息,消费端则需配置`application.yml`或自定义工厂以支持topic消息监听。为解决点对点与发布/订阅消息兼容问题,可通过设置`containerFactory`实现两者共存。最后,文章还提供了测试方法及总结,帮助读者掌握ActiveMQ在异步消息处理中的应用。
440 0
|
9月前
|
消息中间件 网络协议 Java
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ——ActiveMQ集成
本文介绍了在 Spring Boot 中集成 ActiveMQ 的详细步骤。首先通过引入 `spring-boot-starter-activemq` 依赖并配置 `application.yml` 文件实现基本设置。接着,创建 Queue 和 Topic 消息类型,分别使用 `ActiveMQQueue` 和 `ActiveMQTopic` 类完成配置。随后,利用 `JmsMessagingTemplate` 实现消息发送功能,并通过 Controller 和监听器实现点对点消息的生产和消费。最后,通过浏览器访问测试接口验证消息传递的成功性。
622 0
|
9月前
|
消息中间件 Java API
微服务——SpringBoot使用归纳——Spring Boot中集成ActiveMQ—— JMS 和 ActiveMQ 介绍
本文介绍如何在Spring Boot中集成ActiveMQ,首先阐述了JMS(Java消息服务)的概念及其作为与具体平台无关的API在异步通信中的作用。接着说明了JMS的主要对象模型,如连接工厂、会话、生产者和消费者等,并指出JMS支持点对点和发布/订阅两种消息类型。随后重点讲解了ActiveMQ,作为Apache开源的消息总线,它完全支持JMS规范,适用于异步消息处理。最后,文章探讨了在Spring Boot中使用队列(Queue)和主题(Topic)这两种消息通信形式的方法。
328 0
|
消息中间件 监控 Java
您是否已集成 Spring Boot 与 ActiveMQ?
您是否已集成 Spring Boot 与 ActiveMQ?
404 0
|
消息中间件 Java
springboot整合ActiveMQ(点对点+发布订阅)
springboot整合ActiveMQ(点对点+发布订阅)
|
消息中间件 Java Kafka
SpringBoot实用开发篇第六章(整合第三方技术,ActiveMQ,RabbitMQ,RocketMQ,Kafka)
SpringBoot实用开发篇第六章(整合第三方技术,ActiveMQ,RabbitMQ,RocketMQ,Kafka)
|
消息中间件 监控 Java
使用Spring Boot结合ActiveMQ和MQTT实现消息的发送和接收
使用Spring Boot结合ActiveMQ和MQTT实现消息的发送和接收
1277 3
|
消息中间件 Java Apache
使用Spring Boot实现与ActiveMQ的消息队列集成
使用Spring Boot实现与ActiveMQ的消息队列集成