Spring2.5整合ActiveMQ 5.2

简介:

项目环境:

JDK1.5

ActiveMQ5.2

所用的包都是ActiveMQ自带的。引用的包如下图:

27113551_E0eY.png

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

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

 

发一张图看看:

27113552_ZOdq.png


目录
相关文章
|
消息中间件 Java 数据格式
【报错】spring整合activeMQ,pom.xml文件缺架包,启动报错:Caused by: java.lang.ClassNotFoundException: org.apache.xbean.spring.context.v2.XBeanNamespaceHandler
spring版本:4.3.13 ActiveMq版本:5.15 ======================================================== spring整合activeMQ,pom.
1971 0
|
消息中间件 网络协议 Java
ActiveMQ系列:结合Spring,基于配置文件的使用ActiveMQ
从activemq脚本可以看出启动ActiveMQ实际是启动,bin文件夹下的其实activemq.jar 包中有一个类为Main,这就是active的启动入口,Main主要是加载lib目录和ClassPath,初始化 类加载器,委托给ShellCommand,由ShellCommand根据命令描述去执行,如果是Version和HELP, 则打印信息,若是启动命令,则通过XBeanBrokerFactory创建BrokerService
124 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实例详解
340 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应用程序开发。
2090 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版本为例。
2114 0
|
消息中间件 Java Spring
|
消息中间件 Java Spring
【ActiveMQ】2.spring Boot下使用ActiveMQ
在spring boot下使用ActiveMQ,需要一下几个条件   1.安装并启动了ActiveMQ,参考:http://www.cnblogs.com/sxdcgaq8080/p/7919489.
1286 0