Writing a Session Bean
Session Beans: Overview
- Session
- A client’s conversation with a server
- Client has server carry out business operations
- Session Bean
- A bean that represents a client’s session with server
- Encapsulates a business process
- Usually transient
- Client can be end user, another bean or anything else
- Examples: stock broker, bank teller, shopping cart
- Session beans can be stateless or stateful
- A given design may use either or both
- Container handles them differently
- Details follow...
Stateless Session Beans...
- Stateless session beans hold no session data
- Hence usually have no instance variables
- For example, a stock broker:
- Acts as gateway to your portfolio
- Does not hold your money; puts it into an account
- A broker bean would be stateless
- Portfolio and account beans would have state
...Stateless Session Beans
- Stateless session beans are shared
- Container creates a pool of identical instances
- Any instance can serve any method call from any client
- Idle instances remain in pool
- When necessary, container can expand or shrink pool
Stateful Session Beans...
- Stateful session beans hold session data:
- They create & maintain data as session proceeds
- Data is held in instance variables
- For example, a customer's shopping cart:
- Cart grows during a session
- When customer "leaves the store", session ends
...Stateful Session Beans
- Stateful session beans are not shared:
- One instance is allocated per client session
- Instance is released when session ends
- Each client gets its own
- Idle instances can be swapped out of / into memory
Session Beans & Persistence
- State and persistence are related but different:
- Stateless session beans are always transient
- Stateful session beans can be persistent if desired
- Persistent state usually relates to entity beans
- Why make a stateful session bean persistent?
- To let client suspend and resume a session
- E.g., to hold a shopping cart pending customer's return
- If a stateful bean's state must persist:
- Handle persistence explicitly (More on this later...)
- Or use an entity bean instead
Session Bean Life Cycle...
- Every session bean has a home object
- A "factory" for that bean
- EJB client uses bean's home to create bean instances
- All clients share one home object for given bean type.
- Creating a session:
- Establishes client’s connection to a session
- Creates a skeleton
- To create a session, a client:
- Gets a reference to home object using JNDI
- Uses home object's methods to connect to a session bean
...Session Bean Life Cycle
- Removing a session:
- Severs client's connection to the bean
- Destroys skeleton
- Client retains stub but can not use it
- Client must create a new session again through home object
- To do this, client uses a method of remote reference
- Stateless and stateful beans are handled differently
- But they look identical to the client
- Details follow...
Stateless Session Beans
- Clients share a pool of
identical instances
- "Creating" simply
connects to a
skeleton
- "Removing" simply
disconnects from
skeleton
- State diagram for a
stateless session bean
Stateful Session Beans
- Each client gets its own dedicated instance
- "Creating" creates an instance
- "Removing" destroys it, losing state
- State diagram for a stateful session bean
Bean Use Examples
- Two examples illustrate use of session beans:
- Code snippets from client applications
- The StockBroker example:
- Stateless bean is shared by all
- create( ) connects client to session
- Removal severs connection
- The ShoppingCart example:
- Session is created using Customer ID
- Bean accumulates state
- Removal kills session, loses state
- See notes for code...
Continuation of Notes
- This is a full page of notes.
- The slide is hidden.
Writing a Session Bean...
- There are 3 parts of a session bean you must write:
- The remote interface:
- Java interface describing bean's remote functions
- Like CORBA IDL or RMI remote interface
- The home interface:
- Describes the bean factory
- The bean class:
- Provides code for remote and home interfaces
- A note on class names:
- Conventional names: Xyz, XyzHome, XyzBean
- Handy, but not required
- See diagram on next page; details follow...
...Writing a Session Bean
The Remote Interface
- Defines the bean's remote "business methods"
- Must be a public interface
- Must extend javax.ejb.EJBObject
- EJBObject defines all EJB remote interfaces
- EJBObject extends java.rmi.Remote
- Must meet RMI remote interface requirements:
- Methods are public & throw java.rmi.RemoteException
- Arguments & return values are valid remote types:
- Primitives, Serializable classes or Remote classes
- See examples in notes...
- A stateless bean: StockBroker for an online brokerage
- A stateful bean: ShoppingCart for an online bookstore
Continuation of Notes...
- This is a full page of notes.
- The slide is hidden.
...Continuation of Notes
- This is a full page of notes.
- The slide is hidden.
EJBObject
- All remote interfaces extend javax.ejb.EJBObject
- Defines all stubs and skeletons
- This interface defines methods to:
- Remove the bean (discussed here)
- Do various other things (discussed later...)
- Removing the bean:
- Client calls this method
to sever connection
- RemoveException thrown if removal fails
- Checked exception; must be trapped
- Container may trigger the failure
- java.rmi.RemoteException thrown if system failure occurs
- More on exceptions later...
Using the Remote Interface
- Client uses a bean through the remote interface:
- This applies to remote as well as local clients
- Client code looks the same
- See notes for examples...
- A client getting stock information.
- A client buying books
Continuation of Notes
- This is a full page of notes.
- The slide is hidden.
The Home Interface
- The home interface defines a bean's home object
- Defines a "factory" for that bean
- Home interface requirements:
- Interface is public
- Extends javax.ejb.EJBHome
- Methods meet RMI remote requirements
- Has at least one create( ) method
- See examples in notes...
- A StockBrokerHome interface
- A ShoppingCartHome interface
- Details follow...
Continuation of Notes
- This is a full page of notes.
- The slide is hidden.
create(*)...
- “Creating” a session bean:
- Represents creating a client session
- Triggered by calling Home#create( … )
- create( ) is a factory method
- Like a constructor, defines how to initialize a session
- Returns the remote bean type
- May be overloaded
- Syntax:
...create(*)
- Home must have at least one create( ) method:
- Otherwise client cannot create a session
- What create( ) methods should a home have?
- Arguments are used to initialize session state
- For stateless beans: no-args version only
- For stateful beans: any versions warranted by the design
- The CreateException:
- javax.ejb.CreateException
- A checked exception
- Thrown in server code if bean instance can not be created
- Comes back to client as failure of Home#create( * )
Continuation of Notes
- This is a full page of notes.
- The slide is hidden.
The Session Bean Class
- The "bean class" implements the session bean
- Provides functionality defined in remote interface
- Responds to create( ) methods in home interface
- This is the only class you must write for a session bean
- Requirements for a session bean class:
- Class is public
- Has a public default constructor
- Has all methods of remote interface (“business methods”)
- Has an ejbCreate( ) for each create( ) of home interface
- Implements javax.ejb.SessionBean interface
- Details follow...
Business Methods...
- Bean class must provide business methods
- Methods defined in remote interface
- But bean class does not implement this interface!
- Container associates these methods with skeleton
- Hence Java compiler will not detect errors/omissions
- These will be flagged by server at deployment time
- Business methods & exceptions:
- Remote interface methods must throw RemoteException
- Bean class methods need not
- They need only throw application-specific exceptions
- And then, only when actually thrown in method body
- More on exceptions later...
...Business Methods
- Q: Why doesn’t bean class implement remote interface?
- A: It would create an invalid “is a” relationship.
- Remote interface defines an EJBObject
- Bean class is not the EJBObject, just a servant
- A: EJBObject defines 5 methods.
- Your bean class would have to implement them
- Only the stub & skeleton implement remote interface
- These are generated by server’s deployment tool
ejbCreate( )
- The bean class will have ejbCreate( ) methods:
- One to match each create( ) in home interface
- Called by container when it creates bean instance
- What do these methods do?
- Generally used to initialize state data
- No-arguments version may be empty
- To abort session creation:
- Throw javax.ejb.CreateException from ejbCreate()
- Checked exception; must be declared if used!
- Client sees failure of Home#create( )
- See example in notes...
Continuation of Notes
- This is a full page of notes.
- The slide is hidden.
The SessionBean Interface
- Bean class must implement SessionBean interface
- javax.ejb.SessionBean
- Extends javax.ejb.EnterpriseBean (a "marker" interface)
- SessionBean interface has life cycle methods:
- "Event methods", called by container during bean's life cycle
- Bean can respond or do nothing
- See notes for complete listing…
- We will cover SessionBean in detail later...
- For now, we will leave the methods empty
Continuation of Notes
Bean Class Examples
- Putting it together, here are 2 complete bean classes:
- A StockBrokerBean class:
- Implements methods of StockBroker interface
- Has single ejbCreate( ) for StockBrokerHome interface
- A ShoppingCartBean class:
- Implements methods of ShoppingCart interface
- Has two ejbCreate( ) for ShoppingCartHome interface
- In both examples:
- Methods of SessionBean are empty
- See code in notes...
Continuation of Notes...
- This is a full page of notes.
- The slide is hidden.
...Continuation of Notes...
- This is a full page of notes.
- The slide is hidden.
...Continuation of Notes...
- This is a full page of notes.
- The slide is hidden.
...Continuation of Notes
- This is a full page of notes.
- The slide is hidden.
Writing a Bean: Recap
- The remote interface defines bean's remote methods
- Extends EJBObject
- Its methods must meet requirements of RMI methods
- The home interface defines home object:
- Extends EJBHome
- Will have one or more create( ) methods
- The bean class provides implementation of both
- Implements remote interface methods
- Has an ejbCreate( ) to match each home#create( )
- Must implement SessionBean interface
Where We've Been
- Session beans:
- Represent a business process
- Are usually transient
- Stateless session beans:
- Stateful session beans:
- Dedicated to client, not recycled
- The parts of a bean:
- Remote interface
- Home interface
- Bean class
Session Bean Workshop
...Continuation of Notes
- This is a full page of notes.
- The slide is hidden.