Concurrency Utilities in JavaEE with TomEE?

JavaEE 7 is just out but it already makes a lot of noise! One interesting thing is the JSR 236 (aka Concurrency Utilities for JavaEE). It is basically the specification of what everybody was already doing: using “multi-threading” in JavaEE.

While TomEE still targets JavaEE 6 (hopefully it will target JavaEE 7 after the 1.6.0 release) it can’t be integrated directly in the server. However with a little tip you can already play with it!

Reminder: what is the JSR 236

What do you need to write a concurrent program: some threads, some synchronization, not really more in fact for basic cases (for complex cases you’ll surely want to use JBatch).

What do we have in the JVM itself? ExecutorService and ScheduledExecutorService for the multi-threading. Latch, Locks and “children” for the synchronization. So what is missing?

First it was not allowed by all specifications to play with threading (basically because implementations was very linked to ThreadLocal). Then if your container lets you start threads then you are no more managed. It means you loose the security context, the caller context (if in an ejb it can be a big issue), the caller classloader etc…

If sometimes that’s not an issue you are quickly limited in the interactions you can get with the container. This is mainly what the JSR 236 solves providing a ManagedExecutorService (same for scheduled version) which ensures you the threads are “managed” by the container.

Hello World

Here is a basic application using this new API:

package org.cuee;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.enterprise.concurrent.ManagedScheduledExecutorService;
import java.util.Date;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class DatePrinter {
    private ManagedScheduledExecutorService ses;

    private ScheduledFuture<?> future;

    public void start() {
        future = ses.scheduleAtFixedRate(new Runnable() {
            public void run() {
                System.out.println("Date: " + new Date());
        }, 0, 1000, TimeUnit.MILLISECONDS);

    public void stop() {

The API is known since that’s the same as the JRE one but the type (prefixed with “managed”) ensures you execute your tasks in a JavaEE environment.

Side note: the scheduled executor service version got two new methods (one for Runnable tasks and one for Callable ones). These methods rely on a Trigger to compute runs. It is the generalization of scheduleAtFixedRate method allowing you to schedule task with dynamic rates.

How to test it in TomEE?

To test it with the TomEE the easier is to use the tomee maven plugin to add the javax.enterprise.concurrent-api jar and the implementation (openejb-concurrency-utilities-ee).

Here is a sample configuration (think to add apache snapshot repository to your pom):


    <!-- these 2 lines are not mandatory but makes the dev cooler -->

Think to add as provided the concurrent-api jar to be able to use its types:


Then just compile your war project and run it (mvn tomee:run).


2 thoughts on “Concurrency Utilities in JavaEE with TomEE?

  1. Shailendra

    I am trying to run JSR 236 on tomcat 8 and so far this is the only post which I have found useful. However I am little confused after reading this post. As of TomEE 1.7.1, it is still based on Java EE 6 while JSR 236 is a Java EE 7 api. So is it ok to run a Java EE 7 api on top of a Java EE 6 compliant app server?

    Another confusion is openejb name in the JSR 236 implementation (openejb-concurrency-utilities-ee) which has been mentioned here. JSR 236 is a standalone JSR and I am of the opinion that implementation of a standalone JSR, which does not mention any dependency on any other JSR in its specification, should not have any dependency on any other Java EE api. However in this case it seems that the implementation mentioned here is having dependency on EJB api which means this can’t be used with tomcat 8.

    1. rmannibucau Post author


      New EE 7 spec can be used in EE 6 container if they dont rely on new EE 7 features of existing spec. This is the case here.

      Then OpenEJB is not only an EJB implementation but it is TomEE without Tomcat. That is really a product name historically starting with EJB but now it is really more.

      The jar you mentionned is linked to openejb core to make the impl lighter and easier so it is not designed to run on Tomcat 8 alone (but TomEE 2 does with this jar).


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s