activemq.xml
是否使用jmx
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="activemq-jf" dataDirectory="${activemq.data}" useJmx="true">
配置连接端口,jmxDomainName
<managementContext>
<managementContext createConnector="true" connectorPort="11091" connectorPath="/jmxrmi" jmxDomainName="org.apache.activemq"/>
</managementContext>
启动参数添加:
ACTIVEMQ_SUNJMX_START="-Dcom.sun.management.jmxremote.port=11091 "
ACTIVEMQ_SUNJMX_START="$ACTIVEMQ_SUNJMX_START -Dcom.sun.management.jmxremote.password.file=${ACTIVEMQ_CONF}/jmx.password"
ACTIVEMQ_SUNJMX_START="$ACTIVEMQ_SUNJMX_START -Dcom.sun.management.jmxremote.access.file=${ACTIVEMQ_CONF}/jmx.access"
ACTIVEMQ_SUNJMX_START="$ACTIVEMQ_SUNJMX_START -Dcom.sun.management.jmxremote.ssl=false"
Java代码
String urlStr =" service:jmx:rmi:///jndi/rmi://10.1.0.206:11091/jmxrmi";
JMXServiceURL urls = new JMXServiceURL(urlStr);
JMXConnector connector = JMXConnectorFactory.connect(urls, null);
connector.connect();
MBeanServerConnection conn = connector.getMBeanServerConnection();
//这里brokerName的b要小些,大写会报错
ObjectName name = new ObjectName("org.apache.activemq:brokerName=activemq-jf,type=Broker");
BrokerViewMBean mBean = (BrokerViewMBean) MBeanServerInvocationHandler.newProxyInstance
(conn, name, BrokerViewMBean.class, true);
for (ObjectName na : mBean.getQueues()) {
QueueViewMBean queueBean = (QueueViewMBean) MBeanServerInvocationHandler.newProxyInstance
(conn, na, QueueViewMBean.class, true);
if (queueBean.getName().equals(queueName)) {
if(logger.isDebugEnabled()){
logger.debug(GlobalFunc.strCat(queueName,"的积压量:",queueBean.getQueueSize()));
}
return queueBean.getQueueSize();
}
}