Hashim Otto
Hashim Otto

Reputation: 416

Can I make it granted EJB @asynchronous method's thread starts after caller thread ends?

I wanted to create some process after MDB's onMessage ends.

I think I can realize it using EJB's @asyncronous. But I'm afraid that this method can start before caller thread ends.

Is there any way to make it sure that this asynchronous method start after caller thread ends, at least caller thread commits it's own transaction.

Thanks, and nice day.

Upvotes: 0

Views: 120

Answers (1)

Camilo
Camilo

Reputation: 1909

You can achieve this by using another MDB instead, if you publish a message from within a transaction, the message won't be published until the publisher transaction has been committed:

@MessageDriven...
public class MDB1 {

  @TransactionAttribute(TransactionAttributeType.REQUIRED)
  public void onMessage(Message message) {
    ...
    producer.send(msg1); // msg1 will be published at commit of this transaction
    ...
    myMethod1();
  }
}

and,

@MessageDriven... // Consumes msg1
public class MDB2 {

  @TransactionAttribute(TransactionAttributeType.REQUIRED)
  public void onMessage(Message message) {
    ...
    myMethod2();
    ...
  }
}

this way (and using CMT), the message msg won't be published until MDB1.onMessage() has returned, and so MDB2.myMethod2() will never be executed before MDB1.onMessage() returns.

Upvotes: 1

Related Questions