jmx和rmi基本不熟
网上相关帖子不是很多,但是都好坑,研究了一天终于是调通了,有些还是没去深究的以后有时间再弄
1.修改conf/activemq.xml配置
a. 加useJmx=“true”
<broker xmlns="http://activemq.apache.org/schema/core" useJmx="true" brokerName="localhost" dataDirectory="${activemq.data}">
b.从false修改到如下
<managementContext>
<managementContext createConnector="true" connectorPort="1099" connectorPath="/jmxrmi" jmxDomainName="org.apache.activemq"/>
</managementContext>
网上很多1093和11099端口说法,行不通,很蛋疼,官方文档说的是1099.配置完之前最好telnet一下看看1099是否通的,否则在docker启动的时候-p开放端口,其他方式解压安装的注意iptable打开端口,还要注意一下防火墙之类的.
注意,我的方式,仅仅需要修改activemq的此两处配置即可,其他的不需要做任何修改
activemq中文手册
2.代码
public ActiveMqEntity getQueueMap() {
Map<String, String[]> env = new HashMap<>();
String[] credentials = {"admin", "admin"};
env.put(JMXConnector.CREDENTIALS, credentials);
ActiveMqEntity activeMqEntity = new ActiveMqEntity();
JMXConnector conn = null;
BrokerViewMBean bean = null;
MBeanServerConnection connection = null;
try {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" +
"localhost" + ":" + "1099" + connectorPath);
conn = JMXConnectorFactory.connect(url, env);
conn.connect();
connection = conn.getMBeanServerConnection();
ObjectName name = new ObjectName(jmxDomain +
":brokerName=localhost,type=Broker");
bean = MBeanServerInvocationHandler.newProxyInstance(connection, name,
BrokerViewMBean.class, true);
if (bean != null) {
for (ObjectName queueName : bean.getQueues()) {
QueueViewMBean queueBean = MBeanServerInvocationHandler.
newProxyInstance(connection, queueName, QueueViewMBean.class, true);
Map<String, Object> map = new HashMap<>();
map.put("queueName", queueBean.getName());//消息队列名称
map.put("queueSize", queueBean.getQueueSize());//队列剩余消息数
map.put("producerCount", queueBean.getProducerCount());//生产者数
map.put("consumerCount", queueBean.getConsumerCount());//消费者数
map.put("dequeueCount", queueBean.getDequeueCount());//出队数
map.put("enqueueCount", queueBean.getEnqueueCount());//入队数
map.put("expiredCount", queueBean.getExpiredCount());//过期数
activeMqEntity.getQueue().add(map);
}
for (ObjectName queueName : bean.getTopics()) {
QueueViewMBean queueBean = MBeanServerInvocationHandler.
newProxyInstance(connection, queueName, QueueViewMBean.class, true);
Map<String, Object> map = new HashMap<>();
map.put("topicName", queueBean.getName());//消息队列名称
map.put("topicSize", queueBean.getQueueSize());//队列剩余消息数
map.put("producerCount", queueBean.getProducerCount());//生产者数
map.put("consumerCount", queueBean.getConsumerCount());//消费者数
map.put("dequeueCount", queueBean.getDequeueCount());//出队数
map.put("enqueueCount", queueBean.getEnqueueCount());//入队数
activeMqEntity.getTopic().add(map);
}
}
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
try {
if (conn != null) {
//关闭连接
conn.close();
}
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
return activeMqEntity;
}
注意:
我的是localhost:1099,我把监控的程序jar是部署到与activemq同一台linux服务器上,非远程(跨服务器),也没有直接使用当前ip,因为会报错拒绝连接,尽管我的etc/hosts已经把127.0.0.1设置成了本地ip,依然会报错,不去深究了后续有时间处理.
查询返回结果:
网上一些帖子,可以参考下
https://stackoverflow.com/questions/19093003/apache-activemq-browser-cant-connect-to-jmx-console
https://stackoverflow.com/questions/7431734/activemq-get-list-of-connections-through-jmx
除了我这些配置修改,还有些说要在activemq的bin目录下去修改启动脚本,在invock_start{的地方新增
conf/下去 jmx.password and jmx.access新增只读账号,我都试了没有成功,主要是想远程调试监控broker,这个以后再继续研究
#ACTIVEMQ_SUNJMX_START="-Dcom.sun.management.jmxremote.port=11099 "
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_SUNJMX_START="$ACTIVEMQ_SUNJMX_START -Dcom.sun.management.jmxremote"