java activemq jmx_JMX操作ActiveMQ(1)

我们知道ActiveMQ broker的管理接口是通过JMX方式提供的。

一个简单的访问方式就是通过jconsole,输入

service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi

需要注意的是:

1、默认JMX功能是没有打开的,需要在activemq.xml的broker配置上添加useJmx="true"

2、需要在managementContext里,修改为createConnector="true",(同时这里也可以修改jmx的端口和domain)

1372147241_4166.jpg

1372148003_3071.jpg

通过jconsole来操作还是不太方便。特别是某些时候我们需要把对broker和queue的管理集成到我们的管理系统中去。

这时候我们就需要通过JMX的编程接口来与broker进行交互了。

可以先写一个小程序,看看broker的jmx中都提供了什么东西。

package kk;

import java.util.Iterator;

import java.util.Set;

import javax.management.MBeanAttributeInfo;

import javax.management.MBeanInfo;

import javax.management.MBeanOperationInfo;

import javax.management.MBeanServerConnection;

import javax.management.ObjectInstance;

import javax.management.ObjectName;

import javax.management.remote.JMXConnector;

import javax.management.remote.JMXConnectorFactory;

import javax.management.remote.JMXServiceURL;

public class TestJMX {

public static void main(String[] args) throws Exception {

String surl = "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi";

JMXServiceURL url = new JMXServiceURL(surl);

JMXConnector jmxc = JMXConnectorFactory.connect(url, null);

MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();

System.out.println("Domains:---------------");

String domains[] = mbsc.getDomains();

for (int i = 0; i < domains.length; i++) {

System.out.println("\tDomain[" + i + "] = " + domains[i]);

}

System.out.println("all ObjectName:---------------");

Set set = mbsc.queryMBeans(null, null);

for (Iterator it = set.iterator(); it.hasNext();) {

ObjectInstance oi = (ObjectInstance) it.next();

System.out.println("\t" + oi.getObjectName());

}

System.out.println("org.apache.activemq:BrokerName=localhost,Type=Broker:---------------");

ObjectName mbeanName = new ObjectName("org.apache.activemq:BrokerName=localhost,Type=Broker");

MBeanInfo info = mbsc.getMBeanInfo(mbeanName);

System.out.println("Class: " + info.getClassName());

if (info.getAttributes().length > 0){

for(MBeanAttributeInfo m : info.getAttributes())

System.out.println("\t ==> Attriber:" + m.getName());

}

if (info.getOperations().length > 0){

for(MBeanOperationInfo m : info.getOperations())

System.out.println("\t ==> Operation:" + m.getName());

}

jmxc.close();

}

}

输出结果:

Domains:---------------

Domain[0] = JMImplementation

Domain[1] = com.sun.management

Domain[2] = java.lang

Domain[3] = org.apache.activemq

Domain[4] = java.util.logging

all ObjectName:---------------

java.lang:type=OperatingSystem

java.lang:type=MemoryPool,name=Perm Gen

java.lang:type=Memory

JMImplementation:type=MBeanServerDelegate

org.apache.activemq:BrokerName=localhost,Type=Producer,destinationType=Queue,destinationName=kk.qq,clientId=ID_bsb3-1381-1372146822218-0_1,producerId=ID_bsb3-1381-1372146822218-1_1_1_1

org.apache.activemq:BrokerName=localhost,Type=Connection,ConnectorName=openwire,Connection=ID_bsb3-1381-1372146822218-0_1

org.apache.activemq:BrokerName=localhost,Type=Subscription,persistentMode=Non-Durable,destinationType=Queue,destinationName=kk.qq,clientId=ID_bsb3-1381-1372146822218-0_1,consumerId=ID_bsb3-1381-1372146822218-1_1_1_1

org.apache.activemq:BrokerName=localhost,Type=Connection,ConnectorName=openwire,ViewType=address,Name=tcp_//127.0.0.1_1347

java.lang:type=GarbageCollector,name=MarkSweepCompact

org.apache.activemq:BrokerName=localhost,Type=Broker

org.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Producer.Topic.kk.dp

org.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Topic

java.lang:type=MemoryManager,name=CodeCacheManager

org.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Connection

org.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Queue

org.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Consumer.Topic.kk.dp

org.apache.activemq:BrokerName=localhost,Type=Connection,ConnectorName=openwire,Connection=ID_bsb3-1346-1372146798953-0_1

java.lang:type=Compilation

org.apache.activemq:BrokerName=localhost,Type=Connection,ConnectorName=openwire,ViewType=address,Name=tcp_//127.0.0.1_1382

