JMX Get ACTIVEMQ Consumer Details

Source: Internet
Author: User
Tags message queue stomp xmlns

Original link: http://www.cnblogs.com/king1302217/p/6278987.html


Modify the MQ configuration file on the server

<!--Licensed to the Apache software Foundation (ASF) under one or more
Contributor license agreements. See the NOTICE file distributed with
This is for additional information regarding copyright ownership.
The ASF licenses this file to you under the Apache License, Version 2.0
(the "License"); Except in compliance with
The License. Obtain a copy of the License at


http://www.apache.org/licenses/LICENSE-2.0


Unless required by applicable law or agreed to writing, software
Distributed under the License is distributed on a "as is" BASIS,
Without warranties or CONDITIONS of any KIND, either express or implied.
See the License for the specific language governing permissions and
Limitations under the License.
-
<!--START Snippet:example--
<beans
Xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd
Http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd ">


<!--allows us to use system properties as variables on this configuration file--
<bean class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name= "Locations" >
<value>file:${activemq.conf}/credentials.properties</value>
</property>
</bean>


<!--allows accessing the server log--
<bean id= "LogQuery" class= "Io.fabric8.insight.log.log4j.Log4jLogQuery"
Lazy-init= "false" scope= "Singleton"
Init-method= "Start" destroy-method= "Stop" >
</bean>


<!--
The <broker> element is used to configure the ActiveMQ broker.
Configuring the use of JMX
-
<broker xmlns= "Http://activemq.apache.org/schema/core" brokername= "broker" datadirectory= "${activemq.data}" Usejmx= "true" >


<destinationPolicy>
<policyMap>
<policyEntries>
<policyentry topic= ">" >
<!--the constantpendingmessagelimitstrategy is used to prevent
Slow topic consumers to block producers and affect other consumers
By limiting the number of messages that is retained
For more information, see:


Http://activemq.apache.org/slow-consumer-handling.html


-
<pendingMessageLimitStrategy>
<constantpendingmessagelimitstrategy limit= "/>"
</pendingMessageLimitStrategy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>




<!--
The managementcontext is used to configure how ActiveMQ are exposed in
Jmx. By default, ActiveMQ uses the MBean server, which is started by
The JVM. For more information, see:


Http://activemq.apache.org/jmx.html
Configure Port connectorport=1099
-
<managementContext>
<managementcontext connectorport= "1099" createconnector= "true"/>
</managementContext>


<!--
Configure message persistence for the broker. The default persistence
Mechanism is the KAHADB store (identified by the kahadb tag).
For more information, see:


Http://activemq.apache.org/persistence.html
-
<persistenceAdapter>
&LT;KAHADB directory= "${activemq.data}/kahadb"/>
</persistenceAdapter>




<!--
The Systemusage controls the maximum amount of space the broker would
Use before disabling caching and/or slowing down producers. For more information, see:
Http://activemq.apache.org/producer-flow-control.html
-
<systemUsage>
<systemUsage>
<memoryUsage>
<memoryusage percentofjvmheap= "/>"
</memoryUsage>
<storeUsage>
<storeusage limit= "GB"/>
</storeUsage>
<tempUsage>
<tempusage limit= "GB"/>
</tempUsage>
</systemUsage>
</systemUsage>


<!--
The transport connectors expose ActiveMQ over a given protocol to
Clients and other brokers. For more information, see:


Http://activemq.apache.org/configuring-transports.html
-
<transportConnectors>
<!--DOS protection, limit concurrent connections to $ and frame size to 100MB--
<transportconnector name= "Openwire" uri= "tcp://0.0.0.0:61616?maximumconnections=1000&amp; wireformat.maxframesize=104857600 "/>
<transportconnector name= "AMQP" uri= "amqp://0.0.0.0:5672?maximumconnections=1000&amp; wireformat.maxframesize=104857600 "/>
<transportconnector name= "Stomp" uri= "stomp://0.0.0.0:61613?maximumconnections=1000&amp; wireformat.maxframesize=104857600 "/>
<transportconnector name= "Mqtt" uri= "mqtt://0.0.0.0:1883?maximumconnections=1000&amp; wireformat.maxframesize=104857600 "/>
<transportconnector name= "ws" Uri= "Ws://0.0.0.0:61614?maximumconnections=1000&amp;wireformat.maxframesize =104857600 "/>
</transportConnectors>


<!--destroy the spring context on shutdown to stop jetty--
<shutdownHooks>
<bean xmlns= "Http://www.springframework.org/schema/beans" class= "Org.apache.activemq.hooks.SpringContextHook" />
</shutdownHooks>


</broker>


<!--
Enable Web consoles, REST and Ajax APIs and demos
The Web consoles requires by default login, you can disable this in the Jetty.xml file


Take a look at ${activemq_home}/conf/jetty.xml for more details
-
<import resource= "Jetty.xml"/>


</beans>

<!--END Snippet:example--



Test class

Package com. Policeposdatacenter;


Import javax.management.MBeanServerConnection;
Import Javax.management.MBeanServerInvocationHandler;
Import Javax.management.ObjectName;
Import Javax.management.remote.JMXConnector;
Import Javax.management.remote.JMXConnectorFactory;
Import Javax.management.remote.JMXServiceURL;


Import Org.apache.activemq.broker.jmx.BrokerViewMBean;
Import Org.apache.activemq.broker.jmx.QueueViewMBean;


public class Testmqclient {
JMX Service address, note port
Static String jmxserviceurl= "Service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi";
Brokername and type first letter is smaller, brokername=broker to be consistent with the above configuration file, Italic underline section
Static String objectname= "Org.apache.activemq:brokername=broker,type=broker";
/**
* Get Status
* @throws Exception
*/
public static void Main (string[] args) throws Exception {
Jmxserviceurl url = new Jmxserviceurl (Jmxserviceurl);
Jmxconnector connector = jmxconnectorfactory.connect (URL, null);
Connector.connect ();
Mbeanserverconnection connection = Connector.getmbeanserverconnection ();


ObjectName name = new ObjectName (ObjectName);
Brokerviewmbean MBean = (brokerviewmbean) mbeanserverinvocationhandler.newproxyinstance (connection,
Name, Brokerviewmbean.class, true);
System.out.println (Mbean.getbrokername ());

For (ObjectName queueName:mBean.getQueues ()) {
Queueviewmbean Queuembean = (Queueviewmbean) Mbeanserverinvocationhandler.
Newproxyinstance (Connection, QueueName, Queueviewmbean.class, true);
System.out.println ("\ n------------------------------\ n");


Message Queue name
System.out.println ("states for queue---" + queuembean.getname ());


Number of messages remaining in the queue
System.out.println ("Size---" + queuembean.getqueuesize ());


Number of consumers
System.out.println ("Number of consumers---" + queuembean.getconsumercount ());


Number of teams out
System.out.println ("Number of dequeue---" + queuembean.getdequeuecount ());
}

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.