`

activeMQ

    博客分类:
  • java
阅读更多

原文出处:http://blog.chenlb.com/2010/01/activemq-hello.html

<!-- google_ad_section_start -->

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

在 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. publicstaticvoidmain(String[]args)throwsException{
  2. ConnectionFactoryconnectionFactory=newActiveMQConnectionFactory();
  3. Connectionconnection=connectionFactory.createConnection();
  4. connection.start();
  5. Sessionsession=connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
  6. Destinationdestination=session.createQueue("my-queue");
  7. MessageProducerproducer=session.createProducer(destination);
  8. for(inti=0;i<3;i++){
  9. MapMessagemessage=session.createMapMessage();
  10. message.setLong("count",newDate().getTime());
  11. Thread.sleep(1000);
  12. //通过消息生产者发出消息
  13. producer.send(message);
  14. }
  15. session.commit();
  16. session.close();
  17. connection.close();
  18. }

Jms 接收代码:

  1. publicstaticvoidmain(String[]args)throwsException{
  2. ConnectionFactoryconnectionFactory=newActiveMQConnectionFactory();
  3. Connectionconnection=connectionFactory.createConnection();
  4. connection.start();
  5. finalSessionsession=connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
  6. Destinationdestination=session.createQueue("my-queue");
  7. MessageConsumerconsumer=session.createConsumer(destination);
  8. /*//listener方式
  9. consumer.setMessageListener(newMessageListener(){
  10. publicvoidonMessage(Messagemsg){
  11. MapMessagemessage=(MapMessage)msg;
  12. //TODOsomething....
  13. System.out.println("收到消息:"+newDate(message.getLong("count")));
  14. session.commit();
  15. }
  16. });
  17. Thread.sleep(30000);
  18. */
  19. inti=0;
  20. while(i<3){
  21. i++;
  22. MapMessagemessage=(MapMessage)consumer.receive();
  23. session.commit();
  24. //TODOsomething....
  25. System.out.println("收到消息:"+newDate(message.getLong("count")));
  26. }
  27. session.close();
  28. connection.close();
  29. }

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

结合 Spring 的 JmsTemplate 方便用:

xml:

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

sender:

  1. publicstaticvoidmain(String[]args){
  2. ApplicationContextctx=newFileSystemXmlApplicationContext("classpath:app*.xml");
  3. JmsTemplatejmsTemplate=(JmsTemplate)ctx.getBean("jmsTemplate");
  4. jmsTemplate.send(newMessageCreator(){
  5. publicMessagecreateMessage(Sessionsession)throwsJMSException{
  6. MapMessagemm=session.createMapMessage();
  7. mm.setLong("count",newDate().getTime());
  8. returnmm;
  9. }
  10. });
  11. }

receiver:

  1. publicstaticvoidmain(String[]args){
  2. ApplicationContextctx=newFileSystemXmlApplicationContext("classpath:app*.xml");
  3. JmsTemplatejmsTemplate=(JmsTemplate)ctx.getBean("jmsTemplate");
  4. while(true){
  5. Map<String,Object>mm=(Map<String,Object>)jmsTemplate.receiveAndConvert();
  6. System.out.println("收到消息:"+newDate((Long)mm.get("count")));
  7. }
  8. }

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

参考资料:

Hello ActiveMQ

Spring2.5整合ActiveMQ 5.2

spring-support

JMS 相关资料

分享到:
评论

相关推荐

    activeMQ收发工具.rar

    ActiveMQ是中国最流行的开源消息中间件之一,由Apache软件基金会开发。它基于Java Message Service (JMS) 规范,提供了可靠的消息传递功能,适用于分布式系统中的应用间通信。本压缩包“activeMQ收发工具.rar”包含...

    ActiveMQ高并发处理方案

    ### ActiveMQ高并发处理方案详解 #### 一、引言 在现代分布式系统中,消息队列作为异步通信的核心组件之一,对于提高系统的吞吐量、降低响应时间和实现服务解耦等方面起着至关重要的作用。Apache ActiveMQ作为一款...

    WebSocket协议接收ActiveMQ

    2. 客户端连接:客户端通过WebSocket API建立到ActiveMQ的连接,指定目标URL通常是ws://或者wss://(如果是加密连接)加上ActiveMQ服务器的地址和WebSocket端口。 3. 订阅主题或队列:连接建立后,客户端可以订阅想...

    apache-activemq-5.17.3

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

    jmx监控activeMQ监控

    jmx监控ActiveMQ监控 jmx(Java Management Extensions)是一种Java技术,为Java应用程序提供了管理和监控的功能。ActiveMQ是Apache软件基金会下的一个开源消息队列系统,提供了高效、可靠的消息传递服务。在生产...

    apache-activemq-5.16.5

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

    Jmeter测试ActiveMQ性能报告

    本报告详细阐述了使用JMeter对ActiveMQ进行性能测试的过程和结果,旨在评估ActiveMQ在JMS(Java消息服务)环境下的性能表现。JMeter作为一个强大的负载和性能测试工具,被广泛用于测试各种应用程序,包括消息中间件...

    ActiveMQ的activemq.xml详细配置讲解

    **ActiveMQ的activemq.xml配置详解** ActiveMQ是Apache软件基金会开发的一个开源消息代理,它遵循Java消息服务(JMS)规范,提供可靠的消息传递功能。`activemq.xml`是ActiveMQ的核心配置文件,它定义了服务器的...

    Linux下activeMQ的启动和停止.docx

    在Linux环境下,Apache ActiveMQ是一个广泛使用的开源消息代理和队列服务器,它是Java Message Service (JMS) 的实现,能够处理大量的并发消息传递。ActiveMQ提供了高可用性、可扩展性和稳定性,使得它成为分布式...

    apache-activemq-5.9.0 下载

    Apache ActiveMQ是开源社区中最流行的消息中间件之一,它基于Java消息服务(JMS)标准,提供高效、可靠的异步通信解决方案。ActiveMQ在企业级应用中广泛应用,因为它支持多种协议,如OpenWire、STOMP、AMQP、MQTT、...

    apache-activemq-5.15.9.rar

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

    ActiveMQ路由配置方式

    ActiveMQ路由配置方式 ActiveMQ路由配置是Apache ActiveMQ项目中的一种重要配置方式,它依赖另一个Apache项目Camel。ActiveMQ集成了Camel,启动时同时会启动Camel。通过Camel Web Console可以进行Routing配置。 ...

    ActiveMQ接受和发送工具.rar

    ActiveMQ是中国最流行的开源消息中间件之一,基于Java Message Service(JMS)规范,它提供了一个高可伸缩、高性能、稳定且灵活的消息传递平台。这个"ActiveMQ接受和发送工具.rar"压缩包包含了用于与ActiveMQ交互的...

    7道消息队列ActiveMQ面试题!

    ActiveMQ是一款非常流行的开源消息队列中间件,它实现了JMS(Java Message Service,Java消息服务)1.1规范,面向消息的中间件(Message Oriented Middleware,MOM)是指利用高效可靠的消息传递机制进行与平台无关的...

    Spring集成ActiveMQ配置

    Spring集成ActiveMQ是将Spring框架与ActiveMQ消息中间件相结合,实现异步处理和解耦应用程序的关键技术。在本文中,我们将深入探讨如何配置和使用这一组合,以及它在实际项目中的应用。 首先,让我们了解Spring框架...

    activemq自启动并设置用户名密码

    #### 一、ActiveMQ简介 ActiveMQ是一款非常流行的开源消息中间件,它基于Java语言开发,并且遵循了Java消息服务(JMS)规范。ActiveMQ提供了丰富的特性,包括持久化消息存储、事务支持、集群等功能。在企业级应用中,...

    springboot集成activemq实现消息接收demo

    而ActiveMQ是Apache出品的一款开源消息中间件,它遵循JMS(Java Message Service)规范,用于处理应用程序之间的异步通信。本教程将详细介绍如何在Spring Boot项目中集成ActiveMQ,实现消息接收的Demo。 首先,我们...

    ActiveMQ in Action pdf英文版+源代码

    ActiveMQ in Action pdf英文原版加源代码压缩包。 Apache ActiveMQ in Action is a thorough, practical guide to implementing message-oriented systems in Java using ActiveMQ. The book lays out the core of ...

    ActiveMQ消息服务器 v6.0.1.zip

    ActiveMQ是Apache软件基金会开发的一款开源消息中间件,它遵循开放消息传递标准(JMS,Java Message Service),用于在分布式系统中实现可靠的消息传递。在本文中,我们将深入探讨ActiveMQ v6.0.1的核心特性、应用...

    自己实现的ActiveMQ连接池和新版本ActiveMQ自带的连接池,封装好的工具类,可直接使用

    本资源提供的内容是关于ActiveMQ的连接池实现,分为两部分:一是作者自己实现的ActiveMQ连接池,二是新版本ActiveMQ自带的连接池。连接池是一种资源管理技术,通过复用已建立的数据库连接或网络连接,减少创建和销毁...

Global site tag (gtag.js) - Google Analytics