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

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

Embed Size (px)

Citation preview

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

12Copyright © 2005, Oracle. All rights reserved.

Implementing Business Tasks with Session EJBs

Page 2: 12 Copyright © 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

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

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

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

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

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

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.

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

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

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

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

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

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

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

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

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

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.

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

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.

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

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

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

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>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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