springboot使用activemq的jmx配置

springboot版本:2.3.4.RELEASE
activemq版本:5.14.5
linux版本:redhat6
java端需要配置
在activemq的配置文件中新增以下代码


@Bean
	public MBeanServerConnection getBrokerViewMBean() throws Exception {
		JMXServiceURL urls = new JMXServiceURL(”service:jmx:rmi://192.168.126.128:1616/jndi/rmi://192.168.126.128:1617/jmxrmi“);
		JMXConnector connector = JMXConnectorFactory.connect(urls, null);
		connector.connect();
		MBeanServerConnection conn = connector.getMBeanServerConnection();
		return conn;
	}

在activemq消费者中添加以下代码

@Autowired
	private MBeanServerConnection conn;
private String borker=“org.apache.activemq:brokerName=localhost,type=Broker”
@JmsListener(destination = "${activemq.mail-queue}", containerFactory = "jmsListenerContainerQueue")
	public void Queue(Map map) throws Exception {
		getMQQueueNum();
	}
@Override
	public Boolean getMQQueueNum() throws Exception {
		ObjectName name = new ObjectName(borker);
		BrokerViewMBean mBean = (BrokerViewMBean) MBeanServerInvocationHandler.newProxyInstance(conn, name,
				BrokerViewMBean.class, true);
		for (ObjectName na : mBean.getQueues()) {// 获取点对点的队列 mBean.getTopics() 获取订阅模式的队列
			QueueViewMBean queueBean = (QueueViewMBean) MBeanServerInvocationHandler.newProxyInstance(conn, na,
					QueueViewMBean.class, true);
			if ("mail-queue".equals(queueBean.getName())) {
				System.out.println("******************************");
				System.out.println("队列的名称:" + queueBean.getName());
				System.out.println("队列中总消息数:" + queueBean.getEnqueueCount());
				System.out.println("队列中剩余的消息数:" + queueBean.getQueueSize());
				System.out.println("消费者数:" + queueBean.getConsumerCount());
				System.out.println("出队列的数量:" + queueBean.getDequeueCount());

			}
		}
		Thread.sleep(2000);
		return true;
	}

在activemq中需要添加的配置(此链接是官网配置:http://activemq.apache.org/jmx.html 以供大家参考)
在activemq.xml中添加以下配置:

  1. 在broker 中添加 useJmx=“true”
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}" useJmx="true" >

2.按照如下配置修改managementContext配置

 <managementContext>
            <!--<managementContext createConnector="false"/> -->
            <managementContext createConnector="true" connectorPort="1617" connectorHost="192.168.126.128" rmiServerPort="1616" />
        </managementContext>

3.在activemq启动脚本 activemq 开始位置添加以下配置

ACTIVEMQ_HOME=/home/activemq/apache-activemq-5.14.5
ACTIVEMQ_BASE=${ACTIVEMQ_HOME}
ACTIVEMQ_SUNJMX_START="-Dcom.sun.management.jmxremote.port=1616 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.password.fil
e=${ACTIVEMQ_BASE}/conf/jmx.password -Dcom.sun.management.jmxremote.access.file=${ACTIVEMQ_BASE}/conf/jmx.access"

4.重启activemq

运行效果:
springboot使用activemq的jmx配置_第1张图片

你可能感兴趣的:(java,spring,boot,java,activemq)