java操作activemq queues_ActiveMQ REST Management API

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即可拿到我们想要的数据。

jolokia库通过使用URL来传递参数的方式来转换REST请求变成实际的JMX调用。

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‘

实际操作的例子

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

5ccfc18dc04e5f427f1dc7e3226a0458.png

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

{"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了。

2. 查看消息:

浏览器返回:

{"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. 从队列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。

原文:http://blog.csdn.net/kimmking/article/details/19081973

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值