Java EE specification and multi threading

32,486

Solution 1

This question pops up once in a while.

As per the spec it's not authorized. The best page to look at is this one: Q/A: J2EE Restrictions

That said, there are ways to spawn threads, especiall in Weblogic with the WorkManager.

See these questions:

The fact that the first one targets EJB shouldn't matter that much, and the last one about access to file system is about general restrictions.

Hope it helps.

Solution 2

The recommended way to create threads in a Java EE environment, is with the Concurrency Utils API, which is part of the EE7 specification.

By using this API your new thread will be created, and managed by the container, guaranteeing that all EE services are available to your thread (eg security, transactions).

The examples below are taken from my own site here and here

Using a ManagedExecutorService

To create a new thread using a ManagedExecutorService, first create a task object that implements Callable. Within the call() method we will define the work that we want carried out in a separate thread.

public class ReportTask implements Callable<Report> {

    Logger logger = Logger.getLogger(getClass().getSimpleName());

    public Report call() {
        try {
            Thread.sleep(3000);
        catch (InterruptedException e) {
            logger.log(Level.SEVERE, "Thread interrupted", e);
        }
        return new Report();
    }
}

Then we need to invoke the task by passing it though to the submit() method of the ManagedExecutorService.

@Stateless
public class ReportBean {

    @Resource
    private ManagedExecutorService executorService;

    public void runReports() {
        ReportTask reportTask = new ReportTask();
        Future<Report> future = executorService.submit(reportTask);
    }
}

Using a ManagedThreadFactory

First create a Runnable task which will define what work is to be done in the background.

public class ReportTask implements Runnable {

    Logger logger = Logger.getLogger(getClass().getSimpleName());

    public void run() {
        try {
            //do your background task
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            logger.log(Level.SEVERE, "Thread interrupted", e);
        }
    }
}

To get a container managed thread, we simply ask the ManagedThreadFactory for a new thread, and pass it our Runnable instance. To start the thread we call start().

@Stateless
public class ReportBean {

    @Resource
    private ManagedThreadFactory threadFactory;

    public void runReports() {
        ReportTask reportTask = new ReportTask();
        Thread thread = threadFactory.newThread(reportTask);
        thread.start();
    }
}

Solution 3

These restrictions are in place mostly because Java EE and EJB want to support transparent clustering. For example one server of a cluster should not modify files because these changes can not be easily mirrored to other servers. For threads there is the question if there should be one thread per cluster or per server. These threads also can not be easily monitored by the application server.

That said, it should be possible to create threads, socket connections or access the filesystem in a Java EE server just like in a normal application.

Share:
32,486
Sid
Author by

Sid

Passionate about technology and startups. Love to work with smart people.

Updated on April 26, 2020

Comments

  • Sid
    Sid about 4 years

    I am writing a Java EE application using Struts and Spring. In one of the operations there is heavy database processing, and hence performance issues. What I want to know is can I use multithreading here? I think the Java EE specification does not allow custom threads to be created apart from those created by Server (I use Weblogic). Please guide me through this.

  • Panu Haaramo
    Panu Haaramo over 5 years
    How do you define "Java EE environment"? Only EJBs or using any Java EE feature on any server like Tomcat or Jetty or something else?
  • Chris Ritchie
    Chris Ritchie over 5 years
    @PanuHaaramo A Java EE environment is typically when the code runs in a JavaEE compliant container, such as JBoss, If you are just running standard SE code without a container it is considered non Java EE. In an enterprise environment, things like threads and transactions are managed by the container. Hope this helps
  • Panu Haaramo
    Panu Haaramo over 5 years
    I'm trying to understand what makes container JavaEE compliant. For example in OpenLiberty I can include Java EE features one by one or I can include full Java EE support. Am I running in Java EE environment if I have included for example only JAX-RS? Tomcat is not JavaEE container so is it OK to start my own threads there no matter what JavaEE features I include?
  • Leonardo Savio
    Leonardo Savio about 4 years
    Thank you for all your assistance. @somshivam