A Thread pattern for JBoss Enterprise applications

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active

Using Threads in your Enterprise applications has been traditionally discouraged as it might conflict with the Application Server's own threads. Nevertheless with the introduction of the Asynchronous EJBs it is possible to explore new design patterns which allow managing your thread pool from within the EJB container.


 Important notice: this tutorial requires JBoss AS 7.2 (EAP 6.1) in order to run, since the AS 7.1.1 distribution does now allow deploying EJB which implement Runnable. See https://issues.jboss.org/browse/AS7-6088  

So let's start from this simple Legacy code which performs a Math summation and uses a Runnable interface to perform the computation:

public class MyRunnable implements Runnable {
    private final long countUntil;

    MyRunnable(long countUntil) {
        this.countUntil = countUntil;

    public void run() {
        System.out.println("Goint to perform Summation!");  
        long sum = 0;
        for (long i = 1; i < countUntil; i++) {
            sum += i;
        System.out.println("Result: " +sum);

So what about using an EJB to execute this operation in a set of Threads ? For this purpose we will use the Executor framework, which provides an implementation of the java.util.concurrent.Executor interface such as Executors.newFixedThreadPool(int n) that which will create n worker threads. The ExecutorService adds lifecycle methods to the Executor, which allows to shutdown the Executor and to wait for termination.

So let's suppose you want to execute your summation as part of a pool of 5 threads:

package com.sample;

import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import java.util.concurrent.Executor;

public   class ExecutorBean implements Executor {

    public void execute(Runnable command) {
        ExecutorService executor = Executors.newFixedThreadPool(5);

Triggering your execution from a Java EE component such as a Servlet is trivial:
private Executor executor;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Runnable worker = new MyRunnable(100);

As you can see, using the Executor pattern together with Async EJBs can be an efficient way to integrate some legacy applications in your Enterprise applications. Feel free to share your opinion about it! I'd like to thanks David Blevins for sharing his thoughts about this pattern on StackOverflow

Related articles available on mastertheboss.com

JBoss AS 7 introduction

This is an introduction tutorial to the newest JBoss AS 7 which a

Develop Java EE applications with JBoss AS 7

In this tutorial we will learn how to create and deploy a Java EE

Adding users with JBoss AS 7

If you are planning to add new users to your management interface

Using JBoss AS 7 management API programmatically

In this tutorial we will show how to use detyped management API t

JNDI view in JBoss AS 7

JNDI can be checked on the naming subsystem of the application se

How to install a module on WildFly / JBoss AS 7

Installing a module on WildFly / JBoss AS 7 requires creating a p