12 copyright © 2005, oracle. all rights reserved. implementing business tasks with session ejbs
TRANSCRIPT
12Copyright © 2005, Oracle. All rights reserved.
Implementing Business Tasks with Session EJBs
12-2 Copyright © 2005, Oracle. All rights reserved.
Objectives
After completing this lesson, you should be able to do the following:
• Describe session beans
• Differentiate stateless session beans from stateful session beans
• Develop a home interface, a remote interface, and a bean class for session beans
• Develop a client application to invoke the business methods
12-3 Copyright © 2005, Oracle. All rights reserved.
Session Beans
A session bean:
• Implements business processes
• Is short-lived and has the lifetime of a client’s session
• Does not survive server, machine, or network crashes
• Is not saved in permanent storage
• Implements the javax.ejb.SessionBean interface
12-5 Copyright © 2005, Oracle. All rights reserved.
javax.ejb.SessionBean Interface
The SessionBean interface contains the following callback methods:
• setSessionContext(SessionContext ctx)• ejbActivate()• ejbPassivate()• ejbRemove()
12-7 Copyright © 2005, Oracle. All rights reserved.
Types of Session Beans
There are two types of session beans:
• Stateless session bean: A stateless session bean does not maintain the state for a client.
• Stateful session bean: A stateful session bean maintains the state for a client, and the instance variable represents the state of a unique client.
12-9 Copyright © 2005, Oracle. All rights reserved.
When to Use Session Beans
• The state of the bean need not be persistent.• Use stateless session beans when:
– The state need not be maintained for a client– A general task must be performed– Data is fetched only from a database, and data
manipulation is not necessary
• Use stateful session beans when:– Interaction between bean and client must be
maintained across method calls and transactions– A bean works on logic based on entity beans that
represent persistent data
12-11 Copyright © 2005, Oracle. All rights reserved.
Life Cycle of a Stateless Session Bean
Container invokesclass.newInstance,
setSessionContext(sessCtx), and ejbCreate().
Ready
Container invokesejbRemove() .
Does not exist
12-12 Copyright © 2005, Oracle. All rights reserved.
Home Interface for Stateless Session Beans
import javax.ejb.EJBHome;import java.rmi.RemoteException;import javax.ejb.CreateException;public interface StatelessejbHome extends EJBHome {Statelessejb create() throws RemoteException, CreateException;}
12-14 Copyright © 2005, Oracle. All rights reserved.
Remote Interface for Stateless Session Beans
import javax.ejb.EJBObject;import java.rmi.*;
public interface Statelessejb extends EJBObject {public String incrementValue() throws RemoteException;public int getValue()throws RemoteException;}
12-15 Copyright © 2005, Oracle. All rights reserved.
The Session Bean Class
• The class must be defined as public, must not be final, and must not be abstract.
• The class must implement ejbCreate() methods:– There must be an ejbCreate() method for each
create() method of the home interface.
– The signatures of the two methods mentioned above should match.
– The return type of the ejbCreate() method should be void.
– Remote or create exceptions need not be thrown.
• The class can optionally implement the SessionSynchronization interface.
12-17 Copyright © 2005, Oracle. All rights reserved.
The Session Bean Class:Business Methods
• The bean class may define zero or more methods to process the business logic.
• The business methods that are to be accessed by the client applications must be public.
• The business methods must not be declared as final or static.
• The business methods that are to be accessed by clients must be exposed through the component interface.
• The method arguments and return types must be legal types for RMI.
• Application-specific exceptions can be thrown.
12-18 Copyright © 2005, Oracle. All rights reserved.
Bean Class for the Stateless Session Bean
...public class StatelessejbBean implements SessionBean {int value =0;public void ejbCreate() { }public void ejbActivate() { }public void ejbPassivate(){ }public void ejbRemove() { }public void setSessionContext(SessionContext ctx) { }public String incrementValue() { value++; return " value incremented by 1"; }public int getValue() { return value; }}
12-19 Copyright © 2005, Oracle. All rights reserved.
Deployment Descriptor
<?xml version = '1.0' encoding = 'windows-1252'?><!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd"><ejb-jar> <enterprise-beans> <session> <description>Session Bean ( Stateless ) </description> <display-name>statelessejb</display-name> <ejb-name>Statelessejb</ejb-name> <home>StatelessejbHome</home> <remote>Statelessejb</remote> <ejb-class>StatelessejbBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans></ejb-jar>
12-20 Copyright © 2005, Oracle. All rights reserved.
Client Application
To access methods on the bean instance, an EJB client must perform the following operations:
• Obtain access to the naming service (Java Naming and Directory Interface [JNDI]) where the bean’s home interface is published
• Authenticate itself with the naming service interface
• Obtain a reference to the bean’s home interface from the naming service by using the bean’s URL
• Invoke the create() method on the home interface
• Invoke business methods
12-21 Copyright © 2005, Oracle. All rights reserved.
Client Application for Stateless Session Beans
...public class StatelessejbClient { public static void main(String [] args) { StatelessejbClient statelessejbClient = new StatelessejbClient(); try {Context context = getInitialContext(); StatelessejbHome statelessejbHome = (StatelessejbHome)PortableRemoteObject.narrow (context.lookup("Statelessejb"), StatelessejbHome.class);//create 3 instances Statelessejb obj[] = new Statelessejb[3]; for (int i=0;i<3;i++) { obj[i]= statelessejbHome.create(); }...
12-23 Copyright © 2005, Oracle. All rights reserved.
Client Application for Stateless Session Beans
...// Invoke the business methods with each of the// instances created to observe the state of each // instancefor (int i=0;i<3;i++) { System.out.println("Value before increment for object" + i + " "+ obj[i].getValue()); System.out.println( "Calling incrementValue with object" + i+" " +obj[i].incrementValue()); System.out.println("Calling getValue with object" + i+" " +obj[i].getValue()+"\n"); }for (int i=0;i<3;i++) { obj[i].remove(); }...
12-25 Copyright © 2005, Oracle. All rights reserved.
Life Cycle of a Stateful Session Bean
Ready
setSessionContext(sessCtx), and ejbCreate()
Container invokesejbRemove()
Passivated instances
ejbPassivate()
ejbActivate()
Does not exist
12-26 Copyright © 2005, Oracle. All rights reserved.
Home Interface for Stateful Session Bean
import javax.ejb.EJBHome;import java.rmi.RemoteException;import javax.ejb.CreateException;public interface StatefulejbHome extends EJBHome {Statefulejb create(int x) throws RemoteException, CreateException;}
12-27 Copyright © 2005, Oracle. All rights reserved.
Client Application for Stateful Session Bean
...Statefulejb obj[]= new Statefulejb[3]; for (int i=0;i<3;i++) { obj[i]= StatefulejbHome.create(0); } for (int i=0;i<3;i++) { System.out.println("Value before increment for object" + i + " "+ obj[i].getValue()); System.out.println( "Calling incrementValue with object" + i+" " +obj[i].incrementValue()); System.out.println("Calling getValue with object" + i+" " +obj[i].getValue()+"\n"); }for (int i=0;i<3;i++) { obj[i].remove(); }...
12-28 Copyright © 2005, Oracle. All rights reserved.
Summary
In this lesson, you should have learned how to:
• Describe session beans
• Differentiate stateless session beans from stateful session beans
• Develop a stateless session bean
• Develop a client application to invoke a stateless session bean
12-29 Copyright © 2005, Oracle. All rights reserved.
Practices 12-1 and 12-2: Overview
These practices cover the following topics:
• Creating a session bean to validate a card
• Creating a session bean to display the first_name, last_name, email, and department_name of an employee whose employee_id is provided