`
ssxxjjii
  • 浏览: 938393 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用 ActiveMQ 示例

阅读更多

企业中各项目中相互协作的时候可能用得到消息通知机制。比如有东西更新了,可以通知做索引。

在 Java 里有 JMS 的多个实现。其中 apache 下的 ActiveMQ 就是不错的选择。还有一个比较热的是 RabbitMQ (是 erlang 语言实现的)。这里示例下使用 ActiveMQ

用 ActiveMQ 最好还是了解下 JMS

JMS 公共 点对点域 发布/订阅域
ConnectionFactory QueueConnectionFactory TopicConnectionFactory
Connection QueueConnection TopicConnection
Destination Queue Topic
Session QueueSession TopicSession
MessageProducer QueueSender TopicPublisher
MessageConsumer QueueReceiver TopicSubscriber

JMS 定义了两种方式:Quere(点对点);Topic(发布/订阅)。

ConnectionFactory 是连接工厂,负责创建Connection。

Connection 负责创建 Session。

Session 创建 MessageProducer(用来发消息) 和 MessageConsumer(用来接收消息)。

Destination 是消息的目的地。

详细的可以网上找些 JMS 规范(有中文版)。

下载 apache-activemq-5.3.0。http://activemq.apache.org/download.html, 解压,然后双击 bin/activemq.bat。运行后,可以在 http://localhost:8161/admin 观察。也有 demo,http://localhost:8161/demo。 把 activemq-all-5.3.0.jar 加入 classpath。

Jms 发送 代码:

  1. public static void main(String[] args) throws Exception {  
  2.     ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();  
  3.   
  4.     Connection connection = connectionFactory.createConnection();  
  5.     connection.start();  
  6.   
  7.     Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);  
  8.     Destination destination = session.createQueue("my-queue");  
  9.   
  10.     MessageProducer producer = session.createProducer(destination);  
  11.     for(int i=0; i<3; i++) {  
  12.         MapMessage message = session.createMapMessage();  
  13.         message.setLong("count"new Date().getTime());  
  14.         Thread.sleep(1000);  
  15.         //通过消息生产者发出消息  
  16.         producer.send(message);  
  17.     }  
  18.     session.commit();  
  19.     session.close();  
  20.     connection.close();  
  21. }  

Jms 接收代码:

  1. public static void main(String[] args) throws Exception {  
  2.     ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();  
  3.   
  4.     Connection connection = connectionFactory.createConnection();  
  5.     connection.start();  
  6.   
  7.     final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);  
  8.     Destination destination = session.createQueue("my-queue");  
  9.   
  10.     MessageConsumer consumer = session.createConsumer(destination);  
  11.     /*//listener 方式 
  12.     consumer.setMessageListener(new MessageListener() { 
  13.  
  14.         public void onMessage(Message msg) { 
  15.             MapMessage message = (MapMessage) msg; 
  16.             //TODO something.... 
  17.             System.out.println(" 收到消息:" + new Date(message.getLong("count"))); 
  18.             session.commit(); 
  19.         } 
  20.  
  21.     }); 
  22.     Thread.sleep(30000); 
  23.     */  
  24.     int i=0;  
  25.     while(i<3) {  
  26.         i++;  
  27.         MapMessage message = (MapMessage) consumer.receive();  
  28.         session.commit();  
  29.   
  30.         //TODO something....  
  31.         System.out.println("收到消 息:" + new Date(message.getLong("count")));  
  32.     }  
  33.   
  34.     session.close();  
  35.     connection.close();  
  36. }  

启动 JmsReceiver 和 JmsSender 可以在看输出三条时间信息。当然 Jms 还指定有其它格式的数据,如 TextMessage

结合 Spring 的 JmsTemplate 方便用:

xml:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  4.   
  5. <!-- 在非 web / ejb 容器中使用 pool 时,要手动 stop,spring 不会为 你执行 destroy-method 的方法  
  6.     <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">  
  7.         <property name="connectionFactory">  
  8.             <bean class="org.apache.activemq.ActiveMQConnectionFactory">  
  9.                 <property name="brokerURL" value="tcp://localhost:61616" />  
  10.             </bean>  
  11.         </property>  
  12.     </bean>  
  13. -->  
  14.     <bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
  15.         <property name="brokerURL" value="tcp://localhost:61616" />  
  16.     </bean>  
  17.     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
  18.         <property name="connectionFactory" ref="jmsFactory" />  
  19.         <property name="defaultDestination" ref="destination" />  
  20.         <property name="messageConverter">  
  21.             <bean class="org.springframework.jms.support.converter.SimpleMessageConverter" />  
  22.         </property>  
  23.     </bean>  
  24.   
  25.     <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">  
  26.         <constructor-arg index="0" value="my-queue" />  
  27.     </bean>  
  28.   
  29. </beans>  

sender:

  1. public static void main(String[] args) {  
  2.     ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:app*.xml");  
  3.   
  4.     JmsTemplate jmsTemplate = (JmsTemplate) ctx.getBean("jmsTemplate");  
  5.   
  6.     jmsTemplate.send(new MessageCreator() {  
  7.   
  8.         public Message createMessage(Session session) throws JMSException {  
  9.             MapMessage mm = session.createMapMessage();  
  10.             mm.setLong("count"new Date().getTime());  
  11.             return mm;  
  12.         }  
  13.   
  14.     });  
  15. }  

receiver:

  1. public static void main(String[] args) {  
  2.     ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:app*.xml");  
  3.   
  4.     JmsTemplate jmsTemplate = (JmsTemplate) ctx.getBean("jmsTemplate");  
  5.     while(true) {  
  6.         Map<String, Object> mm =  (Map<String, Object>) jmsTemplate.receiveAndConvert();  
  7.         System.out.println("收到消 息:" + new Date((Long)mm.get("count")));  
  8.     }  
  9. }  

注意:直接用 Jms 接口时接收了消息后要提交一下,否则下次启动接收者时还可以收到旧数据。有了 JmsTemplate 就不用自己提交 session.commit() 了。如果使用了 PooledConnectionFactory 要把 apache-activemq-5.3.0\lib\optional\activemq-pool-5.3.0.jar 加到 classpath

分享到:
评论

相关推荐

    使用ActiveMQ示例.pdf

    使用ActiveMQ示例.pdf

    使用ActiveMQ示例

    发布于2013-4-27企业中各项目中相互协作的时候可能用得到消息...这里示例下使用 ActiveMQJMS定义了两种方式:Quere(点对点);Topic(发布/订阅)。ConnectionFactory是连接工厂,负责创建Connection。Session创建M

    activeMQ 示例

    标题中的“ActiveMQ示例”指的是Apache ActiveMQ的使用实例,这是一个开源的消息中间件,用于在分布式系统中传输消息。ActiveMQ是Java消息服务(JMS)的实现,支持多种协议,如OpenWire、STOMP、AMQP和MQTT,能够...

    activeMQ示例 activeMQ demo,java分布式技术

    本示例中,使用maven管理,完美解决各种依赖问题,不需要自行配置,导入项目等待eclipse自行下载jar包后即可; 请将本maven项目引入你自己的maven项目中(在你自己的pom.xml文件中配置这个项目的gourp和id以及版本号)...

    activeMQ代码示例简单通信

    `readme.txt`可能包含了一些关于如何使用这些代码示例的说明,包括如何配置ActiveMQ服务器地址、如何创建消息以及如何启动和停止producer和consumer。开发者应按照这些指南进行操作,以便了解ActiveMQ在实际项目中的...

    activemq示例

    描述中提到的“activemq示例的源码,聊天室,实现多人聊天”,意味着这个压缩包可能包含了一个使用ActiveMQ构建的聊天室应用的源代码。这样的示例通常会展示如何利用ActiveMQ进行实时通信,创建一个可以处理多个用户...

    ActiveMQ例子

    压缩包中的 "ActiveMQ Demo" 可能包括了示例代码和指南,展示了如何创建和使用ActiveMQ。通常,它会涵盖以下步骤: 1. **配置连接**: 创建连接工厂以连接到ActiveMQ服务器。 2. **创建生产者**: 编写代码来创建消息...

    SpringActiveMQ入门示例

    总结起来,SpringActiveMQ入门示例教你如何在Spring应用中配置和使用ActiveMQ进行消息传递。通过这个示例,你可以学习到如何设置连接工厂,创建生产者和消费者,以及如何在Eclipse和Maven环境下运行和测试这个集成。...

    JAVA编程之Spring boot-activeMQ示例

    分别实现生产者-消费者模式和发布-订阅模式,作为java编程发送消息和消费消息的基础示例。 源码主要包含如下内容: 1.spring boot配置初始化activeMQ 2.队列类型queue,生产者发送队列消息,以及消费者消费相关队列...

    activeMQ示例

    总的来说,ActiveMQ示例展示了如何在Java环境中利用消息中间件进行高效、可靠的通信。通过服务器端和客户端的交互,结合监听器机制,我们可以实现各种消息传递模式,提高系统的灵活性和可扩展性。理解和掌握ActiveMQ...

    memcached和activeMQ的JAVA示例代码

    标题"memcached和activeMQ的JAVA示例代码"表明这是一个关于使用Java编程语言实现的,针对memcached缓存系统和activeMQ消息中间件的示例项目。这通常是为了帮助开发者理解如何在实际应用中整合这两种技术。 描述中的...

    activemq 入门示例代码

    **ActiveMQ 入门示例代码详解** ActiveMQ 是 Apache 开源组织开发的一款高效、可靠的开源消息中间件,它遵循 JMS(Java Message Service)规范,支持多种协议,如 AMQP、STOMP、OpenWire 等,广泛应用于分布式系统...

    ActiveMQ与Spring整合示例Demo

    将ActiveMQ与Spring整合,可以方便地在Spring应用中使用消息队列,实现异步处理和分布式通信。 **一、ActiveMQ安装与配置** 在Linux系统中安装ActiveMQ,首先需要下载ActiveMQ的二进制包,解压后将其放置在合适的...

    ActiveMQ完整项目示例

    在本项目示例中,我们将深入探讨如何使用MyEclipse 10开发基于ActiveMQ的消息通信应用。 1. **ActiveMQ基础概念** - **消息队列**: 消息队列是ActiveMQ的核心,它存储和转发消息,确保消息的可靠传输。 - **生产...

    activeMQ代码示例API通信

    ActiveMQ是一种开源的,实现了JMS规范的,面向消息(MOM)的中间件,为应用程序提供高效的、可扩展的、稳定的和安全的企业级消息通信。 网盘地址:链接: https://pan.baidu.com/s/1nvOcn0l 密码: 4esh 话说怎么上传...

    spring使用activeMQ实现消息发送

    在上述示例中,我们使用了队列,但根据需求,你可以选择适合的类型。 6. **消息持久化**:ActiveMQ支持消息的持久化,这意味着即使在服务器重启后,未被消费的消息也能恢复。通过配置`JmsTemplate`或ActiveMQ服务器...

    JMS 使用 ActiveMQ 传送文件

    2. **JMS 使用 ActiveMQ 传送文件.doc** - 这个文档应该直接涵盖了如何使用ActiveMQ进行文件传输的具体步骤,可能包括了创建消息、设置目的地、发送和接收文件的代码示例,以及如何处理错误和异常的情况。...

    ActiveMQ示例代码

    本示例代码提供了一个完整的Java项目,帮助开发者理解如何在实际应用中使用ActiveMQ。 在讲解ActiveMQ的基本概念和示例代码之前,我们先来了解下**JMS(Java Message Service)**。JMS是一种为应用程序提供创建、...

    ActiveMQ / RabbitMQ 示例

    本示例主要关注三个流行的消息队列服务:ActiveMQ、RabbitMQ以及AliyunMQ,它们都是基于Java消息服务(JMS)的实现。 首先,ActiveMQ是由Apache开发的开源消息代理,它是JMS 1.1规范的实现者。ActiveMQ支持多种协议...

    spring使用activemq

    本篇文章将详细探讨如何在Spring环境中整合并使用ActiveMQ,帮助开发者更好地理解和运用这两个强大的工具。 首先,让我们了解Spring与ActiveMQ整合的基础概念。Spring框架提供了对多种消息中间件的集成支持,包括...

Global site tag (gtag.js) - Google Analytics