ActiveMQ笔记(5):JMX监控

时间:2022-05-13 10:46:26

系统上线运行后,及时监控报警是很必要的手段,对于ActiveMQ而言,主要监控的指标有:MQ本身的健康状况、每个队列的生产者数量、消费者数量、队列的当前消息数等。

ActiveMQ支持JMX监控,使用步骤如下:

一、修改conf/activemq.xml

<broker … useJmx="true”>
<managementContext>
<managementContext createConnector="true" connectorPort=“jmx端口号” connectorHost=“本机ip地址" />
</managementContext>
</broker>

二、设置jmx.access、jmx.password的文件权限

chmod 400 conf/jmx.*

(即:将jmx.password, jmx.access这二个文件设置成只读权限,activemq出于安全考虑,要求这二个文件只读)

三、修改bin\activemq 启动shell脚本

找到invoke_start(){ 这段,然后在前面插入:

ACTIVEMQ_CONF=“jmx.password所在位置的物理路目录"
ACTIVEMQ_SUNJMX_START="-Dcom.sun.management.jmxremote.port=端口号 "
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"

然后重启activemq即可。

然后在jconsole中,可以输入 ip地址:jmx端口号

ActiveMQ笔记(5):JMX监控 

其中username,password即jmx.password中定义的用户名和密码。

四、 spring中使用JMX

    <bean class="org.springframework.jmx.support.MBeanServerConnectionFactoryBean" id="mbeanServerConnection1">
<property name="serviceUrl" value="${mq_jmx_url1}"/>
<property name="connectOnStartup" value="false"/>
<property name="environment">
<props>
<prop key="java.naming.security.principal">
${mq_jmx_user1}
</prop>
<prop key="java.naming.security.credentials">
${mq_jmx_passwor1}
</prop>
</props>
</property>
</bean>

其中serviceUrl的值类似:service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi

参考示例:

private List<ActiveMQData> getMonitorDataList(MBeanServerConnection conn, String objectName) {
List<ActiveMQData> datas = new ArrayList<>();
try {
ObjectName objRootName = new ObjectName(objectName); String brokerName = (String) conn.getAttribute(objRootName, "BrokerName");
String brokerId = (String) conn.getAttribute(objRootName, "BrokerId");
String openWireUrl = (String) conn.getAttribute(objRootName, "OpenWireURL"); //健康状态
ObjectName healthObjName = new ObjectName(objectName + ",service=Health");
String healthStatus = (String) conn.getAttribute(healthObjName, "CurrentStatus"); //遍历队列
ObjectName[] objectNames = (ObjectName[]) conn.getAttribute(objRootName, "Queues");
Arrays.sort(objectNames); List<String> blackList = monitorConfig.getQueueBlackList();
for (ObjectName queueName : objectNames) {
...
Long queueSize = (Long) conn.getAttribute(queueName, "QueueSize");//队列消息数量
Long producerCount = (Long) conn.getAttribute(queueName, "ProducerCount");//生产者数量
Long consumerCount = (Long) conn.getAttribute(queueName, "ConsumerCount");//消费者数量
Long enqueueCount = (Long) conn.getAttribute(queueName, "EnqueueCount");//入队消息总数
Long dequeueCount = (Long) conn.getAttribute(queueName, "DequeueCount");//出队消息总数
...
}
} catch (Exception e) {
...
} return datas;
}

其中objectName值,可以在jconsole中查到