Book Image

EJB 3.1 Cookbook

By : Richard M. Reese
Book Image

EJB 3.1 Cookbook

By: Richard M. Reese

Overview of this book

<p>Enterprise Java Beans enable rapid and simplified development of secure and portable applications based on Java technology.Creating and using EJBs can be challenging and rewarding. Among the challenges are learning the EJB technology itself, learning how to use the development environment you have chosen for EJB development, and the testing of the EJBs.<br /><br />This EJB 3.1 Cookbook addresses all these challenges and covers new 3.1 features, along with explanations of useful retained features from earlier versions. It brings the reader quickly up to speed on how to use EJB 3.1 techniques through the use of step-by-step examples without the need to use multiple incompatible resources. The coverage is concise and to the point, and is organized to allow you to quickly find and learn those features of interest to you.<br /><br />The book starts with coverage of EJB clients. The reader can choose the chapters and recipes which best address his or her specific needs. The newer EJB technologies presented include singleton beans which support application wide needs and interceptors to permit processing before and after a target method is invoked. Asynchronous invocation of methods and enhancements to the timer service are also covered. <br /><br />The EJB 3.1 CookBook is a very straightforward and rewarding source of techniques supporting Java EE applications.</p>
Table of Contents (19 chapters)
EJB 3.1 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using an asynchronous method to create a background process


Accessing an EJB synchronously allows the client to continue its work without having to wait for the EJB to return. This can be used in one of two ways. The first technique is done in an "invoke and forget" manner where the request is made of the EJB and the client is not concerned about the success or failure of the request. The second technique invokes the method but does not wait for the method to complete. The method returns a Future object. This object is used later to determine the result of the request.

In this recipe, we will develop a stateless PrintServlet EJB with two methods: printAndForget and printAndCheckLater, used to demonstrate the use of asynchronous methods.

How to do it...

Create a Java EE application called AsynchronousExample. Add a packt package to the EJB module and a stateless EJB called PrintBean. Add a servlet package to the WAR module and a servlet called PrintServlet. The PrintServlet uses the PrintBean...