SCBCD Notes: Stateless Session Bean
Published November 25th, 2006 in Java.
Stateless session beans are lightweight and fast and require no passivation and activation. Therefore, they are particularly suitable for any activity that can be accomplished in one method call, such as report generation, credit card validation and batch processing.
Stateless session beans maintain no state from one method invocation to the next. They have longer timeout period because they are not dedicated to one client. A timeout or removal operation invalidates the EJB object reference but may not destroy the bean instance. A client can also explicitly remove a stateless session bean by invoking a business interface annotated as @javax.ejb.Remove.
Stateless session beans have only two states - Does not Exist and Method-Ready Pool. The actions on the transition of both states include
Class.newInstance()- injections
- (callback)
@javax.annotation.PostConstructor<post-construct>inejb-jar.xml - (callback)
@javax.annotation.PreDestroyor<pre-destroy>inejb-jar.xml
Stateless session beans can use instance variables for internal state. However, the state is meaningless for a client because different instances of stateless session bean do not share the same state. The client cannot determine which instance will provide the service on the next method invocation.
A stateless session bean can have remote, local or web service business interface. Invoking a remote interface (@javax.ejb.Remote) uses call-by-value semantics while a local interface (@javax.ejb.Local) uses call-by-reference semantics.
The EJB container treats any exception that does not extend java.lang.RuntimeException as an application exception. Application exception can be declared explicitly using annotation @javax.ejb.ApplicationException and <application-exception> XML deployment descriptor meta data. Non-application exceptions are always wrapped in a javax.ejb.EJBException and ready for try-catch block or further propagation.
Stateless bean has the ability to access environment properties via injection. For example, @javax.annotation.Resource(name="x") int y; tells the EJB container to initialize the variable (”y”) with value (identified by “x”) in the Enterprise Naming Context (ENC), the container’s internal registry that stores configuration data.
The javax.ejb.SessionContext provides a view into the EJB container’s environment. The following are some of the useful methods:
getBusinessObject()- return a reference to the current EJB, equivalent to this pointer.getInvokedBusinessInterface()- determine which interface (remote, local or endpoint) the EJB was invoked.lookup()- look up entries in the container’s ENC.getTimerService()- return a reference to the container’sTimerservice.getCallerPrincipal()- return ajava.security.Principalrepresenting the client that is accessing the EJB.isCallerInRole()- determine whether the client that is accessing the EJB is of a specific role.


2 Responses to “SCBCD Notes: Stateless Session Bean”
Please Wait
Leave a Reply