activemq-in-action学习

本文介绍 ActiveMQ 的启动过程、内部结构及消息收发示例。详细讲解了通过 Maven 运行示例代码的方法,并展示了如何配置 Broker 以记录消息传输详情。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



activemq-in-action出版时间2011年,是5年前出版的技术参考书,目前没有更新版本。书中示例是基于activemq-5.3.2。书中使用的版本是activemq-5.4.1,

目前activemq官网上已更新到5.14.0

各个版本提供的特性在http://activemq.apache.org/download-archives.html


activemq是java实现的消息中间件,除了实现jms spec, 还提供了额外的特性,如broker feature, client feature.


启动activeMQ

首先在$activemq_home\bin目录下执行activemq.bat脚本启动它。


D:\programs\apache-activemq-5.4.3\bin>activemq
Java Runtime: Oracle Corporation 1.8.0_51 D:\programs\Java\jdk1.8.0_51\jre
  Heap sizes: current=94208k  free=89235k  max=466432k
    JVM args: -Dcom.sun.management.jmxremote -Xmx512M -Dorg.apache.activemq.UseDedicatedTaskRunner=true -Djava.util.logging.config.file=logg
ing.properties -Dactivemq.classpath=D:\programs\apache-activemq-5.4.3\bin\../conf;D:\programs\apache-activemq-5.4.3\bin\../conf; -Dactivemq.
home=D:\programs\apache-activemq-5.4.3\bin\.. -Dactivemq.base=D:\programs\apache-activemq-5.4.3\bin\..
ACTIVEMQ_HOME: D:\programs\apache-activemq-5.4.3\bin\..
ACTIVEMQ_BASE: D:\programs\apache-activemq-5.4.3\bin\..
Loading message broker from: xbean:activemq.xml
 INFO | Refreshing org.apache.activemq.xbean.XBeanBrokerFactory$1@2d209079: startup date [Sat Mar 05 21:07:52 CST 2016]; root of context hie
rarchy
 WARN | destroyApplicationContextOnStop parameter is deprecated, please use shutdown hooks instead
 INFO | PListStore:D:\programs\apache-activemq-5.4.3\bin\..\data\localhost\tmp_storage started
 INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[D:\programs\apache-activemq-5.4.3\bin\..\data\kahadb]
 INFO | KahaDB is version 3
 INFO | Recovering from the journal ...
 INFO | Recovery replayed 1 operations from the journal in 0.021 seconds.
 INFO | ActiveMQ 5.4.3 JMS Message Broker (localhost) is starting
 INFO | For help or more information please see: http://activemq.apache.org/
 INFO | Listening for connections at: tcp://localhost:61616
 INFO | Connector openwire Started
 INFO | ActiveMQ JMS Message Broker (localhost, ID:shuhang-58981-1457183275402-0:1) started
 INFO | jetty-7.1.6.v20100715
 INFO | ActiveMQ WebConsole initialized.
 INFO | Initializing Spring FrameworkServlet 'dispatcher'
 INFO | ActiveMQ Console at http://0.0.0.0:8161/admin
 INFO | ActiveMQ Web Demos at http://0.0.0.0:8161/demo
 INFO | RESTful file access application at http://0.0.0.0:8161/fileserver
 INFO | Started SelectChannelConnector@0.0.0.0:8161


查看消息收发情况

http://localhost:8161/admin



书中第三章给了两个典型的例子,可以通过maven exec:java演示运行情况。

注:在github上输入activemq in action可以找到书中实例源码,下载解压后用maven编译。


股票询价系统,Publish-subscribe模式, 基于topic

mvn exec:java  -Dexec.mainClass=org.apache.activemq.book.ch3.portfolio.Consumer  -Dexec.args="CSCO ORCL"

mvn exec:java -Dexec.mainClass=org.apache.activemq.book.ch3.portfolio.Publisher -Dexec.args="CSCO ORCL"



point-to-point模式,基于queue
mvn exec:java -Dexec.mainClass=org.apache.activemq.book.ch3.jobs.Publisher

mvn exec:java -Dexec.mainClass=org.apache.activemq.book.ch3.jobs.Consumer


API

公共部分

javax.jms.Connection;
javax.jms.ConnectionFactory;
javax.jms.Destination;
javax.jms.JMSException;
javax.jms.Session;

org.apache.activemq.ActiveMQConnectionFactory;


发消息

javax.jms.Message;
javax.jms.MessageProducer;
javax.jms.ObjectMessage;

javax.jms.MapMessage;


收消息

javax.jms.MessageConsumer;

javax.jms.MessageListener;


================== ActiveMQ内部结构 ====================


connector

理解 connector URIs, 通常的格式:

<scheme>://<authority><path><?query>

例子

tcp://localhost:61616?trace=true


Composite URI

static:(tcp://host1:61616,tcp://host2:61616)



配置Transport connectors的例子

启动方式: bin/activemq  xbean:conf/activemq-demo.xml



ch4 传入brokerURL


mvn exec:java -Dexec.mainClass=org.apache.activemq.book.ch4.Publisher -Dexec.args="tcp://localhost:61616 CSCO ORCL"

mvn exec:java -Dexec.mainClass=org.apache.activemq.book.ch4.Consumer -Dexec.args="tcp://localhost:61616 CSCO ORCL"



Wire Protocol

Before exchanging messages over the network, we need to serialize them to a suitable form. Messages must be serialized in and out of a byte sequence to be sent over  the wire using what’s known as a wire protocol.


The default wire protocol used in  ActiveMQ is called OpenWire.


As we’ve seen in previous sections, a default broker configuration starts the TCP transport listening for client connections on port 61616. The TCP connector URI uses
the following syntax:
tcp://hostname:port?key=value&key=value


以下trace=true让activemq的broker以日志的方式记录下所有发送的commands,对debug追踪问题有帮助

<transportConnectors>
<transportConnector name="tcp"
uri="tcp://localhost:61616?trace=true"/>
</transportConnectors>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值