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

Creating a stateful session bean


In order to illustrate the creation of a stateful EJB, we will use a bean to maintain a list of names. A name will be entered in the index.jsp page and passed to a servlet. The servlet will add the name to the stateful bean. Unique to the stateful EJB is the process of passivation. When a stateful bean is experiencing a period of inactivity, the EJB container may decide to remove it from memory temporarily. This process is called passivation. Most of the state of the EJB is saved automatically except for transient fields. When the EJB is restored, the stateless EJB has its original content except for the transient fields.

Getting ready

Creating a stateful session bean requires:

  1. Annotating a class with the @Stateful annotation

  2. Adding appropriate business methods

Earlier versions of EJB required the use of local and/or remote interfaces. This is no longer necessary.

To use a session bean, inject the EJB into the client using the @EJB annotation followed by the declaration...