ActiveMQ统计插件无法使用通配符目的地。
创始人
2024-05-21 01:45:40
0

可以使用自定义插件来解决这个问题。在自定义插件中,可以直接从BrokerService中获取目的地列表,并计算每个目的地的统计数据。以下是一个示例代码:

public class CustomStatisticsPlugin extends BrokerPlugin {
  
  private final Map destinationStats = new HashMap<>();

  @Override
  public Broker installPlugin(Broker broker) throws Exception {
    final ManagementContext managementContext = broker.getManagementContext();

    destinationStats.clear(); // make sure destinationStats is empty

    // register MBean listener to listen to destination creation events
    final DestinationInterceptor destinationInterceptor = new DestinationInterceptor() {
      @Override
      public Destination intercept(Destination destination) {
        // only intercept wildcard destinations
        if (destination instanceof DestinationFilter && ((DestinationFilter) destination).getDestination() == null) {
          final String destinationName = destination.getName();
          // create DestinationStatistics for new destination
          final DestinationStatistics stats = new DestinationStatistics();
          destinationStats.put(destinationName, stats);

          // register DestinationStatistics as MBean
          final ObjectName destinationObjectName = managementContext.createObjectName(destinationName);
          managementContext.registerMBean(stats, destinationObjectName);
        }
        return destination;
      }
    };

    // combine existing destination interceptor with custom destination interceptor
    final DestinationInterceptor[] interceptors = broker.getDestinationInterceptors();
    final DestinationInterceptor[] newInterceptors = new DestinationInterceptor[interceptors.length + 1];
    newInterceptors[0] = destinationInterceptor;
    System.arraycopy(interceptors, 0, newInterceptors, 1, interceptors.length);

    // install custom destination interceptor
    final BrokerService brokerService = broker.getBrokerService();
    brokerService.setDestinationInterceptors(newInterceptors);

    // create and register broker statistics MBean
    final BrokerStatistics brokerStats = new BrokerStatistics();
    final ObjectName brokerObjectName = managementContext.createObjectName("org.apache.activemq:type=Broker,brokerName=" + brokerService.getBrokerName());
    managementContext.registerMBean(brokerStats, brokerObjectName);

    // create and start ScheduledExecutorService to update statistics periodically
    final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    final long updateInterval = 1000; // update statistics every second
    executorService.scheduleAtFixedRate(new Runnable() {
      @Override
      public void run() {
        for (Map.Entry entry : destinationStats.entrySet()) {
          final String destinationName = entry.getKey();
          final DestinationStatistics stats = entry.getValue();
          final Destination destination = brokerService.getDestination(destinationName);
          if (destination != null) {
            stats.update(destination);
          }
        }
        brokerStats.update(broker);
      }
    }, updateInterval, updateInterval, TimeUnit.MILLISECONDS);

    // store executorService in broker context so it can be stopped later
    brokerService.setContext(new BrokerContext() {
      @Override
      public void stop() {
        executorService.shutdown();
        try {
          executorService.awaitTermination(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
          Thread.currentThread().interrupt();
        }
      }

      @Override
      public void start() {
      }

      @Override
      public void init() {
      }
    });

    return broker;
  }
}

要使用此自定义插件,请将其添加到ActiveMQ配置文件中。以下是一个示例配置文件:


  
    com.example.CustomStatisticsPlugin
  

这将使用CustomStatisticsPlugin类作为自定义插件。

上一篇:ActiveMQ停止通信

下一篇:activemqubuntu

相关内容

热门资讯

闲鱼搜索规则与技巧 闲鱼最新特... 在闲鱼这个二手交易平台上,有很多用户都希望能够找到一些特殊的东西,比如一些罕见的收藏品、独特的手工艺...
玻璃硬盘原理图 玻璃硬盘原理 玻璃硬盘,又称为磁头悬浮硬盘(Magnetic Head Flying Disk,MHFD),是一种...
家里监控最长能保存多少天的记录... 家里监控一般保存多久 随着科技的发展,家庭监控系统已经成为了许多家庭的必备设备,它不仅可以帮助我们...
QQ音乐提示代理模式可能无法正... QQ音乐提示代理模式可能无法正常访问,如上图所示,是怎么回事呢? 这个可能和你的网络设置有关系,首先...
别人打电话听不见我说话怎么回事... 当我们在使用手机时,可能会遇到别人打电话过来听不见声音的情况,这种情况可能是由多种原因导致的,下面我...
ps5手柄可用手机快充充电吗 ... PS5手柄,即PlayStation 5的DualSense手柄,是索尼公司为PlayStation...
华为tag有用吗 华为tag-... 华为Tag是华为手机中的一种功能,它可以帮助用户更好地管理自己的手机数据和应用,通过使用华为Tag,...
a100显卡对应的cuda版本 在进行GPU加速的编程中,CUDA是常用的架构和平台,其版本和显卡型号之间存在着一定的对应关系。本篇...
frp内网穿透配置 HTTP ... HTTP 类型的代理相比于 TCP 类型,不仅在服务端只需要监听一个额外的端口 vhost_http...
hwid是永久激活吗 hwid... HWID,全称Hardware ID,是硬件识别码的缩写,它是计算机硬件制造商为了区分每一台设备而分...