java.util.logging:type=Logging

java.lang:type=MemoryPool,name=Tenured Gen

org.apache.activemq:BrokerName=localhost,Type=Subscription,persistentMode=Non-Durable,destinationType=Topic,destinationName=ActiveMQ.Advisory.TempQueue_ActiveMQ.Advisory.TempTopic,clientId=ID_bsb3-1346-1372146798953-0_1,consumerId=ID_bsb3-1346-1372146798953-1_1_-1_1

java.lang:type=MemoryPool,name=Survivor Space

java.lang:type=Runtime

org.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Producer.Queue.kk.qq

java.lang:type=GarbageCollector,name=Copy

org.apache.activemq:BrokerName=localhost,Type=Queue,Destination=kk.qq

org.apache.activemq:BrokerName=localhost,Type=Subscription,persistentMode=Durable,subscriptionID=kk-dp-dc,destinationType=Topic,destinationName=kk.dp,clientId=kk-dp

java.lang:type=MemoryPool,name=Eden Space

org.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Consumer.Queue.kk.qq

java.lang:type=Threading

org.apache.activemq:BrokerName=localhost,Type=Topic,Destination=kk.dp

com.sun.management:type=HotSpotDiagnostic

java.lang:type=ClassLoading

org.apache.activemq:BrokerName=localhost,Type=Subscription,persistentMode=Non-Durable,destinationType=Topic,destinationName=ActiveMQ.Advisory.TempQueue_ActiveMQ.Advisory.TempTopic,clientId=ID_bsb3-1381-1372146822218-0_1,consumerId=ID_bsb3-1381-1372146822218-1_1_-1_1

java.lang:type=MemoryPool,name=Code Cache

org.apache.activemq:BrokerName=localhost,Type=Connector,ConnectorName=openwire

org.apache.activemq:BrokerName=localhost,Type=Broker:---------------

Class: org.apache.activemq.broker.jmx.BrokerView

==> Attriber:Uptime

==> Attriber:BrokerVersion

==> Attriber:Slave

==> Attriber:BrokerName

==> Attriber:Persistent

==> Attriber:TransportConnectors

==> Attriber:BrokerId

==> Attriber:Topics

==> Attriber:Queues

==> Attriber:TemporaryTopics

==> Attriber:TemporaryQueues

==> Attriber:TopicSubscribers

==> Attriber:DurableTopicSubscribers

==> Attriber:QueueSubscribers

==> Attriber:TemporaryTopicSubscribers

==> Attriber:TemporaryQueueSubscribers

==> Attriber:InactiveDurableTopicSubscribers

==> Attriber:TopicProducers

==> Attriber:QueueProducers

==> Attriber:TemporaryTopicProducers

==> Attriber:TemporaryQueueProducers

==> Attriber:DynamicDestinationProducers

==> Attriber:TotalEnqueueCount

==> Attriber:TotalDequeueCount

==> Attriber:TotalConsumerCount

==> Attriber:TotalProducerCount

==> Attriber:TotalMessageCount

==> Attriber:MemoryPercentUsage

==> Attriber:MemoryLimit

==> Attriber:StoreLimit

==> Attriber:StorePercentUsage

==> Attriber:TempLimit

==> Attriber:TempPercentUsage

==> Attriber:StatisticsEnabled

==> Attriber:OpenWireURL

==> Attriber:StompURL

==> Attriber:SslURL

==> Attriber:StompSslURL

==> Attriber:VMURL

==> Attriber:DataDirectory

==> Attriber:JMSJobScheduler

==> Operation:gc

==> Operation:stop

==> Operation:enableStatistics

==> Operation:addConnector

==> Operation:removeConnector

==> Operation:addNetworkConnector

==> Operation:removeNetworkConnector

==> Operation:stopGracefully

==> Operation:resetStatistics

==> Operation:disableStatistics

==> Operation:terminateJVM

==> Operation:addTopic

==> Operation:addQueue

==> Operation:removeTopic

==> Operation:removeQueue

==> Operation:createDurableSubscriber

==> Operation:destroyDurableSubscriber

==> Operation:reloadLog4jProperties

==> Operation:getTransportConnectorByType

==> Operation:start

上面只是拿到了broker的属性和操作,同理也可以拿到其它对象的属性和操作列表。

根据这些,我们就可以拿到broker,connector,producer,consumer,queue,topic,Subscription等等的Object对象,进一步的操作他们。

待续。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值