JMX操作ActiveMQ(1)

来源:互联网 发布:淘宝网舞蹈上衣长袖 编辑:程序博客网 时间:2024/05/22 13:33

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


输出结果:

[plain] view plain copy
  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://blog.csdn.net/kimmking/article/details/9170563

0 0
原创粉丝点击