This class implements the Bean's business methods of the component interface and the methods of the SessionBean interface, which are those dedicated to the EJB environment. The class must be defined as public and may not be abstract. The Session Bean interface methods that the EJB provider must develop are the following:
public void setSessionContext(SessionContext ic);
This method is used by the container to pass a reference to the SessionContext to the bean instance. The container invokes this method on an instance after the instance has been created. Generally, this method stores this reference in an instance variable.
public void ejbRemove();
This method is invoked by the container when the instance is in the process of being removed by the container. Since most session Beans do not have any resource state to clean up, the implementation of this method is typically left empty.
public void ejbPassivate();
This method is invoked by the container when it wants to passivate the instance. After this method completes, the instance must be in a state that allows the container to use the Java Serialization protocol to externalize and store the instance's state.
public void ejbActivate();
This method is invoked by the container when the instance has just been reactivated. The instance should acquire any resource that it has released earlier in the ejbPassivate() method.
A stateful session Bean with container-managed transaction demarcation can optionally implement the javax.ejb.SessionSynchronization interface. This interface can provide the Bean with transaction-synchronization notifications. The Session Synchronization interface methods that the EJB provider must develop are the following:
public void afterBegin();
This method notifies a session Bean instance that a new transaction has started. At this point the instance is already in the transaction and can do any work it requires within the scope of the transaction.
public void afterCompletion(boolean committed);
This method notifies a session Bean instance that a transaction commit protocol has completed and tells the instance whether the transaction has been committed or rolled back.
public void beforeCompletion();
This method notifies a session Bean instance that a transaction is about to be committed.
package sb; import java.rmi.RemoteException; import javax.ejb.EJBException; import javax.ejb.EJBObject; import javax.ejb.SessionBean; import javax.ejb.SessionContext; import javax.ejb.SessionSynchronization; import javax.naming.InitialContext; import javax.naming.NamingException; // This is an example of Session Bean, stateful, and synchronized. public class OpBean implements SessionBean, SessionSynchronization { protected int total = 0; // actual state of the bean protected int newtotal = 0; // value inside Tx, not yet committed. protected String clientUser = null; protected SessionContext sessionContext = null; public void ejbCreate(String user) { total = 0; newtotal = total; clientUser = user; } public void ejbActivate() { // Nothing to do for this simple example } public void ejbPassivate() { // Nothing to do for this simple example } public void ejbRemove() { // Nothing to do for this simple example } public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; } public void afterBegin() { newtotal = total; } public void beforeCompletion() { // We can access the bean environment everywhere in the bean, // for example here! try { InitialContext ictx = new InitialContext(); String value = (String) ictx.lookup("java:comp/env/prop1"); // value should be the one defined in ejb-jar.xml } catch (NamingException e) { throw new EJBException(e); } } public void afterCompletion(boolean committed) { if (committed) { total = newtotal; } else { newtotal = total; } } public void buy(int s) { newtotal = newtotal + s; return; } public int read() { return newtotal; } } |