Java EE 7: Using Concurrency Utilities in Asynchronous Servlets
This tutorial shows you how to create an application that uses the concurrency utilities for Java Platform, Enterprise Edition 7 (Java EE 7)
to generate an asynchronous random number generator and a distributed search utility.
Time to Complete
Approximately 1 hour
In computer science and computer systems, concurrency refers to the act in which several tasks are executed simultaneously (or asynchronously).
The concurrency utilities for Java EE adds asynchronous capabilities to Java EE.
They also provide a standard way for Java EE applications to submit asynchronous tasks and obtain managed threads from the Java EE application servers.
In this tutorial, you learn how to use Java EE's package for concurrency utilities to create concurrent applications.
In this tutorial, you create two applications. First, you create an asynchronous random number generator by using the ManagedScheduledExecutor class.
The numbers are generated periodically and can be visualized on all clients connected to the server.
Next, you create a minimalistic distributed search utility. This utility searches words asynchronously among different files in your computer.
The following software requirements are needed for this tutorial:
Knowledge of Java EE, especially servlets and Enterprise JavaBeans (EJB)
Basic knowledge of HTML/HTML 5
Introduction to Concurrency Utilities in Java EE 7
The concurrency utilities for the Java EE (JSR-236) package provide a powerful, extensible framework of high-performance threading utilities.
Concurrency utilities provide a standard way of accessing low-level asynchronous processing capabilities.
The following are the primary components of the concurrency utilities:
ManagedExecutorService, is used by applications to execute submitted tasks asynchronously.
ManagedScheduledExecutorService, is used by applications to execute submitted tasks asynchronously at specific times.
ManagedThreadFactory, is used by application to create managed threads.
ContextFactory, is used to create dynamic proxy objects. The objects capture the context of a container and enable applications to run within that context later or to be submitted to a ManagedExecutorService object.
Generating a Random Number
Creating a Java EE 7 Project
Creating a Java Servlet
Generating an Atomic Random Number Asynchronously
Configuring the ManagedExecutorService Object
Testing the RandomNumberGenerator Servlet
Creating a Distributed Search by Using Concurrency Utilities
Preparing the Project
Creating the MultiSearchServlet Class
Creating the SearchResult Class
Creating the MultiSearchEjb Class
Generating the Search Results
Implementing the Search Class
Assigning Asynchronous Permissions by Using Semaphores
Testing the Asynchronous Search
In this tutorial, you learned how to use the ManagedExecutorService and the ManagedScheduledExecutorService
classes of the concurrency utilities for Java EE.
You also learned how to create an asynchronous random number generator that periodically updates its value and how to create a distributed search utility that handles concurrency.
For more information about the topics in this tutorial, see: