Managedexecutor

Run
How to run the sample
The source code for this sample can be found in the javaee7-samples GitHub repository. The first thing we need to do is to get the source by downloading the repository and then go into the samples folder:
git clone git://github.com/javaee-samples/javaee7-samples.git
cd javaee7-samples/concurrency/managedexecutor/
Now we are ready to start testing. You can run all the tests in this sample by executing:
mvn test
Or you can run individual tests by executing one of the following:
mvn test -Dtest=ExecutorJNDITestmvn test -Dtest=ExecutorInjectTest

ExecutorJNDITest

Missing a description for the story. Add some javadoc to the TestClass. Show me how!
Missing a description for the deployment. Add some javadoc to the @Deployment method. Show me how!
@Deployment
public static WebArchive createDeployment() {
    return ShrinkWrap.create(WebArchive.class).
            addClasses(MyRunnableTask.class,
                    MyCallableTask.class,
                    Product.class,
                    TestStatus.class);
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testSubmitWithRunnableDefault() throws Exception {
    TestStatus.latch = new CountDownLatch(1);
    defaultExecutor.submit(runnableTask);
    assertTrue(TestStatus.latch.await(2000, TimeUnit.MILLISECONDS));
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testSubmitWithCallableDefault() throws Exception {
    TestStatus.latch = new CountDownLatch(1);
    Future<Product> future = defaultExecutor.submit(callableTask);
    assertTrue(TestStatus.latch.await(2000, TimeUnit.MILLISECONDS));
    assertEquals(1, future.get().getId());
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testInvokeAllWithCallableDefault() throws Exception {
    List<Future<Product>> results = defaultExecutor.invokeAll(callableTasks);
    int count = 0;
    for (Future<Product> f : results) {
        assertEquals(count++, f.get().getId());
    }
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testInvokeAnyWithCallableDefault() throws Exception {
    Product results = defaultExecutor.invokeAny(callableTasks);
    assertTrue(results.getId() >= 0);
    assertTrue(results.getId() <= 5);
}

ExecutorInjectTest

Missing a description for the story. Add some javadoc to the TestClass. Show me how!
Missing a description for the deployment. Add some javadoc to the @Deployment method. Show me how!
@Deployment
public static WebArchive createDeployment() {
    return ShrinkWrap.create(WebArchive.class).
            addClasses(MyRunnableTask.class,
                    MyCallableTask.class,
                    Product.class,
                    TestStatus.class,
                    MyTaskWithListener.class,
                    MyTaskWithTransaction.class,
                    MyTransactionScopedBean.class,
                    TestBean.class).
            setWebXML(new FileAsset(new File("src/main/webapp/WEB-INF/web.xml"))).
            addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); // Adding beans.xml shouldn't be required? WildFly Beta1
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testSubmitWithRunnableDefault() throws Exception {
    TestStatus.latch = new CountDownLatch(1);
    defaultExecutor.submit(runnableTask);
    assertTrue(TestStatus.latch.await(2000, TimeUnit.MILLISECONDS));
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testSubmitWithCallableDefault() throws Exception {
    TestStatus.latch = new CountDownLatch(1);
    Future<Product> future = defaultExecutor.submit(callableTask);
    assertTrue(TestStatus.latch.await(2000, TimeUnit.MILLISECONDS));
    assertEquals(1, future.get().getId());
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testInvokeAllWithCallableDefault() throws Exception {
    List<Future<Product>> results = defaultExecutor.invokeAll(callableTasks);
    int count = 0;
    for (Future<Product> f : results) {
        assertEquals(count++, f.get().getId());
    }
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testInvokeAnyWithCallableDefault() throws Exception {
    Product results = defaultExecutor.invokeAny(callableTasks);
    assertTrue(results.getId() >= 0);
    assertTrue(results.getId() <= 5);
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testSubmitWithRunnableNoName() throws Exception {
    TestStatus.latch = new CountDownLatch(1);
    executorNoName.submit(runnableTask);
    assertTrue(TestStatus.latch.await(2000, TimeUnit.MILLISECONDS));
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testSubmitWithCallableNoName() throws Exception {
    TestStatus.latch = new CountDownLatch(1);
    Future<Product> future = executorNoName.submit(callableTask);
    assertTrue(TestStatus.latch.await(2000, TimeUnit.MILLISECONDS));
    assertEquals(1, future.get().getId());
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testInvokeAllWithCallableNoName() throws Exception {
    List<Future<Product>> results = executorNoName.invokeAll(callableTasks);
    int count = 0;
    for (Future<Product> f : results) {
        assertEquals(count++, f.get().getId());
    }
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testInvokeAnyWithCallableNoName() throws Exception {
    Product results = executorNoName.invokeAny(callableTasks);
    assertTrue(results.getId() >= 0);
    assertTrue(results.getId() <= 5);
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testSubmitWithRunnableFromWebXML() throws Exception {
    TestStatus.latch = new CountDownLatch(1);
    executorFromWebXml.submit(new MyRunnableTask());
    assertTrue(TestStatus.latch.await(2000, TimeUnit.MILLISECONDS));
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testSubmitWithCallableFromWebXML() throws Exception {
    TestStatus.latch = new CountDownLatch(1);
    Future<Product> future = executorFromWebXml.submit(new MyCallableTask(1));
    assertTrue(TestStatus.latch.await(2000, TimeUnit.MILLISECONDS));
    assertEquals(1, future.get().getId());
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testInvokeAllWithCallableFromWebXML() throws Exception {
    List<Future<Product>> results = executorFromWebXml.invokeAll(callableTasks);
    int count = 0;
    for (Future<Product> f : results) {
        assertEquals(count++, f.get().getId());
    }
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testInvokeAnyWithCallableFromWebXML() throws Exception {
    Product results = executorFromWebXml.invokeAny(callableTasks);
    assertTrue(results.getId() >= 0);
    assertTrue(results.getId() <= 5);
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testSubmitWithListener() throws Exception {
    TestStatus.latch = new CountDownLatch(1);
    defaultExecutor.submit(taskWithListener);
    assertTrue(TestStatus.latch.await(2000, TimeUnit.MILLISECONDS));
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testSubmitWithTransaction() throws Exception {
    TestStatus.latch = new CountDownLatch(1);
    defaultExecutor.submit(taskWithTransaction);
    assertTrue(TestStatus.latch.await(2000, TimeUnit.MILLISECONDS));
    assertTrue(TestStatus.foundTransactionScopedBean);
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testSubmitWithEJB() throws Exception {
    assertTrue(ejb.doSomething());
}

Share the Knowledge

Find this sample useful? Share on

There's a lot more about JavaEE to cover. If you're ready to learn more, check out the other available samples.

Help Improve

Find a bug in the sample? Something missing? You can fix it by editing the source, making the correction and sending a pull request. Or report the problem to the issue tracker

Recent Changelog

  • Oct 05, 2014: #252 fixed arquillian configuration for the jobs by John D. Ament
  • Jul 15, 2014: Removed header license. the licensing is now referenced in the license file in the root of the project by Roberto Cortez
  • Mar 20, 2014: Update arquillian.xml cr1 references to final by Aslak Knutsen
  • Dec 24, 2013: Updated to wildfly 8.0.0.cr1 by Roberto Cortez
  • Nov 28, 2013: Inject mytaskwithtransaction to use managed tx by Aslak Knutsen
  • Nov 28, 2013: Using countdownlatch for more accurate test result calculation and adding some more tests by arun-gupta
  • Nov 27, 2013: Fixed the test by arun-gupta
  • Nov 27, 2013: Use servlet 3.0 protocol for concurrent tests by Aslak Knutsen
  • Nov 27, 2013: Missed a file by arun-gupta
  • Nov 27, 2013: Adding new unit tests, arquillian-enabling them by arun-gupta
How to help improve this sample
The source code for this sample can be found in the javaee7-samples GitHub repository. The first thing you need to do is to get the source by downloading the repository and then go into the samples folder:
git clone git://github.com/javaee-samples/javaee7-samples.git
cd javaee7-samples/concurrency/managedexecutor/

Do the changes as you see fit and send a pull request!

Good Luck!