You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java Spring应用中获取ActiveMQ队列的入队、出队计数

Spring 项目获取 ActiveMQ 队列统计指标实现方案

前置要求

先确认 ActiveMQ Broker 已开启 JMX 功能:

  • 打开 ActiveMQ 安装目录下 conf/activemq.xml 配置文件,检查 broker 节点配置 useJmx="true"(默认已开启)
  • 记录 JMX 服务端口(默认是 1099,可通过配置文件修改)

1. 引入依赖(以 Maven 为例,Spring Boot 场景)

<!-- ActiveMQ 启动器 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!-- Spring JMX 支持(可选,简化JMX操作) -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
</dependency>

2. 配置 JMX 连接 Bean

创建配置类,注册 JmxTemplate 用于连接 ActiveMQ 的 JMX 服务:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jmx.support.MBeanServerConnectionFactoryBean;
import org.springframework.jmx.core.JmxTemplate;
import javax.management.remote.JMXServiceURL;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class ActiveMqJmxConfig {

    @Bean
    public MBeanServerConnectionFactoryBean mBeanServerConnectionFactory() {
        MBeanServerConnectionFactoryBean factory = new MBeanServerConnectionFactoryBean();
        try {
            // 替换为你的ActiveMQ实际IP、JMX端口
            factory.setServiceUrl(new JMXServiceURL("service:jmx:rmi:///jndi/rmi://192.168.1.100:1099/jmxrmi"));
            // 如果JMX开启了认证,添加下面的用户名密码配置
            Map<String, Object> environment = new HashMap<>();
            environment.put("jmx.remote.credentials", new String[]{"admin", "admin"});
            factory.setEnvironment(environment);
        } catch (Exception e) {
            throw new RuntimeException("JMX连接配置错误", e);
        }
        return factory;
    }

    @Bean
    public JmxTemplate jmxTemplate(MBeanServerConnectionFactoryBean factory) throws Exception {
        return new JmxTemplate(factory.getObject());
    }
}

3. 编写指标查询服务类

封装队列指标查询逻辑,核心是通过 MBean ObjectName 定位到指定队列的统计属性:

import org.springframework.jmx.core.JmxTemplate;
import org.springframework.stereotype.Service;
import javax.management.ObjectName;

@Service
public class ActiveMqMetricService {

    private final JmxTemplate jmxTemplate;
    // 替换为你的ActiveMQ实际broker名称,默认是localhost
    private static final String BROKER_NAME = "localhost";
    private static final String QUEUE_MBEAN_PATTERN = "org.apache.activemq:type=Broker,brokerName=%s,destinationType=Queue,destinationName=%s";

    public ActiveMqMetricService(JmxTemplate jmxTemplate) {
        this.jmxTemplate = jmxTemplate;
    }

    // 队列统计数据DTO
    public record QueueMetricDTO(Long enqueueCount, Long dequeueCount, Long queueSize, Integer consumerCount) {}

    public QueueMetricDTO getQueueMetric(String queueName) {
        try {
            ObjectName queueMbeanName = new ObjectName(String.format(QUEUE_MBEAN_PATTERN, BROKER_NAME, queueName));
            Long enqueueCount = (Long) jmxTemplate.getAttribute(queueMbeanName, "EnqueueCount");
            Long dequeueCount = (Long) jmxTemplate.getAttribute(queueMbeanName, "DequeueCount");
            Long queueSize = (Long) jmxTemplate.getAttribute(queueMbeanName, "QueueSize");
            Integer consumerCount = (Integer) jmxTemplate.getAttribute(queueMbeanName, "ConsumerCount");
            return new QueueMetricDTO(enqueueCount, dequeueCount, queueSize, consumerCount);
        } catch (Exception e) {
            throw new RuntimeException("查询ActiveMQ队列指标失败:" + e.getMessage(), e);
        }
    }
}

4. 开发REST接口

创建 Controller 对外暴露查询端点:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/activemq/metric")
public class ActiveMqMetricController {

    private final ActiveMqMetricService metricService;

    public ActiveMqMetricController(ActiveMqMetricService metricService) {
        this.metricService = metricService;
    }

    @GetMapping("/queue/{queueName}")
    public QueueMetricDTO getQueueMetric(@PathVariable String queueName) {
        return metricService.getQueueMetric(queueName);
    }
}

注意事项

  • 生产环境建议增加本地缓存,比如每5-10秒更新一次指标,避免频繁请求JMX对ActiveMQ Broker造成压力
  • 若ActiveMQ部署在容器内,需确保JMX端口对外暴露且网络可达
  • broker名称可通过ActiveMQ的JMX GUI工具查看,和配置保持一致即可

内容的提问来源于stack exchange,提问作者Crislips

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 00:18:05