Presentation is loading. Please wait.

Presentation is loading. Please wait.

12 Copyright © 2005, Oracle. All rights reserved. Implementing Business Tasks with Session EJBs.

Similar presentations


Presentation on theme: "12 Copyright © 2005, Oracle. All rights reserved. Implementing Business Tasks with Session EJBs."— Presentation transcript:

1 12 Copyright © 2005, Oracle. All rights reserved. Implementing Business Tasks with Session EJBs

2 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

3 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 clients session Does not survive server, machine, or network crashes Is not saved in permanent storage Implements the javax.ejb.SessionBean interface

4 12-4 Copyright © 2005, Oracle. All rights reserved.

5 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()

6 12-6 Copyright © 2005, Oracle. All rights reserved.

7 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.

8 12-8 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.

9 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

10 12-10 Copyright © 2005, Oracle. All rights reserved.

11 12-11 Copyright © 2005, Oracle. All rights reserved. Life Cycle of a Stateless Session Bean Container invokes class.newInstance, setSessionContext(sessCtx), and ejbCreate(). Ready Container invokes ejbRemove(). Does not exist

12 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; }

13 12-13 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; }

14 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; }

15 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.

16 12-16 Copyright © 2005, Oracle. All rights reserved.

17 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.

18 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; }

19 12-19 Copyright © 2005, Oracle. All rights reserved. Deployment Descriptor Session Bean ( Stateless ) statelessejb Statelessejb StatelessejbHome Statelessejb StatelessejbBean Stateless Container

20 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 beans home interface is published Authenticate itself with the naming service interface Obtain a reference to the beans home interface from the naming service by using the beans URL Invoke the create() method on the home interface Invoke business methods

21 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(); }...

22 12-22 Copyright © 2005, Oracle. All rights reserved.

23 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 // 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(); }...

24 12-24 Copyright © 2005, Oracle. All rights reserved.

25 12-25 Copyright © 2005, Oracle. All rights reserved. Life Cycle of a Stateful Session Bean Ready setSessionContext(sessCtx), and ejbCreate() Container invokes ejbRemove() Passivated instances ejbPassivate() ejbActivate() Does not exist

26 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; }

27 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(); }...

28 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

29 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

30 12-30 Copyright © 2005, Oracle. All rights reserved.

31 12-31 Copyright © 2005, Oracle. All rights reserved.

32 12-32 Copyright © 2005, Oracle. All rights reserved.

33 12-33 Copyright © 2005, Oracle. All rights reserved.

34 12-34 Copyright © 2005, Oracle. All rights reserved.


Download ppt "12 Copyright © 2005, Oracle. All rights reserved. Implementing Business Tasks with Session EJBs."

Similar presentations


Ads by Google