JMX操作ActiveMQ(1)

來源:互聯網
上載者:User

我們知道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中都提供了什麼東西。

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<ObjectInstance> set = mbsc.queryMBeans(null, null);for (Iterator<ObjectInstance> 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] = JMImplementationDomain[1] = com.sun.managementDomain[2] = java.langDomain[3] = org.apache.activemqDomain[4] = java.util.loggingall ObjectName:---------------java.lang:type=OperatingSystemjava.lang:type=MemoryPool,name=Perm Genjava.lang:type=MemoryJMImplementation:type=MBeanServerDelegateorg.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_1org.apache.activemq:BrokerName=localhost,Type=Connection,ConnectorName=openwire,Connection=ID_bsb3-1381-1372146822218-0_1org.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_1org.apache.activemq:BrokerName=localhost,Type=Connection,ConnectorName=openwire,ViewType=address,Name=tcp_//127.0.0.1_1347java.lang:type=GarbageCollector,name=MarkSweepCompactorg.apache.activemq:BrokerName=localhost,Type=Brokerorg.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Producer.Topic.kk.dporg.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Topicjava.lang:type=MemoryManager,name=CodeCacheManagerorg.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Connectionorg.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Queueorg.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Consumer.Topic.kk.dporg.apache.activemq:BrokerName=localhost,Type=Connection,ConnectorName=openwire,Connection=ID_bsb3-1346-1372146798953-0_1java.lang:type=Compilationorg.apache.activemq:BrokerName=localhost,Type=Connection,ConnectorName=openwire,ViewType=address,Name=tcp_//127.0.0.1_1382java.util.logging:type=Loggingjava.lang:type=MemoryPool,name=Tenured Genorg.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_1java.lang:type=MemoryPool,name=Survivor Spacejava.lang:type=Runtimeorg.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Producer.Queue.kk.qqjava.lang:type=GarbageCollector,name=Copyorg.apache.activemq:BrokerName=localhost,Type=Queue,Destination=kk.qqorg.apache.activemq:BrokerName=localhost,Type=Subscription,persistentMode=Durable,subscriptionID=kk-dp-dc,destinationType=Topic,destinationName=kk.dp,clientId=kk-dpjava.lang:type=MemoryPool,name=Eden Spaceorg.apache.activemq:BrokerName=localhost,Type=Topic,Destination=ActiveMQ.Advisory.Consumer.Queue.kk.qqjava.lang:type=Threadingorg.apache.activemq:BrokerName=localhost,Type=Topic,Destination=kk.dpcom.sun.management:type=HotSpotDiagnosticjava.lang:type=ClassLoadingorg.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_1java.lang:type=MemoryPool,name=Code Cacheorg.apache.activemq:BrokerName=localhost,Type=Connector,ConnectorName=openwireorg.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對象,進一步的操作他們。

待續。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.