Creating Asynchronous Methods in EJB 3.1
Overview
- Fire-and-forget asynchronous methods having
void
return type.
- Retrieve-result-later asynchronous methods having
Future<?>
return type.
Purpose
This tutorial covers creating a EJB 3.1 application that
demonstrates the use of @Asynchronous
annotation in an Enterprise Java Bean(EJB) class or specific
method.
Time to Complete
Approximately 45 minutes.
Introduction
This example demonstrates the use of the EJB 3.1 @Asynchronous
annotation in an EJB class or specific method. In EJB 3.1,
session beans support asynchronous method invocations. Bean
methods annotated with @Asynchronous
are invoked asynchronously. When a client invokes methods with
the @Asynchronous
annotation, the container
immediately returns control to the client and invokes the method
in a different thread. The method may return a future object to
allow the client to check on the status of the method
invocation, and retrieve result values that are asynchronously
produced. EJB 3.1 supports this feature by using Future
,
which is part of standard java concurrency. Future
represents the result of an asynchronous computation.
Before EJB 3.1, if you want to execute an asynchronous
processing you had to use JMS(Java Messaging Services) and a
MDB(Message Driven Bean), not so easy and rather heavy in most
cases. JMS should be really used where messaging is required –
either publish/subscribe topics or point-to-point queues. Now
with EJB 3.1, you can use a simple session bean with the @Asynchronous
annotation on the method which must be called asynchronously.
Two ways of asynchronous EJB invocations are used:
In this tutorial, you will create a Java EE 6 Web Application
and add the following components to it - a Stateless Session
Bean with two asynchronous methods. We define a Servlet
to call the asynchronous methods and to keep track of the
invocation and completion times to demonstrate the asynchronous
nature of the method calls. The index.jsp
will contain a form with a submit button, Run allowing
you to execute the application. The form will submit to the Servlet
which invokes the asynchronous methods defined in the session
bean and the response is re-directed to response.jsp
.
Information about the asynchronous handling procedure is
displayed to users. From this information, users will
notice that the invoker thread and the called asynchronous
thread are working concurrently.
- Download and install Java JDK 7 from this link.
- Download and install NetBeans 7.1.2 with Java EE which includes GlassFish 3.1.2 (Java EE download bundle) from this link. During installation, be sure to check the box to install GlassFish. JUnit is an optional installation and not required for this tutorial.
- Have the software installed as listed under Hardware and Software Requirements section.
- Ensure NetBeans is running.
Hardware and Software Requirements
The following is a list of hardware and software requirements:
Prerequisites
Before starting this tutorial, you should:
Create a Java EE Web Application
To create a Java EE Web Application, perform the
following steps in the NetBeans IDE.
You should now have a Web Application project with an index.jsp file.
Create a Session Bean
To create a CalculatorBean session bean which contains asynchronous methods, perform the following steps in NetBeans IDE.
Create a Servlet
To create a Servlet, perform the below steps in NetBeans IDE.
Create a JSP
To create response.jsp, perform the following steps in NetBeans IDE. This jsp page is forwarded from the servlet, and displays the messages printed.
Modify index.jsp
To modify index.jsp, perform the following steps in
NetBeans IDE.
Deploy the Web Application
To deploy and run the application, perform the following steps in NetBeans IDE.
Summary
- Create asynchronous methods in a stateless session bean.
- Access the asynchronous methods in a Servlet.
In this tutorial, you have learned how to:
- JSR 318: Enterprise JavaBeans 3.1
- Java
EE 6 Tutorial
- Enterprise JavaBeans Technology
- Creating an Enterprise Application with EJB 3.1
- Future
Interface
- Lead Curriculum Developer: Anjana Shenoy
Resources
Credits
To help navigate this Oracle by Example, note the following:
- Hiding Header Buttons:
- Click the Title to hide the buttons in the header. To show the buttons again, simply click the Title again.
- Topic List Button:
- A list of all the topics. Click one of the topics to navigate to that section.
- Expand/Collapse All Topics:
- To show/hide all the detail for all the sections. By default, all topics are collapsed
- Show/Hide All Images:
- To show/hide all the screenshots. By default, all images are displayed.
- Print:
- To print the content. The content currently displayed or hidden will be printed.
To navigate to a particular section in this tutorial, select the topic from the list.