如何在最先进的 MDB 中进行统计收集?

发布于 2024-12-25 16:42:29 字数 2294 浏览 0 评论 0原文

我有多个 MDB(以及大量 mdb 实例)作为消息的使用者。我必须收集这些 Bean 内的某些统计信息,并每 X(当前为 30)秒将它们发送到 JMS 目的地。

可以在 bean 本身中执行此操作吗?

例如:bean 在本地收集数据并具有使用 @Scheduled 注释的 writeStatistic() 方法。

是否可以集中进行统计工作?

中央 bean 从所有 bean 实例收集所有统计数据并将其发送。如果可以的话,怎样才能做到呢?

编辑

我通过编写一个单例会话 bean 解决了我的任务,如下所示:

@Singleton
@Startup
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
@Asynchronous
public class StatisticsCollector {

    private ConcurrentMap<Integer, ConcurrentMap<String, AtomicInteger>> statistics = new ConcurrentHashMap<Integer, ConcurrentMap<String, AtomicInteger>>();

    public void trackDatum(int projectId, String property, int increment) {
        LOG.debug("Tracking datum: project: " + projectId + ", property: " + property + ", increment: " + increment);

        // get statistics for project
        ConcurrentMap<String, AtomicInteger> productstats;
        if (!statistics.containsKey(projectId)) {
            synchronized (statistics) {
                if (!statistics.containsKey(projectId)) {
                    productstats = new ConcurrentHashMap<String, AtomicInteger>();
                    statistics.put(projectId, productstats);
                } else {
                    productstats = statistics.get(projectId);
                }
            }
        } else {
            productstats = statistics.get(projectId);
        }

        // get current counter for property
        AtomicInteger value;
        if (!productstats.containsKey(property)) {
            synchronized (productstats) {
                if (!productstats.containsKey(property)) {
                    value = new AtomicInteger();
                    productstats.put(property, value);
                } else {
                    value = productstats.get(property);
                }
            }
        } else {
            value = productstats.get(property);
        }

        // increment
        value.addAndGet(increment);
    }

    @Schedule(minute = "*", hour = "*", second = "*/30", persistent = false)
    public void sendStatistics() {
        // send statistics to remote consumer via JMS
    }
}

我自己进行了并发管理,因为我想从 bean 中获得尽可能多的性能。

I have multiple MDBs (and plenty of mdb instances) as consumers for messages. I have to collect certain statistics inside these Beans and send them every X (currently 30) seconds to a JMS destination.

Is it ok to do this in the bean itself?

for example: the bean gathers the data localy and has a writeStatistic() method that uses the @Scheduled annotation.

Is it possible to do the statistic stuff centralised?

a central bean collects all statistics data from all bean instances and sends it of. If this is possible, how can it be done?

EDIT

I solved my task by writing a singleton session bean that looks like this:

@Singleton
@Startup
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
@Asynchronous
public class StatisticsCollector {

    private ConcurrentMap<Integer, ConcurrentMap<String, AtomicInteger>> statistics = new ConcurrentHashMap<Integer, ConcurrentMap<String, AtomicInteger>>();

    public void trackDatum(int projectId, String property, int increment) {
        LOG.debug("Tracking datum: project: " + projectId + ", property: " + property + ", increment: " + increment);

        // get statistics for project
        ConcurrentMap<String, AtomicInteger> productstats;
        if (!statistics.containsKey(projectId)) {
            synchronized (statistics) {
                if (!statistics.containsKey(projectId)) {
                    productstats = new ConcurrentHashMap<String, AtomicInteger>();
                    statistics.put(projectId, productstats);
                } else {
                    productstats = statistics.get(projectId);
                }
            }
        } else {
            productstats = statistics.get(projectId);
        }

        // get current counter for property
        AtomicInteger value;
        if (!productstats.containsKey(property)) {
            synchronized (productstats) {
                if (!productstats.containsKey(property)) {
                    value = new AtomicInteger();
                    productstats.put(property, value);
                } else {
                    value = productstats.get(property);
                }
            }
        } else {
            value = productstats.get(property);
        }

        // increment
        value.addAndGet(increment);
    }

    @Schedule(minute = "*", hour = "*", second = "*/30", persistent = false)
    public void sendStatistics() {
        // send statistics to remote consumer via JMS
    }
}

I did the concurrency management myself as i wanted to get as much performance out of the bean as i could.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海夕 2025-01-01 16:42:29

可以在 bean 本身中执行此操作吗?

我想你的意思是:

@MessageDriven(...)
public MDBean implements MessageListener {
     private Statistic statisticForThisBean;
     // ...

     @Timeout
     public void sendStatisticForThisBean() {
         // ...
     }
}

这当然不是一个选择。无法保证带有 @Timeout 注释的方法将针对所有 bean 实例完全运行,更不用说您要求每 30 秒运行一次了。超时方法在 MDB 的空闲实例之一上运行,并且容器选择它。

是否可以集中进行统计工作?

就我个人而言,我会这样做:让所有 MDB(所有实例)在其处理逻辑结束时将部分统计信息发送到队列。我将编写一个单独的组件,该组件将:

  • 仅存在于一个实例中,在
  • 30 秒内(循环)收集统计队列中的所有消息,生成摘要,然后将其发送到最终目的地,然后重新启动。

“组件”可以是计时器任务,也可以是单独的独立应用程序。

另一种可能的解决方案可能是单个有状态 bean,MDB 将依次调用该 bean 来更新统计信息。但是,存在以下限制:

  • 很可能会导致性能问题,除非 SFSB 方法调用速度很快(即统计数据不复杂时),否则
  • 它无法异步运行,因此无法保证调用之间的 30 秒跨度到统计收集引擎;如果 JMS 流量完全停止,那么 SFSB 也将停止响应。

Is it ok to do this in the bean itself?

I suppose you mean this:

@MessageDriven(...)
public MDBean implements MessageListener {
     private Statistic statisticForThisBean;
     // ...

     @Timeout
     public void sendStatisticForThisBean() {
         // ...
     }
}

It's certainly not an option. There's no guarantee that the @Timeout-annotated method will run for all bean instances at all, not to mention your requirement of running every 30 seconds. The timeout method runs on one of free instances of an MDB, and the container chooses it.

Is it possible to do the statistic stuff centralised?

Personally, I'd do it this way: have all MDBs (all instances) send partial statistics to a queue, at the end of their processing logic. I'd write a separate component that would:

  • exist in one instance only,
  • collect all messages from the statistics queue for 30 seconds (in a loop), producing a summary, then sending it to the final destination, then restart.

The "component" could be a timer task, or a separate stand-alone application.

Another possible solution could be a single stateful bean, that MDBs would call in turn to update statistics. However, the following limitations apply:

  • it's likely to cause performance problems, unless SFSB methods calls are fast (i.e. when the statistics are not complicated),
  • it can't be run asynchronously, so you can't guarantee the 30 seconds span between calls to the statistic-gathering engine; if the JMS traffic stops completely, so the SFSB will stop responding, too.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文