1

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 is one way to solve it. Is there a way of avoiding these duplicates in the application, MDB in this case?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
n002213f
  • 7,805
  • 13
  • 69
  • 105

3 Answers3

2

Make sure to have only a single Thread processing all the Messages. This can be configured on the Activation Spec, connection Pool.

Aerosteak
  • 987
  • 1
  • 11
  • 21
2

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")
})
Adeel
  • 2,901
  • 7
  • 24
  • 34
-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…
}
  • this code will synchronize on a single instance only, making LOCK static will synchronize on all class instances, thanks – n002213f Nov 17 '10 at 06:18