Synchronizing access to a MDB method across instances
I have an Message Driven Bean, which receives Audit messages. These messages also have information about the system being audited. When a message is received, the MDB can create the system if it does not exists or reuse an existing system.
My challenge is that when a lot of messages from a new system are received simultaneously, multiple MDB instances are created and can end up creating duplicate systems. Adding a constraint to the database 开发者_StackOverflow中文版is one way to solve it. Is there a way of avoiding these duplicates in the application, MDB in this case?
Make sure to have only a single Thread processing all the Messages. This can be configured on the Activation Spec, connection Pool.
MessageDrivenBean
can be synchronized via queue Destination Options. In my case MessageDriven
annotation looks like this for single message processing:
@MessageDriven(name = "ArchiveCounterStJmsListener", activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = LINK_TO_QUEUE + "?consumer.dispatchAsync=false&consumer.prefetchSize=1"),
@ActivationConfigProperty(propertyName = "maxSessions", propertyValue = "1")
})
You could try something like this:
private Object LOCK;
public void onMessage() {
code…
synchronized(LOCK) {
check if system exists, create if necessary
}
more code…
}
精彩评论