ActiveMQ JMX使用
一、说明
ActiveMQ使用过程中,可以使用自带的控制台进行相关的操作以及查看,但是当队列数相当多的时候,在查询以及整体的监控上,就可能相当的不便。所以可通过JMX的方式,进行MQ中队列相关指标的以及整体健康性能等收集展示。
二、JMX功能开启
2.1、broker标签中添加 userJmx="true":
2.2、配置 managementContext
其中 用户名、密码及权限配置在 jmx.password 和 jmx.access 文件中。
三、Java应用连接使用
3.1、获取在线的队列消费者IP
private static final String queueObjectName = "org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName=%s"; private static final String brokerAddress = "service:jmx:rmi:///jndi/rmi://%s/jmxrmi"; private static List<String> getQueueConsumers(String user, String password, String ip, String queueName) throws Exception{ List<String> result = Lists.newArrayList(); String url = String.format(brokerAddress, ip); Map<String, Object> credentials = new HashMap<>(); credentials.put(JMXConnector.CREDENTIALS, new String[] {user, password}); JMXServiceURL urls = new JMXServiceURL(url); JMXConnector connector = JMXConnectorFactory.connect(urls,credentials); connector.connect(); MBeanServerConnection conn = connector.getMBeanServerConnection(); ObjectName name = new ObjectName(String.format(queueObjectName, queueName)); QueueViewMBean queueViewMBean = MBeanServerInvocationHandler.newProxyInstance(conn, name, QueueViewMBean.class, true); for (ObjectName mbeanNameConsumerObjectName : queueViewMBean.getSubscriptions()) { SubscriptionViewMBean subscriptionViewMBean = MBeanServerInvocationHandler.newProxyInstance(conn, mbeanNameConsumerObjectName,SubscriptionViewMBean.class, true); ObjectName connectionObjectName = new ObjectName(subscriptionViewMBean.getConnection().getCanonicalName()); ConnectionViewMBean connectionViewMBean = MBeanServerInvocationHandler.newProxyInstance(conn, connectionObjectName,ConnectionViewMBean.class, true); if (connectionViewMBean != null) { result.add(connectionViewMBean.getRemoteAddress().replace("//", "").split("\\:")[1]); // System.out.println(connectionViewMBean.getRemoteAddress()+"|"+connectionViewMBean.getClientId()); } } return result; }
其中方法的参数: ip - broker的 ip 地址,user - JMX登录访问的用户名,password - JMX登录访问的密码,queueName - 待查询的队列名
3.2、获取队列的出队、入队以及积压数等指标信息
package workMQ; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import javax.management.MBeanServerConnection; import javax.management.MBeanServerInvocationHandler; import javax.management.ObjectInstance; import javax.management.ObjectName; import javax.management.openmbean.CompositeData; 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.ConnectionViewMBean; import org.apache.activemq.broker.jmx.ConnectorViewMBean; import org.apache.activemq.broker.jmx.QueueViewMBean; import com.google.common.collect.Maps; public class Test08 { public static void main(String[] args) throws Exception { String ip = "192.168.25.43:9607"; String url = String.format("service:jmx:rmi:///jndi/rmi://%s/jmxrmi",ip); Map<String, Object> credentials = new HashMap<>(); credentials.put(JMXConnector.CREDENTIALS, new String[] {"admin", "activemq"}); JMXServiceURL urls = new JMXServiceURL(url); JMXConnector connector = JMXConnectorFactory.connect(urls,credentials); connector.connect(); MBeanServerConnection conn = connector.getMBeanServerConnection(); ObjectName name = new ObjectName("org.apache.activemq:brokerName=localhost,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); System.out.println("队列的名称:"+queueBean.getName()); System.out.println("消息积压数:"+queueBean.getQueueSize()); System.out.println("入队:"+queueBean.getEnqueueCount()); System.out.println("出队:"+queueBean.getDequeueCount()); System.out.println("消费者数:"+queueBean.getConsumerCount()); System.out.println("生产者数:"+queueBean.getProducerCount()); //还有很多队列指标信息,具体可根据实际使用获取 } } }
四、JDK自带JConsole连接
1、JDK安装目录下的 bin 文件夹,点击JConsole.exe
2、选择远程进程,输入 brokerAddress、username、password
brokerAddress 格式 : service:jmx:rmi:///jndi/rmi://具体的brokerip地址:配置的JMX的port访问端口/jmxrmi
username、password 即是JMX的访问用户名、密码
3、进入后,可查看相关信息