简体   繁体   English

Java MDB - 如何跟踪处理的消息数量

[英]Java MDB - How to keep track of number of messages processed

I have an MDB that processes different types of messages (Object Messages). 我有一个处理不同类型的消息(对象消息)的MDB。 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. 我想跟踪MDB收到的消息数量,并记录收到的消息类型。

Although my MDB is distributed across multiple JVMs, I am fine keeping track of the count at individual JVM. 虽然我的MDB分布在多个JVM上,但我可以很好地跟踪单个JVM的计数。

Using static variables is discouraged in EJB, so other alternatives do we have? 在EJB中不鼓励使用静态变量,所以我们有其他替代方案吗?

One idea: 一个想法:

A @Singleton EJB comes to mind. 想到了一个@Singleton EJB。 I would call it Statistics and keep all kinds of statistics there. 我会称之为Statistics并保留各种统计数据。 Each time a Xxx event, that requires statistics, occurs, a addToXxxCount() method could be called. 每次发生需要统计信息的Xxx事件时,都可以调用addToXxxCount()方法。 The actual count is available from the corresponding getXxxCount() . 实际计数可从相应的getXxxCount() It would require read locks on getXxxCount() methods and write locks on addToXxxCount() (see concurrency for singleton EJBs). 这将需要在读锁getXxxCount()方法和写锁addToXxxCount()请参见单EJB的并发)。 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. 实际跟踪计数( intlong )的变量是非静态的,容器保证EJB只被实例化一次。 And you get a central point of reference for all your statistics. 您将获得所有统计数据的中心参考点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM