2 ActiveMQ
2.1 Broker
2.1.1 Running Broker
ActiveMQ5.0 的二进制发布包中bin目录中包含一个名为activemq的脚本,直接运行这个脚本就可以启动一个broker。
此外也可以通过Broker Configuration URI或Broker XBean URI对broker进行配置,以下是一些命令行参数的例子: Example Description
activemq
Runs a broker using the default 'xbean:activemq.xml' as the broker configuration file.
activemq xbean:myconfig.xml
Runs a broker using the file myconfig.xml as the broker configuration file that is located in the classpath.
activemq xbean:file:./conf/broker1.xml
Runs
a broker using the file broker1.xml as the broker configuration file
that is located in the relative file path ./conf/broker1.xml
activemq xbean:file:C:/ActiveMQ/conf/broker2.xml
Runs
a broker using the file broker2.xml as the broker configuration file
that is located in the absolute file path C:/ActiveMQ/conf/broker2.xml
activemq broker:(tcp://localhost:61616, tcp://localhost:5000)?useJmx=true
Runs a broker with two transport connectors and JMX enabled.
activemq broker:(tcp://localhost:61616, network:tcp://localhost:5000)?persistent=false
Runs a broker with 1 transport connector and 1 network connector with persistence disabled.
BrokerService broker = BrokerFactory.createBroker(new URI(someURI)); BrokerService broker = BrokerFactory.createBroker(new URI(someURI)); someURI的可选值如下: URI schemeExampleDescriptionxbean:xbean:activemq.xmlSearches
the classpath for an XML document with the given URI (activemq.xml in
this case) which will then be used as the Xml Configurationfile:file:foo/bar/activemq.xmlLoads the given file (in this example foo/bar/activemq.xml) as the Xml Configurationbroker:broker:tcp://localhost:61616Uses the Broker Configuration URI to configure the broker
当使用XBean的配置方式的时候,需要指定一个xml配置文件,例如:
Java代码
然后在ActiveMQ的conf目录下创建一个访问控制文件和密码文件,如下:
conf/jmx.access:
# The "monitorRole" role has readonly access.
# The "controlRole" role has readwrite access.
monitorRole readonly
controlRole readwrite
conf/jmx.password:
# The "monitorRole" role has password "abc123".
# The "controlRole" role has password "abcd1234".
monitorRole abc123
controlRole abcd1234
然后修改ActiveMQ的bin目录下activemq的启动脚本,查找包含"SUNJMX="的一行如下:
REM
set SUNJMX=-Dcom.sun.management.jmxremote.port=1616
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
把它替换成
set
SUNJMX=-Dcom.sun.management.jmxremote.port=1616
-Dcom.sun.management.jmxremote.authenticate=true
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.password.file=%ACTIVEMQ_BASE%/conf/jmx.password
-Dcom.sun.management.jmxremote.access.file=%ACTIVEMQ_BASE%/conf/jmx.access
最后重启ActiveMQ和jconsole,这时候需要强制login。如果在启动activemq的过程中出现以下错误,那么需要为这个文件增加访问
控制。Windows平台上的具体解决方法请参考如下网址:http://java.sun.com/j2se/1.5.0/docs/guide
/management/security-windows.html
Error: Password file read access must be restricted: D:\apache-activemq-5.0.0\bin\../conf/jmx.password
2.1.3.2 Web Console
Web Console被集成到了ActiveMQ的二进制发布包中,因此缺省访问http://localhost:8161/admin即可访问Web Console。
在配置文件中,可以通过修改nioConnector的port属性来修改Web console的缺省端口:
Xml代码
出于安全性或者可靠性的考虑,Web Console
可以被部署到不同于ActiveMQ的进程中。例如把activemq-web-console.war部署到一个单独的web容器中
(Tomcat,Jetty等)。在ActiveMQ5.0的二进制发布包中不包含activemq-web-console.war,因此需要下载
ActiveMQ的源码,然后进入到${activemq.base}/src/activemq-web-console目录中执行mvn
instanll。如果一切正常,那么缺省会在${activemq.base}/src/activemq-web-console/target目录
中生成activemq-web-console-5.0.0.war。然后将activemq-web-console-5.0.0.war拷贝到
Tomcat的webapps目录中,并重命名成activemq-web-console.war。
需要注意的是,要将activemq-all-5.0.0.jar拷贝到WEB-INF\lib目录中(可能还需要拷贝jms.jar)。还要为Tomcat设置以下五个系统属性(修改catalina.bat文件):
set JAVA_OPTS=%JAVA_OPTS% -Dwebconsole.type="properties"
set JAVA_OPTS=%JAVA_OPTS% -Dwebconsole.jms.url="tcp://localhost:61616"
set JAVA_OPTS=%JAVA_OPTS% -Dwebconsole.jmx.url="service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi"
set JAVA_OPTS=%JAVA_OPTS% -Dwebconsole.jmx.role=""
set JAVA_OPTS=%JAVA_OPTS% -Dwebconsole.jmx.password=""
consumers, producers and connections starting and stopping
temporary destinations being created and destroyed
messages expiring on topics and queues
brokers sending messages to destinations with no consumers.
connections starting and stopping
Advisory Messages可以被想象成某种的管理通道,通过它你可以得到关于JMS
provider、producers、consumers和destinations的信息。Advisory
topics都使用ActiveMQ.Advisory.这个前缀,以下是目前支持的topics:
Client based advisories Advisory TopicsDescriptionActiveMQ.Advisory.ConnectionConnection start & stop messagesActiveMQ.Advisory.Producer.QueueProducer start & stop messages on a QueueActiveMQ.Advisory.Producer.TopicProducer start & stop messages on a TopicActiveMQ.Advisory.Consumer.QueueConsumer start & stop messages on a Queue
ActiveMQ.Advisory.Consumer.TopicConsumer start & stop messages on a Topic
在消费者启动/停止的Advisory Messages的消息头中有个consumerCount属性,他用来指明目前desination上活跃的consumer的数量。
Destination and Message based advisories Advisory TopicsDescriptionActiveMQ.Advisory.QueueQueue create & destroyActiveMQ.Advisory.TopicTopic create & destroyActiveMQ.Advisory.TempQueueTemporary Queue create & destroyActiveMQ.Advisory.TempTopicTemporary Topic create & destroyActiveMQ.Advisory.Expired.QueueExpired messages on a QueueActiveMQ.Advisory.Expired.TopicExpired messages on a TopicActiveMQ.Advisory.NoConsumer.QueueNo consumer is available to process messages being sent on a QueueActiveMQ.Advisory.NoConsumer.TopicNo consumer is available to process messages being sent on a Topic