210 likes | 341 Views
Implementing Business Tasks with Session EJBs. Objectives. After completing this lesson, you should be able to do the following: Describe session beans Differentiate stateless session beans from stateful session beans
E N D
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
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
javax.ejb.SessionBean Interface • The SessionBean interface contains the following callback methods: • setSessionContext(SessionContext ctx) • ejbActivate() • ejbPassivate() • ejbRemove()
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.
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
Life Cycle of a Stateless Session Bean Does not exist Container invokesclass.newInstance,setSessionContext(sessCtx),and ejbCreate(). Container invokesejbRemove() . Ready
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; }
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; }
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.
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.
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; } }
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>
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
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(); }...
Client Application for Stateless Session Beans ... // Invoke the business methods with each of the// instances created to observe the state of each // instance 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(); } ...
Life Cycle of a Stateful Session Bean Does not exist Container invokesejbRemove() setSessionContext(sessCtx),and ejbCreate() ejbPassivate() Passivated instances Ready ejbActivate()
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; }
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(); } ...
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
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