This tutorial covers creating a EJB 3.1 application that
demonstrates the use of
annotation in an Enterprise Java Bean(EJB) class or specific
Time to Complete
Approximately 45 minutes.
This example demonstrates the use of the EJB 3.1
annotation in an EJB class or specific method. In EJB 3.1,
session beans support asynchronous method invocations. Bean
methods annotated with
are invoked asynchronously. When a client invokes methods with
@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
which is part of standard java concurrency.
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
annotation on the method which must be called asynchronously.
Two ways of asynchronous EJB invocations are used:
- Fire-and-forget asynchronous methods having
- Retrieve-result-later asynchronous methods having
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
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
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.
Hardware and Software Requirements
The following is a list of hardware and software requirements:
- 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.
Before starting this tutorial, you should:
- Have the software installed as listed under Hardware and Software Requirements section.
- Ensure NetBeans is running.
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.
To modify index.jsp, perform the following steps in
Deploy the Web Application
To deploy and run the application, perform the following steps in NetBeans IDE.
In this tutorial, you have learned how to:
- Create asynchronous methods in a stateless session bean.
- Access the asynchronous methods in a Servlet.
- JSR 318: Enterprise JavaBeans 3.1
EE 6 Tutorial
- Enterprise JavaBeans Technology
- Creating an Enterprise Application with EJB 3.1
- Lead Curriculum Developer: Anjana Shenoy
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.
- 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.