Message driven beans are wonderful Java EE technology pieces that allow you to handle and process data concurrently, one message - one task - one thread. However sometimes there is need to keep the state of previous messages so that after collecting few or dozens of messages whole set of these messages might be processed together inside the single the same thread. The point is MDB after receiving message should avoid next steps, somehow informs that for example next 4 messages in the JMS queue should be saved somewhere, and after that after receiving 5th message these 5 messages should be used fo some calculation together. The question is: what kind of technology should be used to handle this scenario? Singleton bean? IMDG (for example JBOSS Cache, Infinispan)? What is the best practise?
EDIT: Unfortunatelly (almost) always two JMS messages are handled by two different instances of Message Driven Bean and each instance of MDB uses different instance of any session bean (stateful or stateless).
@MessageDriven(activationConfig = {...})
public class SomeMDB implements MessageListener {
@EJB AnyService service
}
In that such of case, even if AnyService is a stateful bean, each message has its own instance of SomeMDB and instance of AnyService. Correct me if I am wrong.