Title: Session Beans
1Session Beans
- Dont Represent the Database Tables
- Used to Manage Workflow
- Session Beans Model Actions in a Use Case
- Can be Stateless or Stateful
2Stateless
- Collection of Related Services
- Dont Maintain State Between Method Calls
- Represent General Purpose Services
3Stateful
- Extension to the Client Application
- Saves State Between Client and Bean
- They are not Persistent
- Are Timedout After Inactivity
4Resource Management
- Stateless - Lightweight and Shared
- Bean Instances Rarely Destroyed
- Statefull - Less Overhead than Entity Bean
- Communicate With One Client Only During Life Cycle
5Stateless Lifecycle
6Stateful Life Cycle
7Implementation
- Define Remote and Home Interfaces
- Define Bean Class Itself
- Dont Define a Primary Key Class (for entity
beans only) - Create Deployment Descriptor
- Create EJB .jar file
8Remote Interface Example
- import java.rmi.RemoteException
- import java.rmi.Remote
- import javax.ejb.
- public interface Demo extends EJBObject, Remote
-
- // NB this simple example does not even do a
- // lookup in the database
- public String demoSelect() throws
RemoteException -
9Home Interface Example
- import javax.ejb.
- import java.rmi.Remote
- import java.rmi.RemoteException
- import java.util.
- /
- This interface is extremely simple it
declares only - one create method.
- /
- public interface DemoHome extends EJBHome
- public Demo create() throws CreateException,
RemoteException
10Session Bean Class
- /
- DemoBean -- This is implemented by the
EnterPrise - Bean author This class must extend
- javax.ejb.SessionBean and implement
- the methods in this interface as well as
providing - the implementation of the business methods.
-
- /
-
- import javax.ejb.
- import java.io.Serializable
- import java.util.
- import java.rmi.
-
- public class DemoBean implements SessionBean
- static final boolean verbose true
-
- private transient SessionContext ctx
- private transient Properties props
11 // Implement the methods in the SessionBean //
interface public void ejbActivate() if
(verbose) System.out.println("ejbActivate
called") public void ejbPassivate()
if (verbose) System.out.println("ejbPa
ssivate called") / Sets the
session context. _at_param
SessionContext / public void
setSessionContext(SessionContext ctx)
if (verbose) System.out.println("setSessionC
ontext called") this.ctx ctx props
ctx.getEnvironment()
12 / This method corresponds to the create
method in the home interface
DemoHome.java. The parameter sets of the
two methods are identical. When the
client calls DemoHome.create(), the
container allocates an instance of the
EJBean and calls ejbCreate(). / public
void ejbCreate () if (verbose)
System.out.println("ejbCreate called")
public void ejbRemove() if (verbose)
System.out.println("ejbRemove called")
13 / HERE IS THE BUSINESS LOGIC
Do the demoSelect() but don't even
go to the database in this eg but instead
just return a String. The really
BIG thing to notice here is that this is
the only code we have invented at all the
rest of the code has been declarations or
simply implementing methods which are part
of the EJB interfaces and in this example
are not even used. / public String
demoSelect() throws RemoteException
return("hello world") //end of class
14Deployment Descriptor
- (SessionDescriptor
- This file must start with SessionDescriptor or
- EntityDescriptor
-
- Indicate the name which the bean will be bound
- into the JNDI name as
- beanHomeName demo.DemoHome
- The enterprise Java Bean class
- enterpriseBeanClassName ejb.demo.DemoBean
-
15homeInterfaceClassName
ejb.demo.DemoHome The home interface
implemented by a class generated by the
container provided tools remoteInterfaceClassNa
me ejb.demo.Demo isReentrant
false Always false for session beans
stateManagementType
STATELESS_SESSION Either STATELESS_SESSION or
STATEFUL_SESSION. DemoBean is a stateless
session bean sessionTimeout 5
seconds
16(controlDescriptors This section decides the
run-time properties when a method is called.
The DEFAULT sub-section applies to all
methods, but can be overridden on a per-method
basis, similar to the "accessControlEntries"
above. (DEFAULT isolationLevel
TRANSACTION_SERIALIZABLE transactionAttribute
TX_REQUIRED runAsMode
CLIENT_IDENTITY ) end isolationLevel )
end controlDescriptors (environmentProperties
maxBeansInFreePool 100 ) end
environmentProperties ) end SessionDescriptor