Created
January 1, 2015 12:48
-
-
Save maxant/8f99f79c5c34fead43e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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