分类目录

链接

2016 年 8 月
1234567
891011121314
15161718192021
22232425262728
293031  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > MQ > 正文
ActiveMQ REST Management API
MQ 暂无评论 阅读(1,062)

ActiveMQ的管理接口是基于JMX的(参见 JMX操作ActiveMQ(1)、JMX操作ActiveMQ(2)),通过管理接口我们可以与Broker交互,查询各种Broker状态、统计数据,浏览连接、消费者、生产者,以及管理消息。

虽然ActiveMQ通过CMS(支持c++)、NMS(支持.net)以及多种protocol(STOMP、MQTT、AMQP等)支持了多种平台环境下的消息发送接收,但是由于JMX是Java专有的技术,导致ActiveMQ JMX管理接口一直不能使用在其他环境。

从ActiveMQ5.8.0起,引入了jolokia库,使得JMX接口可以直接转换成REST接口,JMX管理接口就成了现在的REST Management API。简单说就是可以通过HTTP URL请求的方式,操作原来的所有JMX接口,读写Mbean的属性,执行Mbean的方法,并且拿到返回的JSON结果。解析这个JSON即可拿到我们想要的数据。

从 JMX操作ActiveMQ(1)、JMX操作ActiveMQ(2) 我们可以看到JMX管理接口大概的结构,jolokia库通过使用URL来传递参数的方式来转换REST请求变成实际的JMX调用。

1.Jolokia的REST API

Jolokia通过如下方式定义了JMX的REST API操作接口

Mbean的属性:

读取:baseURL/read/Mbean的全称/属性名称

写入:baseURL/write/Mbean的全称/属性名称

Mbean方法的调用

调用:baseURL/exec/Mbean的全称/方法名/参数1,参数2,参数3

如果存在同名方法,则需要加方法签名,例如:

http://www.xxx.com/jolokia/exec/org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName=a/removeMatchingMessages(java.lang.String)/JMSType='1'

Jolokia官方文档:http://www.jolokia.org/reference/html/protocol.html

2.安全验证

默认情况下,web console开了安全验证。可以简单模拟登陆后,再访问jolokia API。

比如C#下使用WebRequest的例子,

1)WebRequest带上credentials访问 http://localhost:8161/admin/ 即可模拟登陆(也可以模拟form提交用户名密码到http://localhost:8161/hawtio/#/login)
2)WebRequest保持cookieContainer和Authorization
3)WebRequest再访问jolokia API接口即可拿到数据

代码参见:http://www.oschina.net/code/snippet_236747_33202

3.实际操作的例子

启动ActiveMQ5.9.0,然后在控制台创建一个新的queue,名称为a。

往队列里发送一个type=1的消息


3.1. 查看所有的Queues:
http://localhost:8161/hawtio/jolokia/read/org.apache.activemq:brokerName=localhost,type=Broker/Queues

{"timestamp":1392110578,"status":200,"request":{"mbean":"org.apache.activemq:brokerName=localhost,type=Broker","attribute":"Queues","type":"read"},"value":[{"objectName":"org.apache.activemq:brokerName=localhost,destinationName=a,destinationType=Queue,type=Broker"}]}

从JSON中拿到所有objectName里的destinationName即是所有的Queue了。

3.2. 查看消息:

http://localhost:8161/hawtio/jolokia/exec/org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName=a/browse()/

浏览器返回:

{"timestamp":1392111690,"status":200,"request":{"operation":"browse()","mbean":"org.apache.activemq:brokerName=localhost,destinationName=a,destinationType=Queue,type=Broker","type":"exec"},"value":[{"JMSCorrelationID":"","JMSMessageID":"ID:kimmking-13533-1392097198428-3:3:1:1:4","OriginalDestination":null,"JMSDeliveryMode":"NON-PERSISTENT","BrokerPath":"null","JMSXUserID":null,"FloatProperties":{},"StringProperties":{},"JMSReplyTo":null,"JMSTimestamp":"2014-02-11T17:39:35+08:00","JMSDestination":"queue:\/\/a","JMSType":"1","JMSRedelivered":false,"BooleanProperties":{},"ByteProperties":{},"PropertiesText":"{}","JMSExpiration":0,"DoubleProperties":{},"JMSPriority":0,"JMSXGroupSeq":0,"LongProperties":{},"ShortProperties":{},"IntProperties":{},"JMSXGroupID":null,"Text":"Enter some text here for the message body..."}]}

value即是消息列表。

3.3. 从队列a中删除掉type为1的消息:
http://localhost:8161/hawtio/jolokia/exec/org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName=a/removeMatchingMessages(java.lang.String)/JMSType='1'

浏览器返回:

{"timestamp":"1392113890","status":200,"request":{"operation":"removeMatchingMessages(java.lang.String)","mbean":"org.apache.activemq:brokerName=localhost,destinationName=a,destinationType=Queue,type=Broker","arguments":["JMSType='1'"],"type":"exec"},"value":1}

value为1即为成功删除1条消息。

ps:发现调用列队的browseMessages方法会出错,一个bug。

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:ActiveMQ REST Management API | Bruce's Blog

发表评论

留言无头像?