jagamot
jagamot

Reputation: 5454

Java MDB - How to keep track of number of messages processed

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?

Upvotes: 0

Views: 496

Answers (1)

Nikos Paraskevopoulos
Nikos Paraskevopoulos

Reputation: 40296

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.

Upvotes: 2

Related Questions