Chapter 1. Concurrency Utilities for Java EE

Table of Contents

1.1. Overview
1.2. Managed Task
1.3. Container Thread Context

This chapter describes the background to the introduction of Concurrency Utilities for Java EE and its functionality.

1.1. Overview

It is recommended that application servers do not use concurrency APIs (Thread, Timer, ExecutorService, etc.) provided in Java SE in the EJB or a web component. It is assumed that Java EE application components (Servlet, EJB, etc.) are executed in a thread managed by an application server. As well, it is assumed that the functions provided in a container are executed in the same thread.

For these reasons, application components cannot safely use Java EE services in a thread that is not managed by a container. Furthermore, if resources are used in a thread that is not managed by a container, potential issues regarding usability, security, reliability, and scalability can occur in Java EE.

In order to solve problems that occur in unmanaged threads, the Concurrency Utilities for Java EE (JSR-236) specification, which extended the concurrency utilities of Java SE, is provided.

This technology guarantees the execution of applications without damaging the integrity of containers in the Java EE environment.

1.2. Managed Task

In order to reduce consumption of unnecessary resources, a container must pool resources and manage lifecycles. However, if asynchronous tasks are executed in a component by using the concurrency APIs provided in Java SE, then the container cannot manage the resources as it cannot recognize them.

As a result, this specification has defined managed tasks by expanding on the tasks defined in java.utill.concurrent in the existing Java SE. This allows a container to manage general tasks as managed tasks, and maintains the execution context when a task is executed asynchronously.

1.3. Container Thread Context

In the Java EE environment, a container contains the context information of each service when a service is executed

If the Java SE's concurrency API is used in a component, then to maintain the context information of a service in a thread where a new container has been created, application developers must propagate the context in the following method.

  1. Store the container context of the application component thread.

  2. Determine which container context to store and propagate.

  3. Apply the container context in the newly created thread.

  4. Restore the context of the original component thread.

By using the above method, a task can be executed while maintaining the context of Java SE's concurrency API. However, if the Concurrency Utilities for Java EE services is used, user-defined tasks can be easily sent to managed objects. This allows the context to be automatically maintained and restored.

When Executing a Task by Maintaining the Context

  • java.util.concurrent.Callable

    • call()

  • java.lang.Runnable

    • run()

  • javax.enterprise.concurrent.ManagedTaskListener

    • taskAborted

    • taskSubmitted

    • taskStarting

  • javax.enterprise.concurrent.ManagedTaskListener

    • taskAborted

    • taskSubmitted

    • taskStarting

  • javax.enterprise.concurrent.Trigger

    • getNextRuntime()

    • skipRun()