一.介绍
官方解释为:Apache ActiveMQ is the most popular and powerful open source Message Broker and Enterprise Integration Patterns provider.
Apache ActiveMQ is fast, supports many Cross Language Clients and Protocols, comes with easy to use Enterprise Integration Patterns and many advanced features while fully supporting JMS 1.1 and J2EE 1.4. Apache ActiveMQ is released under the Apache 2.0 License.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。
下面是它的特性列表
1. 多种语言和协议编写客户端。语言: Java, C, C++, C#, Ruby, Perl, Python, PHP。应用协议: OpenWire,Stomp REST,WS Notification,XMPP,AMQP
2. 完全支持JMS1.1和J2EE 1.4规范 (持久化,XA消息,事务)
3. 对Spring的支持,ActiveMQ可以很容得内嵌到使用Spring的系统里面去,而且也支持Spring2.0的特性
4. 通过了常见J2EE服务器(如 Geronimo,JBoss 4, GlassFish,WebLogic)的测试,其中通过JCA 1.5 resource adaptors的配置,可以让ActiveMQ可以自动的部署到任何兼容J2EE 1.4 商业服务器上
5. 支持多种传送协议:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
6. 支持通过JDBC和journal提供高速的消息持久化
7. 从设计上保证了高性能的集群,客户端-服务器,点对点
8. 支持Ajax
9. 支持与Axis的整合
10. 可以很容易得调用内嵌JMS provider,进行测试
ActiveMQ的竞争者
1,其他开源 JMS provider(资料来自 java-source.net) :
jbossmq(jboss 4)
jboss messaging (jboss 5)
joram 2006-09-22
openjms
mantamq
ubermq
SomnifugiJMS
开源的JMS Provider大部分都已经停止发展了,剩下的几个都是找到了东家,和某种J2EE 服务器挂钩,比如jboss mq 与jboss,joram与jonas(objectweb组织),ActiveMQ 与Geronimo(ASF APACHE基金组织),而在这3个之间,从网络底层来看,只有ActiveMQ使用了NIO,单从这个角度来看ActiveMQ在性能上会有一定的优势。
2.商业JMS provider
IBM WebSphere MQ
BEA WebLogic JMS
Oracle AQ
NonStop Server for Java Message Service(JMS)
Sun Java System Message Queue
Sonic jms
TIBCO Enterprise For JMS
iLinkMQ (国内)
现在的商业J2EE 应用服务器大部分都会有JMS Provider的实现,毕竟应用服务器都已经花费不薄,也不在乎在里面送一个JMS Provider了,当然还是有独立的比如IBM WebSphere MQ,Sonic JMS ,前者肯定是商用MQ(这个概念不仅仅是JMS Provier了,只能说JMS 只是它提供的一个应用)中间的巨无霸了。
从这点来看,ActiveMQ明显的竞争者并不多,因为它是作为独立的开源JMS Provider出现的,很容易被用于多种结构设计中,使用ActiveMQ作为默认JMS Provider的开源项目有ServiceMix,Geronimo.
二.安装ActiveMQ
首先去http://activemq.apache.org/download.html 下载目前的最新稳定版本 5.1.0 release (http://activemq.apache.org/activemq-510-release.html),解压文件apache-activemq-5.1.0-bin.zip,目录如下:
+apache-activemq- 5.1.0
+bin (windows下面的bat和unix/linux下面的sh)
+conf (activeMQ配置目录,包含最基本的activeMQ配置文件)
+data (日志等文件所在地)
+docs (index,replease版本里面没有文档)
+example (几个例子)
+lib (activemMQ使用到的lib)
-activemq- 5.1.0 -all.jar (ActiveMQ的jar包,包含所有的类)
-LICENSE.txt
-NOTICE.txt
-README.txt
-user-guide.html
2.运行
打开命令行,切换到activeMQ的安装目录,输入bin/activemq启动,如果一切顺利,你就会看见类似下面的信息 (细节可能不一样,比如路径,或者jmx,jdbc信息)
D:/program/server/activemq5.1> bin/activemq
ACTIVEMQ_HOME: D:/program/server/activemq5.1/bin/..
ACTIVEMQ_BASE: D:/program/server/activemq5.1/bin/..
Loading message broker from: xbean:activemq.xml
INFO BrokerService - Using Persistence Adapter: AMQPersistence
Adapter(D:/program/server/activemq5.1/bin/../data)
INFO BrokerService - ActiveMQ 5.1.0 JMS Message Broker (localh
ost) is starting
INFO BrokerService - For help or more information please see:
http://activemq.apache.org/
INFO AMQPersistenceAdapter - AMQStore starting using directory: D:/pro
gram/server/activemq5.1/bin/../data
INFO KahaStore - Kaha Store using data directory D:/progra
m/server/activemq5.1/bin/../data/kr-store/state
INFO AMQPersistenceAdapter - Active data files: []
WARN AMQPersistenceAdapter - The ReferenceStore is not valid - recover
ing ...
INFO KahaStore - Kaha Store successfully deleted data dire
ctory D:/program/server/activemq5.1/bin/../data/kr-store/data
INFO AMQPersistenceAdapter - Journal Recovery Started from: DataManage
r:(data-)
INFO AMQPersistenceAdapter - Recovered 0 operations from redo log in 0
.015 seconds.
INFO AMQPersistenceAdapter - Finished recovering the ReferenceStore
INFO KahaStore - Kaha Store using data directory D:/progra
m/server/activemq5.1/bin/../data/kr-store/data
INFO TransportServerThreadSupport - Listening for connections at: tcp://rj-we
ijianjun:61616
INFO TransportConnector - Connector openwire Started
INFO TransportServerThreadSupport - Listening for connections at: ssl://rj-we
ijianjun:61617
INFO TransportConnector - Connector ssl Started
INFO TransportServerThreadSupport - Listening for connections at: stomp://rj-
weijianjun:61613
INFO TransportConnector - Connector stomp Started
INFO TransportServerThreadSupport - Listening for connections at: xmpp://rj-w
eijianjun:61222
INFO TransportConnector - Connector xmpp Started
INFO NetworkConnector - Network Connector default-nc Started
INFO BrokerService - ActiveMQ JMS Message Broker (localhost, I
D:rj-weijianjun-2072-1226368584996-0:0) started
INFO log - Logging to org.slf4j.impl.JCLLoggerAdapte
r(org.mortbay.log) via org.mortbay.log.Slf4jLog
INFO log - jetty- 6.1.9
INFO WebConsoleStarter - ActiveMQ WebConsole initialized.
INFO /admin - Initializing Spring FrameworkServlet 'dis
patcher'
INFO log - ActiveMQ Console at http:// 0.0.0 .0:8161/a
dmin
INFO log - ActiveMQ Web Demos at http:// 0.0.0 .0:8161
/demo
INFO log - RESTful file access application at http:/
/ 0.0.0 .0:8161/fileserver
INFO log - Started SelectChannelConnector@ 0.0.0 .0:81
61
INFO FailoverTransport - Successfully connected to tcp://localhost
:61616
INFO TransportConnector - Connector vm://localhost Started
INFO FailoverTransport - Successfully connected to tcp://localhost
:61616
INFO FailoverTransport - Successfully connected to tcp://localhost
:61616
几个小提示
1. 这个仅仅是最基础的ActiveMQ的配置,很多地方都没有配置因此不要直接使用这个配置用于生产系统
2. 有的时候由于端口被占用,导致ActiveMQ错误,ActiveMQ可能需要以下端口1099(JMX),61616(默认的TransportConnector)
3. 如果没有物理网卡,或者MS的LoopBackAdpater Multicast会报一个错误
3. 测试你的ActiveMQ
由于ActiveMQ是一个独立的jms provider,所以我们不需要其他任何第三方服务器就可以马上做我们的测试了.
If ActiveMQ is up and running without problems, the Window's console window or the Unix command shell will display information similar to the following log line:
INFO ActiveMQ JMS Message Broker (ID:apple-s-Computer.local-51222-1140729837569-0:0) has started
ActiveMQ's default port is 61616. From another window run netstat and search for port 61616.
From a Windows console, type:
netstat -an|find "61616"
OR
From a Unix command shell, type:
netstat -an|grep 61616
There are various ways to monitor ActiveMQ. If you are on version 4.2 or later of ActiveMQ you can then monitor ActiveMQ using the Web Console by pointing your browser at
Or you can use the JMX support to view the running state of ActiveMQ.
For both Windows and Unix installations, terminate ActiveMQ by typing "CTRL-C" in the console or command shell in which it is running.
If ActiveMQ was started in the background on Unix, the process can be killed, with the following:
ps -ef|grep activemq
kill [PID]
where [PID] is the process id of the ActiveMQ process.
4.运行example
编译 example目录下面的程序
ProducerTool/ConsumerTool 是JMS参考里面提到的典型应用,Producer产生消息,Consumer消费消息。
而且这个例子还可以加入参数帮助你测试刚才启动的本地ActiveMQ或者是远程的ActiveMQ
ProducerTool [url] broker的地址,默认的是tcp://localhost:61616
[true|flase] 是否使用topic,默认是false
[subject] subject的名字,默认是TOOL.DEFAULT
[durabl] 是否持久化消息,默认是false
[messagecount] 发送消息数量,默认是10
[messagesize] 消息长度,默认是255
[clientID] durable为true的时候,需要配置clientID
[timeToLive] 消息存活时间
[sleepTime] 发送消息中间的休眠时间
[transacte] 是否采用事务
ConsumerTool [url] broker的地址,默认的是tcp://localhost:61616
[true|flase] 是否使用topic,默认是false
[subject] subject的名字,默认是TOOL.DEFAULT
[durabl] 是否持久化消息,默认是false
[maxiumMessages] 接受最大消息数量,0表示不限制
[clientID] durable为true的时候,需要配置clientID
[transacte] 是否采用事务
[sleepTime] 接受消息中间的休眠时间,默认是0,onMeesage方法不休眠
[receiveTimeOut] 接受超时
我们这样可以使用:
java -classpath ./activemq-5.1-all.jar;example/bin ProducerTool tcp://192.168.3.142:61616 test.mysubject
java -classpath ./ activemq-5.1-all.jar;example/bin ConsumerTool tcp://192.168.3.142:61616 test.mysubject
当然你可以使用上面的参数进行更复杂的测试,持久,事务
如果出现下面的信息,恭喜你,你的ActiveMQ实例已经能够工作了
Connecting to URL: tcp://192.168.3.142:61616
Publishing a Message with size 255 to queue: TOOL.DEFAULT
Using non-durable publishing
Sleeping between publish 0 ms
Sending message: Message: 0 sent at: Thu Feb 08 15:05:34 CST 2007 ...
Sending message: Message: 1 sent at: Thu Feb 08 15:05:34 CST 2007 ...
Connecting to URL: tcp://192.168.3.142:61616
Consuming queue: test.mysubject
Using non-durable subscription
Received: Message: 0 sent at: Thu Feb 08 14:51:34 CST 2007 ...
Received: Message: 1 sent at: Thu Feb 08 14:51:34 CST 2007 ...
一个非常有用的ActiveMQ介绍:
http://whitesock.javaeye.com/blog/164925