Skip to content

Commit 848580b

Browse files
committed
Inject MyTaskWithTransaction to use Managed TX
* Fix up latch ordering * Remove Thread.sleep * Add beans.xml, WF issue?
1 parent d017237 commit 848580b

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

concurrency/managedexecutor/src/main/java/org/javaee7/concurrency/managedexecutor/MyTaskWithTransaction.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,14 @@ public void setId(int id) {
6969
@Override
7070
@Transactional
7171
public void run() {
72+
// a Call to a TX Scoped bean should fail if outside a tx
73+
try {
74+
TestStatus.foundTransactionScopedBean = bean.isInTx();
75+
}
76+
catch(Exception e) {
77+
e.printStackTrace();
78+
}
7279
TestStatus.latch.countDown();
73-
TestStatus.foundTransactionScopedBean = Objects.hashCode(bean) != 0;
7480
}
7581

7682
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
package org.javaee7.concurrency.managedexecutor;
22

3+
import java.io.Serializable;
4+
35
/**
46
* @author Arun Gupta
57
*/
68
@javax.transaction.TransactionScoped
7-
public class MyTransactionScopedBean {
9+
public class MyTransactionScopedBean implements Serializable {
10+
11+
private static final long serialVersionUID = 1L;
12+
13+
public boolean isInTx() {
14+
return true;
15+
}
816
}

concurrency/managedexecutor/src/test/java/org/javaee7/concurrency/managedexecutor/ExecutorInjectTest.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
import javax.annotation.Resource;
1515
import javax.ejb.EJB;
1616
import javax.enterprise.concurrent.ManagedExecutorService;
17+
import javax.inject.Inject;
1718

1819
import org.jboss.arquillian.container.test.api.Deployment;
1920
import org.jboss.arquillian.junit.Arquillian;
2021
import org.jboss.shrinkwrap.api.ShrinkWrap;
22+
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
2123
import org.jboss.shrinkwrap.api.asset.FileAsset;
2224
import org.jboss.shrinkwrap.api.spec.WebArchive;
25+
2326
import static org.junit.Assert.assertTrue;
27+
2428
import org.junit.Before;
2529
import org.junit.Test;
2630
import org.junit.runner.RunWith;
@@ -46,6 +50,7 @@ public class ExecutorInjectTest {
4650
Callable<Product> callableTask;
4751
Runnable runnableTask;
4852
MyTaskWithListener taskWithListener;
53+
@Inject // Inject so we have a managed bean to handle the TX
4954
MyTaskWithTransaction taskWithTransaction;
5055
Collection<Callable<Product>> callableTasks = new ArrayList<>();
5156

@@ -60,16 +65,17 @@ public static WebArchive createDeployment() {
6065
TestStatus.class,
6166
MyTaskWithListener.class,
6267
MyTaskWithTransaction.class,
68+
MyTransactionScopedBean.class,
6369
TestBean.class).
64-
setWebXML(new FileAsset(new File("src/main/webapp/WEB-INF/web.xml")));
70+
setWebXML(new FileAsset(new File("src/main/webapp/WEB-INF/web.xml"))).
71+
addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); // Adding beans.xml shouldn't be required? WildFly Beta1
6572
}
6673

6774
@Before
6875
public void setup() {
6976
callableTask = new MyCallableTask(1);
7077
runnableTask = new MyRunnableTask();
7178
taskWithListener = new MyTaskWithListener(1);
72-
taskWithTransaction = new MyTaskWithTransaction();
7379
for (int i = 0; i < 5; i++) {
7480
callableTasks.add(new MyCallableTask(i));
7581
}
@@ -172,7 +178,6 @@ public void testInvokeAnyWithCallableFromWebXML() throws Exception {
172178
public void testSubmitWithListener() throws Exception {
173179
TestStatus.latch = new CountDownLatch(1);
174180
defaultExecutor.submit(taskWithListener);
175-
Thread.sleep(2000);
176181
assertTrue(TestStatus.latch.await(2000, TimeUnit.MILLISECONDS));
177182
}
178183

0 commit comments

Comments
 (0)