Skip to content

Instantly share code, notes, and snippets.

@maxant
Created January 1, 2015 12:48
Show Gist options
  • Save maxant/8f99f79c5c34fead43e5 to your computer and use it in GitHub Desktop.
Save maxant/8f99f79c5c34fead43e5 to your computer and use it in GitHub Desktop.
package ch.maxant.async;
import java.util.concurrent.Future;
import javax.annotation.Resource;
import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.transaction.TransactionSynchronizationRegistry;
@TransactionManagement(TransactionManagementType.CONTAINER)
@Stateless
public class Service2 {
public static final String KEY = "myKey";
@Resource
private TransactionSynchronizationRegistry tsr;
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@Asynchronous
public Future<String> foo(String s) {
try {
// simulate some long running process
Thread.sleep(5000);
s += "<br>Service2: threadId=" + Thread.currentThread().getId() + ", tsrResourceValue="
+ tsr.getResource(KEY) + ", txKey=" + tsr.getTransactionKey() + ", txStatus="
+ tsr.getTransactionStatus();
return new AsyncResult<String>(s);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment