Implementing EJB 3.0 Session Beans
Objectives
Upon completion of this module, you should be able to:
- Create session beans: Essential tasks
- Create session beans: Add life-cycle event handlers
- Package and deploy session beans
- Create a session bean client
Comparison of Stateless and Stateful Behaviors
Session beans can be state lessor stateful.Thestatefulnessofa bean depends on the type of business function it performs:
- In a stateless client-service interaction, no client-specific information is maintained beyond the duration of a single method invocation.
- Shopping Carts
- Multi-page data entry
- Online banking
Stateless Session Bean Operational Characteristics
With stateless session beans:
- The bean does not retain client-specific information.
- A client might not get the same session bean instance.
- Any number of client requests can be handled by the same session bean instance. This has profound performance advantages.
Stateless Session Bean Cardinality
Stateful Session Bean Operational Characteristics
With stateful session beans:
- The bean belongs to a particular client for an entire conversation.
- The client connection exists until the client removes the bean or the session times out.
- The container maintains a separate session object and EJB instance for each client.
Stateful Session Bean Cardinality
Creating Session Beans : Essential Tasks
- Declare a business interface for the session bean.
- Create the session bean class that implements the business interface.
- Configure the session bean by either annotating the session bean class or providing a deployment descriptor (DD).
Declaring a Business Interface for the Session Bean
1 public interface Calculator {
2 int add(int a, int b);
3 int subtract(int a, int b);
4 }
- What are the requirements for the session bean business interface?
- How are business interfaces classified?
Remote interface
- How can a business interface be designated a local interface?
@Local public interface Calculator {...}
@Local public class CalculatorBean implements Calculator
- How can a business interface be designated a remote interface?
@Remote public interface Calculator {...}
@Remote public class CalculatorBean implements Calculator
- Are there any additional requirements for remote interfaces?
- Can a session bean have more than one business interface?
Creating the Session Bean Class that Implements the Business Interface
1 @Remote public interface Calculator {
2 int add(int a, int b);
3 int subtract(int a, int b);
4 }
1 @Stateless public class CalculatorBean implements Calculator {
2 public int add(int a, int b) {
3 return a + b;
4 }
5
6 public int subtract(int a, int b) {
7 return a - b;
8 }
9 }
Requirements for a Session Bean Class
- The class must be a top level class.In addition the class must be defined as public,must not be final,and must not be abstract.
- The class must have a public constructor that takes no parameters. The container uses this constructor to create instances of the session bean class.
- The class must not define thefinalize method.
- The class must implement the methods of the bean s business interface(s), if any.
Annotating the Session Bean Class
Other common annotations
- Transaction-related annotations
- Security-related annotations
- Life-cycle callback handler method annotations
Life Cycle of a Stateless Session Bean
- PostConstruct callback
- PreDestroy callback
Life Cycle of a Stateful Session Bean
- PostConstruct callback
- PreDestroy callback
- PostActivate callback
- PrePassivate callback
Deļ¬ning Life-Cycle Event Handlers
- A call back method is designated using the appropriate callback annotation.
- The same method can be designated to handle multiple callback events.
- Each call back event can only have one callback method designated to handle it.
- A callback method can access entries in the bean’s environment.
- Callback methods can throw runtime exceptions.
- Callback methods must not throw application exceptions.
- Callback methods can have any type of access modifier.
Example of a Callback Method in Bean Class
1 @Stateful public class ShoppingCartBean implements ShoppingCart {
2 private float total;
3 private Vector productCodes;
4 public int someShoppingMethod(){...};
5 //...
6 @PreDestroy private void endShoppingCart() {...};
7
Session Bean Packaging and Deployment
Packaging and deployment tasks
- Optionally create a DD file for the session bean component
- Create a deployable session bean component archive
- Deploy the session bean component archive
Session Bean DD Example
Creating a Session Bean Archive
1. Create a working directory structure.
2. Copy the EJB component class files and helper classes into their corresponding sub directories.For example,you should include the following classes
3. Create a META - INF sub directory off the root directory.
4. From a command line, execute the jar utility to create the EJB JAR archive.
Example of the EJB Module JAR File
Creating a Session Bean Client
No comments:
Post a Comment