Skip to content

Commit 966c5f2

Browse files
committed
Merge pull request #194 from pdudits/transactional-synchronizer
Transaction-aware ReceptionSynchronizer
2 parents 5c6c1b4 + 1e873ca commit 966c5f2

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

jms/send-receive/src/test/java/org/javaee7/jms/send/receive/mdb/ReceptionSynchronizer.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,49 @@
55
import java.util.Map;
66
import java.util.concurrent.CountDownLatch;
77
import java.util.concurrent.TimeUnit;
8+
import javax.annotation.Resource;
89
import javax.interceptor.AroundInvoke;
910
import javax.interceptor.InvocationContext;
11+
import javax.transaction.Status;
12+
import javax.transaction.Synchronization;
13+
import javax.transaction.TransactionSynchronizationRegistry;
1014

1115
/**
12-
* Allows test to wait until a method is invoked. Note that this gets applied as EJB interceptor, and therefore
13-
* returning from {@link #waitFor(java.lang.Class, java.lang.String) } does not guarantee that the bean's transaction
14-
* is already committed.
16+
* Allows test to wait until a method is invoked.
1517
*
1618
* @author Patrik Dudits
1719
*/
1820
public class ReceptionSynchronizer {
1921

2022
private final static Map<Method, CountDownLatch> barrier = new HashMap<>();
2123

24+
@Resource
25+
TransactionSynchronizationRegistry txRegistry;
26+
2227
@AroundInvoke
23-
public Object invoke(InvocationContext ctx) throws Exception {
28+
public Object invoke(final InvocationContext ctx) throws Exception {
29+
boolean transactional = false;
2430
try {
2531
System.out.println("Intercepting "+ctx.getMethod().toGenericString());
32+
transactional = txRegistry != null && txRegistry.getTransactionStatus() != Status.STATUS_NO_TRANSACTION;
33+
if (transactional) {
34+
txRegistry.registerInterposedSynchronization(new Synchronization() {
35+
@Override
36+
public void beforeCompletion() {
37+
38+
}
39+
40+
@Override
41+
public void afterCompletion(int i) {
42+
registerInvocation(ctx.getMethod());
43+
}
44+
});
45+
}
2646
return ctx.proceed();
2747
} finally {
28-
registerInvocation(ctx.getMethod());
48+
if (!transactional) {
49+
registerInvocation(ctx.getMethod());
50+
}
2951
}
3052
}
3153

@@ -59,7 +81,7 @@ public static void waitFor(Class<?> clazz, String methodName) throws Interrupted
5981
}
6082

6183
private static void waitFor(Method method) throws InterruptedException {
62-
CountDownLatch latch = null;
84+
CountDownLatch latch;
6385
synchronized (barrier) {
6486
if (barrier.containsKey(method)) {
6587
latch = barrier.get(method);

0 commit comments

Comments
 (0)