`
bloodwolf_china
  • 浏览: 131066 次
社区版块
存档分类
最新评论

JMS Apache ActiveMQ使用(3)

    博客分类:
  • JMS
阅读更多
还有工作就是如何接受JMS Message并进行处理了,spring中也有现成的框架进行处理MessageListenerAdapter和DefaultMessageListenerContainer,我们只要写一个普通类编写处理消息的逻辑,具体如何接收消息都由Spring完成。
spring-listener-jms.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
		"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
	<import resource="spring-jms.xml"/>

	<bean id="messageListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
		<constructor-arg>
			<bean class="com.viva.server.message.jms.UserRegisterListener"/>
		</constructor-arg>
		<property name="defaultListenerMethod" value="onRegister"/>
		<property name="messageConverter" ref="messageConvert"/>
	</bean>

	<bean id="queueListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="jmsFactory"/>
		<property name="destination" ref="queueDestination"/>
		<property name="messageListener" ref="messageListener"/>
	</bean>

	<bean id="topicListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="jmsFactory"/>
		<property name="destination" ref="topicDestination"/>
		<property name="messageListener" ref="messageListener"/>
	</bean>

</beans>

从配置文件可以看出配置了一个Topic监听器和Queue监听器,实际的监听类就是一个UserRegisterListener,由它处理用户注册事件。
UserRegisterListener.java代码
public class UserRegisterListener {
	public void onRegister(VivaUserRegisterMessage registerMessage){
		System.out.println("User Register,tel="+ registerMessage.getTel()+ ",time="+registerMessage.getRegisterTime());	
	}
}

最后写一个JUnit测试用户来测试一下代码
JMSTestor.java
public class JMSTestor extends TestCase {
	private ApplicationContext context;

	public void testQueueSend(){
		context = new ClassPathXmlApplicationContext("spring-jms.xml");
		JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("queueTemplate");
		VivaUserRegisterMessage msg = new VivaUserRegisterMessage();
		msg.setRegisterTime(new Date());
		msg.setTel("1385199XXXX");
		msg.setUserId(1);
		jmsTemplate.convertAndSend(msg);
	}

	public void testTopicSend(){
		context = new ClassPathXmlApplicationContext("spring-jms.xml");
		JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("topicTemplate");
		VivaUserRegisterMessage msg = new VivaUserRegisterMessage();
		msg.setRegisterTime(new Date());
		msg.setTel("1385199XXXX");
		msg.setUserId(1);
		jmsTemplate.convertAndSend(msg);
	}

	public void testListener()throws Exception{
		context = new ClassPathXmlApplicationContext("spring-listener-jms.xml");
		Thread.sleep(1000*60*30);
	}

	
}

执行testTopicSend发送JMS Message到Topic
执行testQueueSend发送JMS Message到Queue
执行testListener启动Topic和Queue监听器处理JMS Message
完成!另外ActiveMQ集成了Jetty服务,可以访问地址http://localhost:8161/admin/查看具体信息
3
1
分享到:
评论

相关推荐

    JMS Apache ActiveMQ 安装包

    Apache ActiveMQ JMS实现

    JMS 使用 ActiveMQ 传送文件

    ActiveMQ是Apache软件基金会开发的一个开源JMS提供者,它提供了高性能、可靠的跨语言消息传递服务。 **描述:** 尽管描述中并未给出具体信息,但我们可以推断这篇博文可能详细介绍了如何使用JMS与ActiveMQ结合来...

    apache-activemq Linux版本

    Apache ActiveMQ是业界广泛使用的开源消息中间件,尤其在Linux环境下表现出色。它基于Java语言开发,遵循Apache软件基金会的许可证,并且实现了多种消息传递协议,包括OpenWire、STOMP、AMQP和XMPP等。在Linux系统上...

    apache-activemq-5.9.0-bin

    3. **多种协议支持**:除了JMS,ActiveMQ还支持STOMP、AMQP、MQTT、WS-Notification等协议,允许跨平台和跨语言的通信。 4. **持久化**:ActiveMQ提供了多种持久化选项,包括基于文件的KahaDB、LevelDB以及JDBC...

    apache-activemq-5.15.9.rar

    Apache ActiveMQ是Apache软件基金会开发的一个开源消息中间件,它基于Java Message Service (JMS) 规范,提供高效、可靠的消息传递服务。在本文中,我们将深入探讨Apache ActiveMQ,特别是针对“apache-activemq-...

    Apache+ActiveMQ教程

    Apache ActiveMQ 在Java消息服务(JMS)领域扮演着重要角色,为分布式系统提供可靠的消息传递机制。本教程将深入探讨如何利用Apache ActiveMQ搭建、配置和集成到Spring框架中,以实现高效的数据通信。 首先,我们...

    apache-activemq-5.16.5

    Apache ActiveMQ是业界广泛使用的开源消息中间件,它基于Java消息服务(JMS)标准,提供了高度可扩展、可靠的异步通信能力。标题"apache-activemq-5.16.5"指的是该软件的一个特定版本,即5.16.5版本,通常每个新版本...

    ApacheCamel-JMS-ActiveMQ

    在"Apache Camel JMS ActiveMQ"的使用样例中,我们有两个主要的场景: 1. **从本地读取信息推送到MQ中**:这一部分涉及到了Apache Camel的数据交换模型,即从本地源(可能是文件、数据库或任何其他数据源)读取信息...

    spring整合jms+activemq

    ActivemQ是Apache软件基金会的一个项目,它实现了JMS规范,提供了一个高效、可靠的中间件服务,用于处理消息队列。本文将深入探讨如何在Spring 3.0中整合JMS与ActivemQ,以及它们在实际应用中的关键知识点。 首先,...

    一头扎进JMS之ActiveMQ系列

    在提供的“一头扎进JMS之ActiveMQ视频教程”中,你将学习如何配置和使用ActiveMQ,包括安装和启动服务、创建和管理消息队列、使用生产者和消费者发送与接收消息、理解不同消息模式的工作原理,以及如何利用ActiveMQ...

    使用jms 操作ActiveMQ

    ActiveMQ是Apache软件基金会开发的一个开源消息中间件,它是JMS的实现之一,广泛应用于企业级应用中。 本教程将围绕"使用JMS操作ActiveMQ"这一主题,详细阐述如何通过JMS与ActiveMQ进行交互,包括创建生产者、消费...

    Spring 实现远程访问详解——jms和activemq

    同时Apache ActiveMq是速度快,支持多种跨语言客户端和协议,同时配有易于使用的企业集成模式和优秀的特性,并且支持JMS1.1和J2EE1.4。具体特性见官网:http://activemq.apache.org/ 2. 什么是JMS JMS的全称是Java ...

    SpringJMS整合ActiveMQ

    详细内容: SpringJMS整合ActiveMQ.doc 详细说明文档 apache-activemq-5.8.0-bin.zip ActiveMQ安装包 JMSTest.rar MyEclipse8.5下web工程

    JMS+activeMQ 教程

    &lt;a href=http://activemq.apache.org/version-5-getting-started.html &gt;http://activemq.apache.org/version-5-getting-started.html&lt;/a&gt; &lt;a href=http://java.sun.com/j2ee/1.4/docs/tutorial/doc/index.html&gt;...

    JMS-ActiveMQ入门实例

    在众多JMS实现中,Apache ActiveMQ是一个非常流行的开源消息代理。ActiveMQ支持多种协议,如OpenWire、STOMP、AMQP、MQTT、WS和HTTP,并且能够与其他系统集成,例如通过RESTful API或WebSockets。此外,ActiveMQ还...

    Apache ActiveMQ 入门最简单例子

    例如,在Java环境中,我们可以使用JMS(Java Message Service)API与ActiveMQ交互: 1. **消息生产者**:生产者创建一个连接到ActiveMQ服务器的ConnectionFactory,然后创建一个Session,用于发送消息。Session可以...

    JMS-activemq 实例(分ppt,eclipse工程,说明三部分)

    3. **Eclipse工程** - `JMS` 文件可能是一个Eclipse项目,用于演示如何在实际开发环境中集成和使用JMS与ActiveMQ。这个工程可能包含了配置文件(如Spring的XML配置)、消息生产者和消费者的Java代码,以及必要的依赖...

    Instant Apache ActiveMQ Messaging Application Development How-to源码

    Apache ActiveMQ是Apache软件基金会开发的一个开源消息中间件,它基于Java消息服务(JMS)规范,用于在分布式系统中实现可靠的消息传递。本指南主要关注如何使用ActiveMQ开发消息应用,通过`Instant Apache ActiveMQ...

    Apache ActiveMQ教程 JMS 整合Tomcat

    ### Apache ActiveMQ与JMS整合Tomcat:深入解析与实践 #### 一、Apache ActiveMQ:强大而灵活的开源消息中间件 Apache ActiveMQ作为一款成熟的开源消息中间件,不仅遵循了JMS 1.1规范,还兼容J2EE 1.4以上的标准,...

    apache-activemq-5.17.3

    Apache ActiveMQ是开源的、基于Java消息服务(JMS)的应用服务器,它是Apache软件基金会的一部分。这个名为"apache-activemq-5.17.3"的压缩包包含了ActiveMQ的5.17.3版本,这是一个稳定且功能丰富的发布版本。在深入...

Global site tag (gtag.js) - Google Analytics