TheGuyNextDoor
TheGuyNextDoor

Reputation: 7937

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

Upvotes: 1

Views: 857

Answers (3)

Marcin Dąbrowski
Marcin Dąbrowski

Reputation: 61

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")
})

Upvotes: 2

Aerosteak
Aerosteak

Reputation: 997

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

Upvotes: 2

Henrik Walland Lund
Henrik Walland Lund

Reputation: 142

You could try something like this:

private Object LOCK;
public void onMessage() {
    code…
    synchronized(LOCK) {
        check if system exists, create if necessary
    }
    more code…
}

Upvotes: -1

Related Questions