Question

I have an MDB that processes different types of messages (Object Messages). I want to keep a track of the number of messages received by the MDB and as well as keep count of the type of the messages received.

Although my MDB is distributed across multiple JVMs, I am fine keeping track of the count at individual JVM.

Using static variables is discouraged in EJB, so other alternatives do we have?

Was it helpful?

Solution

One idea:

A @Singleton EJB comes to mind. I would call it Statistics and keep all kinds of statistics there. Each time a Xxx event, that requires statistics, occurs, a addToXxxCount() method could be called. The actual count is available from the corresponding getXxxCount(). It would require read locks on getXxxCount() methods and write locks on addToXxxCount() (see concurrency for singleton EJBs). Alternatively, you can synchronize yourself.

The variable that actually keeps track of the count (int or long) is non-static and the container guarantees the EJB is intantiated only once. And you get a central point of reference for all your statistics.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top