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.