Session Beans and Business Logic - PowerPoint PPT Presentation

1 / 56
About This Presentation
Title:

Session Beans and Business Logic

Description:

Middleware is a layer of software between the network and the applications that ... are instantiated, the EJB container can passivate some of them(by writing the ... – PowerPoint PPT presentation

Number of Views:115
Avg rating:3.0/5.0
Slides: 57
Provided by: csee1
Category:

less

Transcript and Presenter's Notes

Title: Session Beans and Business Logic


1
Session Beans and Business Logic
  • Presented By
  • Sethu Ram Kettimuthu

2
For Starters.. ?
3
A look at the Forest
  • What is a middle ware?
  • Middleware is a layer of software between the
    network and the applications that provides
    services such as identification, authentication,
    authorization, directories, and security.
  • By promoting standardization and
    interoperability, middleware will make advanced
    network applications much easier to use.

4
Trees of the Forest
  • CORBA

5
Corba (Contd)
  • The CORBA specification only defines a set of
    conventions and protocols that must be followed
    by CORBA implementations
  • CORBA does not make any restrictions on language
    or underlying operating system
  • Because of CORBA's heterogeneous nature, a
    neutral language was needed to perform the task
    of defining the interfaces to ORB-based objects
  • You can implement IDL interfaces using any
    programming language for which an IDL mapping is
    available

6
E-Speak
  • Fundamentally e-speak is a distributed
    development environment, just like Java/RMI, just
    like CORBA, just like the Web itself
  • To access a service, a client
  • Creates a connection to the e-Speak engine 
  • Identifies a contract and vocabulary to use when
    searching for services 
  • Locates the service using ESServiceFinder 
  • Invokes the methods specified by the service
    interface 

7
(No Transcript)
8
(No Transcript)
9
.Net Com
  • With .NETMicrosoft's new Web services
    platformyour applications, services, and devices
    work together to provide access to the
    information you need at anytime and any place
  • .net is MS equivalent of J2EE
  • Com is MS equivalent of EJB
  • C is MS equivalent of Java

10
Now to EJB..at last
  • Why EJB?
  • EJB enables rapid development of
    mission-critical application that are versatile,
    reusable and portable across middleware while
    protecting IT investment and preventing vendor
    lock-in
  • What is EJB?
  • Enterprise beans are server components written
    in the Java programming language.
  • Enterprise beans contain the business logic for
    your application.
  • For example, a checkbook client might invoke
    the debit and credit methods of an account
    enterprise bean

11
(No Transcript)
12
key features of the EJB technology
  • EJB components are server-side components written
    entirely in the Java programming language
  • EJB components contain business logic only
  • System-level services are automatically managed
    for the EJB component by the EJB server
  • EJB components are fully portable across any EJB
    server and any OS

13
Types of enterprise beans
  • Session Beans
  • (More on Session Beans rest of the seminar)
  • Entity Beans
  • - An entity bean represents a business object in
    a persistent storage mechanism such as a database
  • - May be shared by multiple clients
  • - Persistent - even when the EJB container
    terminates, the entity state remains in a
    database.

14
Session Beans
  • Purpose Performs a task for a client
  • Shared Access May have one client
  • Persistence Not persistent. When the client
    terminates its session bean is no longer
    available.

15
Session Beans Contd..
  • The client accesses remote services by invoking
    the session bean's methods.
  • The session bean performs work for its client,
    shielding the client from complexity by executing
    business tasks inside the server
  • It is similar to an interactive session
  • The Session Beans Terminates with the client
  • Session beans are powerful because they extend
    the reach of your clients into remote servers--
    yet they're easy to build

16
How to Write Session Beans
  • To write a session enterprise bean class, the
    class must implement the javax.ejb.SessionBean
  • interface
  • The javax.ejb.SessionBean interface extends the
    more generic javax.ejb.Enterprise Bean interface
  • So does the javax.ejb.EntityBean

17
Methods in SessionBean Interface
  • Javax.ejb.SessionBean Interface
  • Public interface javax.ejb.SessionBean extends
    javax.ejb.EnterpriseBean
  • public abstract void setSessionContext(SessionCon
    text ctx)
  • throws java.rmi.RemoteException
  • public abstract void ejbPassivate() throws
    java.rmi.RemoteException
  • public abstract void ejbActivate() throws
    java.rmi.RemoteException
  • public abstract void ejbRemove() throws
    java.rmi.RemoteException

18
A look at each method
  • setSessionContext(SessionContext ctx)
  • Container calls this method to associate the bean
    with a session context
  • A session context is the beans gateway to
    interact with the container
  • The bean can use the session contexts to query
    the container about the current transactional
    state,security state etc..

19
A look at each method
  • ejbCreate(..)
  • Initializes the session bean
  • Can define one or more ejbCreate() methods and
    each can take different arguments
  • Can perform any initialization the bean
    needs,ie.,
  • public class MyBean implements SessionBean
  • private int memberVar
  • public void ejbCreate(int init val)
  • this.memberVar initval
  • .

20
A look at each method
  • ejbPassivate()
  • If too many beans are instantiated, the EJB
    container can passivate some of them(by writing
    the beans to a temp storage like a DB or File)
  • The container can then release the Resources the
    beans had claimed
  • Immediately before the Beans are passivated the
    container calls the ejbPassivate() method
  • NOTE Remember that passivation doesnt apply to
    stateless bean as it cannot hold the state.

21
A look at each method
  • ejbActivate()
  • When a client needs to use the passivated bean
    then the container kicks the bean back into
    memory. This is activation.
  • Once the bean is back in the memory again , the
    beans implementation acquires any resource the
    bean needs.
  • NOTE Activation doesnt apply to stateless
    session beans as it cannot hold state and can
    simply be created/destroyed rather than
    passivated/activated

22
Guess who spoke this ?
  • "You've heard Al Gore say he invented the
    internet. Well, if he was so smart, why do all
    the addresses begin with "W"?
  • Any body ?

23
A look at each method
  • ejbRemove()
  • When the container is about to remove your
    session bean instance it calls the beans
    ejbRemove() method
  • ejbRemove is a clean-up method, alerting the bean
    that it is about to destroyed.
  • ejbRemove() is a required method of all beans and
    takes no parametrs.
  • There is only one ejbRemove() method per bean.

24
A look at each method
  • Business Methods
  • There can be zero or more Business methods in
    your bean
  • These methods actually solve business problems
  • For eg
  • public class MyBean implements SessionBean
  • public int add(int I,int j)
  • return(I j)
  • For clients to call your business methods, the
    business methods must be listed in the beans
    remote interface

25
Major parts of an EJB Application
  • Session Bean Class
  • Home Interface
  • Remote Interface
  • Deployment Descriptor

26
Two types of State
  • Transactional state
  • Roughly speaking this is the data stored in the
    persistent store
  • Multiple client can read and modify the data
    without conflict
  • If the App Server crashes or re-started the data
    is still available in the data store
  • Example An order that a customer has placed

27
Two types of State
  • Conversational State
  • Cached data either on the Client or Server
  • Private data that isnt accessible to other
    clients
  • Example The web shopping cart before the order
    has been confirmed
  • REMEMBER A stateless session bean holds
    conversations that span a single method call.
  • So it doesnt hold Conversational state, BUT
  • stateful session bean holds Conversational state

28
Types of Session Beans
  • Stateless Session Beans
  • Stateful Session Beans
  • (Well see about this later)

29
Stateless Session Beans(SLSB)
  • A stateless session bean does not maintain a
    conversational state for a particular client
  • When a client invokes the method of a stateless
    bean, the bean's instance variables may contain a
    state, but only for the duration of the
    invocation
  • The state is not customized for each client
  • The SLSB cannot retain state between method
    calls,but they also cannot retain state after a
    client passes data to an ejbCreate() call
  • So all SLSB may expose only a single ejbCreate()
  • method,which takes no parameters

30
Stateless Session Bean Pooling
  • Stateless session bean pool

Client
Invoke()
Remote Interface
bean
EJB Obj
bean
bean
31
Hello world SLSB
  • Function
  • The SLSB, a component running in a Distributed
    Environment will do the mighty task of returning
    the string Hello World to the client.

32
Hello world Remote Interface
  • The remote interface is what the clients operate
    on when they interact with EJB objects. The
    container vendor will implement this interface
    the implemented object is the EJB object,which
    delegates invocations to the actual bean
  • import javax.ejb.
  • import java.rmi.RemoteException
  • Import java.rmi.remote
  • Public interface Hello extends EJBObject
  • public String hello() throws
    java.rmi.RemoteException

33
Implementing the Hello world bean
  • / Stateless Session bean /
  • import javax.ejb.
  • public class HelloBean implements SessionBean
  • / EJB required methods /
  • public void ejbCreate( ) System.out.println(C
    reate Method)
  • public void ejbremove( ) System.out.println(
    Remove Method)
  • public void setSessionContext(SessionContext
    ctx)
  • System.out.println(Set Session Context)
  • // Business Methods
  • public String hello() System.out.println(Hell
    o())
  • return Hello World

34
Hello World Home Interface
  • The Home Interface for HelloBean is implemented
    by the EJB Servers glue-code tools- the
    implemented object is called the Home Object and
    serves as the factory for EJB Objects
  • import javax.ejb.
  • import java.rmi.RemoteException
  • / One create() method is in this Home INTERFACE
    /
  • public interface HelloHome extends EJBHome
  • / This method creates the EJB Object return
    the newly created EJB object/
  • Hello create() throws RemoteException,
    CreateException

35
Deployment Descriptor
36
EJB-JAR File
  • Last step before deploying
  • Package all the files above in an EJB-Jar file
  • Jar files are compact modules in which to wrap
    our beans
  • The files to be included are
  • Enterprise Bean
  • Remote Interface
  • Home interface
  • Deployment Descriptor

37
Client pseudo-code for Stateless Bean
  • The HelloClient class invokes methods on a simple
    stateless beans
  • Get system properties for JNDI initialization
  • Form an initial context
  • Get a reference for the home object(factory for
    EJB Objects)
  • Use the factory to create EJB Object
  • Call the Hello() method and print the string
  • Since the EJB object is done with, remove it /
    hello.remove /
  • Check for any Exception

38
Lifecycle of a SLSB
Because a stateless session bean is never
passivated, its life cycle has just two stages
non-existent and ready for the invocation of
business methods.
39
Stateful Session Beans
  • Stateful Session Beans are conversational beans
  • Because they hold conversation with clients that
    span multiple method invocations
  • Stateful Session Bean store conversatinal state
    within the bean
  • The conversational state is specific to a
    particular client

40
Passivation of Stateful bean
Client
Remote Interface
bean
EJB Obj
bean
bean
Passivated Bean stored
41
Stateful Session bean Lifecycle
Does not exist
1.create
1.remove
2. setSessionContext
2. ejbRemobe
3 ejbCreate
ejbPassivate
Ready
Passive
ejbActivate
42
CartEJB.java
import java.util.
import javax.ejb.
 
public class CartEJB implements SessionBean
 
String customerName
String customerId
Vector contents
 
public void ejbCreate(String person) throws
CreateException
 
if (person null)
throw new CreateException("Null person
not allowed.")

else
customerName person

 
customerId "0"
contents new Vector()

43
public void ejbCreate(String person, String id)
throws CreateException
 
if (person null)
throw new CreateException("Null person
not allowed.")

else
customerName person

 
IdVerifier idChecker new IdVerifier()
if (idChecker.validate(id))
customerId id

else
throw new CreateException("Invalid id "
id)

 
contents new Vector()

44
public void addBook(String title)
 
contents.addElement(title)

public void removeBook(String title) throws
BookException
 
boolean result contents.removeElement(titl
e)
if (result false)
throw new BookException(title " not in
cart.")


public Vector getContents()
return contents

public CartEJB()
public void ejbRemove()
public void ejbActivate()
public void ejbPassivate()
public void setSessionContext(SessionContext
sc)
 

45
The SessionBean Interface The SessionBean
interface extends the EnterpriseBean interface,
which in turn extends the Serializable
interface. The SessionBean interface declares
the ejbRemove, ejbActivate, ejbPassivate, and
setSessionContext methods
46
The ejbCreate Methods Because an enterprise
bean runs inside an EJB container, a client
cannot directly instantiate the bean. Only the
EJB container can instantiate an enterprise
bean. During instantiation, the example program
performs these steps 1. The client invokes a
create method on the home object
Cart shoppingCart home.create(Sethu","123")
2. The EJB container instantiates the enterprise
bean. 3. The EJB container invokes the
appropriate ejbCreate method in CartEJB
47
public void ejbCreate(String person, String id)
throws CreateException
 
if (person null)
throw new CreateException("Null person
not allowed.")

else
customerName person

 
IdVerifier idChecker new IdVerifier()
if (idChecker.validate(id))
customerId id

else
throw new CreateException("Invalid id "
id)

 
contents new Vector()

48
Business Methods The primary purpose of a session
bean is to run business tasks for the client. The
client invokes business methods on the remote
object reference that is returned by the create
method. From the client's perspective, the
business methods appear to run locally, but they
actually run remotely in the session bean. The
following code snippet shows how the CartClient
program invokes the business methods
Cart shoppingCart home.create("Duke DeEarl",
"123")
. . .
shoppingCart.addBook("The Martian Chronicles")
shoppingCart.removeBook("Alice In Wonderland")
bookList shoppingCart.getContents()
49
The CartEJB class implements the business methods
in the following code
public void addBook(String title)
contents.addElement(new String(title))

public void removeBook(String title) throws
BookException
boolean result contents.removeElement(title)
if (result false)
throw new BookException(title " not in
cart.")


public Vector getContents()
return contents  
50
Home Interface A home interface extends the
EJBHome interface. The purpose of the home
interface is to define the create methods that a
client may invoke. The CartClient program, for
example, invokes this create method
Cart shoppingCart home.create("Duke DeEarl",
"123") Every create method in the home
interface corresponds to an ejbCreate method in
the bean class. The signatures of the ejbCreate
methods in the CartEJB class follow
public void ejbCreate(String person) throws
CreateException
. . .
public void ejbCreate(String person, String id)
throws CreateException
51
create methods in the CartHome interface
import java.io.Serializable
import java.rmi.RemoteException
import javax.ejb.CreateException
import javax.ejb.EJBHome
 
public interface CartHome extends EJBHome
Cart create(String person) throws
RemoteException,

CreateException
Cart create(String person, String id) throws
RemoteException,

CreateException

52
Remote Interface The remote interface, which
extends javax.ejb.EJBObject, defines the business
methods that a client may invoke. Here is the
source code for the Cart remote interface
import java.util.
import javax.ejb.EJBObject
import java.rmi.RemoteException
 
public interface Cart extends EJBObject

public void addBook(String title) throws
RemoteException
public void removeBook(String title) throws
BookException,

RemoteException
public Vector getContents() throws
RemoteException

53
You should consider using a stateful session bean
if any of the following conditions are true
  • The bean's state must be initialized when it is
    created.
  • The bean needs to hold information about the
    client across method invocations.
  • The client is an interactive application.
  • Since the primary goal of a session bean is to
    represent a client in the J2EE server, most of
    your session beans will be stateful. However,
    sometimes you may want to use stateless session
    beans
  • The bean performs a task that is not tailored to
    the needs of a particular client. For example,
    you might use a stateless session bean to fetch
    from a database a commonly used set of data.
  • The bean doesn't need to hold information about
    the client across method invocation

54
FIRE .
55
Am Still Alive !!!!
56
References
  • On Starters
  • www.calvinandhobbes.com
  • More on HIS Quotes
  • www.bushisms.com
  • More on EJB
  • ????????????
  • Ask Google or Dr. juggy ?
  • Actually gt 1) Text Mastering EJB by Ed roman
  • 2) www.javaworld.com
  • 3) http//java.sun.com/products/e
    jb/
  • 4) http//theserverside.com/home/index.j
    sp
Write a Comment
User Comments (0)
About PowerShow.com