如何在最先进的 MDB 中进行统计收集?
我有多个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你的意思是:
这当然不是一个选择。无法保证带有
@Timeout
注释的方法将针对所有 bean 实例完全运行,更不用说您要求每 30 秒运行一次了。超时方法在 MDB 的空闲实例之一上运行,并且容器选择它。就我个人而言,我会这样做:让所有 MDB(所有实例)在其处理逻辑结束时将部分统计信息发送到队列。我将编写一个单独的组件,该组件将:
“组件”可以是计时器任务,也可以是单独的独立应用程序。
另一种可能的解决方案可能是单个有状态 bean,MDB 将依次调用该 bean 来更新统计信息。但是,存在以下限制:
I suppose you mean this:
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.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:
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: