首页 > Java开发 > JMX操作ActiveMQ(1)

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)

(参见http://activemq.apache.org/jmx.html)

 

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

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

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

 

[java][/java] view plaincopy

  1. package kk;
  2. import java.util.Iterator;
  3. import java.util.Set;
  4. import javax.management.MBeanAttributeInfo;
  5. import javax.management.MBeanInfo;
  6. import javax.management.MBeanOperationInfo;
  7. import javax.management.MBeanServerConnection;
  8. import javax.management.ObjectInstance;
  9. import javax.management.ObjectName;
  10. import javax.management.remote.JMXConnector;
  11. import javax.management.remote.JMXConnectorFactory;
  12. import javax.management.remote.JMXServiceURL;
  13. public class TestJMX {
  14.     public static void main(String[] args) throws Exception {
  15.         String surl = "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi";
  16.         JMXServiceURL url = new JMXServiceURL(surl);
  17.         JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
  18.         MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
  19.         System.out.println("Domains:---------------");
  20.         String domains[] = mbsc.getDomains();
  21.         for (int i = 0; i < domains.length; i++) {
  22.             System.out.println("\tDomain[" + i + "] = " + domains[i]);
  23.         }
  24.         System.out.println("all ObjectName:---------------");
  25.         Set<ObjectInstance> set = mbsc.queryMBeans(null, null);
  26.         for (Iterator<ObjectInstance> it = set.iterator(); it.hasNext();) {
  27.             ObjectInstance oi = (ObjectInstance) it.next();
  28.             System.out.println("\t" + oi.getObjectName());
  29.         }
  30.         System.out.println("org.apache.activemq:BrokerName=localhost,Type=Broker:---------------");
  31.         ObjectName mbeanName = new ObjectName("org.apache.activemq:BrokerName=localhost,Type=Broker");
  32.         MBeanInfo info = mbsc.getMBeanInfo(mbeanName);
  33.         System.out.println("Class: " + info.getClassName());
  34.         if (info.getAttributes().length > 0){
  35.             for(MBeanAttributeInfo m : info.getAttributes())
  36.                 System.out.println("\t ==> Attriber:" + m.getName());
  37.         }
  38.         if (info.getOperations().length > 0){
  39.             for(MBeanOperationInfo m : info.getOperations())
  40.                 System.out.println("\t ==> Operation:" + m.getName());
  41.         }
  42.         jmxc.close();
  43.     }
  44. }

 

 

输出结果:

 

[plain][/plain] view plaincopy

  1. Domains:---------------
  2.     Domain[0] = JMImplementation
  3.     Domain[1] = com.sun.management
  4.     Domain[2] = java.lang
  5.     Domain[3] = org.apache.activemq
  6.     Domain[4] = java.util.logging
  7. all ObjectName:---------------
  8.     java.lang:type=OperatingSystem
  9.     java.lang:type=MemoryPool,name=Perm Gen
  10.     java.lang:type=Memory
  11.     JMImplementation:type=MBeanServerDelegate
  12.     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
  13.     org.apache.activemq:BrokerName=localhost,Type=Connection,ConnectorName=openwire,Connection=ID_bsb3-1381-1372146822218-0_1
  14.     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
  15.     org.apache.activemq:BrokerName=localhost,Type=Connection,ConnectorName=openwire,ViewType=address,Name=tcp_//127.0.0.1_1347
  16.     java.lang:type=GarbageCollector,name=MarkSweepCompact
  17.     org.apache.activemq:BrokerName=localhost,Type=Broker
  18.     org.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Producer.Topic.kk.dp
  19.     org.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Topic
  20.     java.lang:type=MemoryManager,name=CodeCacheManager
  21.     org.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Connection
  22.     org.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Queue
  23.     org.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Consumer.Topic.kk.dp
  24.     org.apache.activemq:BrokerName=localhost,Type=Connection,ConnectorName=openwire,Connection=ID_bsb3-1346-1372146798953-0_1
  25.     java.lang:type=Compilation
  26.     org.apache.activemq:BrokerName=localhost,Type=Connection,ConnectorName=openwire,ViewType=address,Name=tcp_//127.0.0.1_1382
  27.     java.util.logging:type=Logging
  28.     java.lang:type=MemoryPool,name=Tenured Gen
  29.     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
  30.     java.lang:type=MemoryPool,name=Survivor Space
  31.     java.lang:type=Runtime
  32.     org.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Producer.Queue.kk.qq
  33.     java.lang:type=GarbageCollector,name=Copy
  34.     org.apache.activemq:BrokerName=localhost,Type=Queue,Destination=kk.qq
  35.     org.apache.activemq:BrokerName=localhost,Type=Subscription,persistentMode=Durable,subscriptionID=kk-dp-dc,destinationType=Topic,destinationName=kk.dp,clientId=kk-dp
  36.     java.lang:type=MemoryPool,name=Eden Space
  37.     org.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Consumer.Queue.kk.qq
  38.     java.lang:type=Threading
  39.     org.apache.activemq:BrokerName=localhost,Type=Topic,Destination=kk.dp
  40.     com.sun.management:type=HotSpotDiagnostic
  41.     java.lang:type=ClassLoading
  42.     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
  43.     java.lang:type=MemoryPool,name=Code Cache
  44.     org.apache.activemq:BrokerName=localhost,Type=Connector,ConnectorName=openwire
  45. org.apache.activemq:BrokerName=localhost,Type=Broker:---------------
  46. Class: org.apache.activemq.broker.jmx.BrokerView
  47.      ==> Attriber:Uptime
  48.      ==> Attriber:BrokerVersion
  49.      ==> Attriber:Slave
  50.      ==> Attriber:BrokerName
  51.      ==> Attriber:Persistent
  52.      ==> Attriber:TransportConnectors
  53.      ==> Attriber:BrokerId
  54.      ==> Attriber:Topics
  55.      ==> Attriber:Queues
  56.      ==> Attriber:TemporaryTopics
  57.      ==> Attriber:TemporaryQueues
  58.      ==> Attriber:TopicSubscribers
  59.      ==> Attriber:DurableTopicSubscribers
  60.      ==> Attriber:QueueSubscribers
  61.      ==> Attriber:TemporaryTopicSubscribers
  62.      ==> Attriber:TemporaryQueueSubscribers
  63.      ==> Attriber:InactiveDurableTopicSubscribers
  64.      ==> Attriber:TopicProducers
  65.      ==> Attriber:QueueProducers
  66.      ==> Attriber:TemporaryTopicProducers
  67.      ==> Attriber:TemporaryQueueProducers
  68.      ==> Attriber:DynamicDestinationProducers
  69.      ==> Attriber:TotalEnqueueCount
  70.      ==> Attriber:TotalDequeueCount
  71.      ==> Attriber:TotalConsumerCount
  72.      ==> Attriber:TotalProducerCount
  73.      ==> Attriber:TotalMessageCount
  74.      ==> Attriber:MemoryPercentUsage
  75.      ==> Attriber:MemoryLimit
  76.      ==> Attriber:StoreLimit
  77.      ==> Attriber:StorePercentUsage
  78.      ==> Attriber:TempLimit
  79.      ==> Attriber:TempPercentUsage
  80.      ==> Attriber:StatisticsEnabled
  81.      ==> Attriber:OpenWireURL
  82.      ==> Attriber:StompURL
  83.      ==> Attriber:SslURL
  84.      ==> Attriber:StompSslURL
  85.      ==> Attriber:VMURL
  86.      ==> Attriber:DataDirectory
  87.      ==> Attriber:JMSJobScheduler
  88.      ==> Operation:gc
  89.      ==> Operation:stop
  90.      ==> Operation:enableStatistics
  91.      ==> Operation:addConnector
  92.      ==> Operation:removeConnector
  93.      ==> Operation:addNetworkConnector
  94.      ==> Operation:removeNetworkConnector
  95.      ==> Operation:stopGracefully
  96.      ==> Operation:resetStatistics
  97.      ==> Operation:disableStatistics
  98.      ==> Operation:terminateJVM
  99.      ==> Operation:addTopic
  100.      ==> Operation:addQueue
  101.      ==> Operation:removeTopic
  102.      ==> Operation:removeQueue
  103.      ==> Operation:createDurableSubscriber
  104.      ==> Operation:destroyDurableSubscriber
  105.      ==> Operation:reloadLog4jProperties
  106.      ==> Operation:getTransportConnectorByType
  107.      ==> Operation:start

上面只是拿到了broker的属性和操作,同理也可以拿到其它对象的属性和操作列表。
根据这些,我们就可以拿到broker,connector,producer,consumer,queue,topic,Subscription等等的Object对象,进一步的操作他们。

 

请接下一篇: http://www.devba.com/index.php/archives/4095.html


本文固定链接: http://www.devba.com/index.php/archives/4094.html | 开发吧

报歉!评论已关闭.