Java Reference
In-Depth Information
which, for our example, we can implement as empty methods. A stateless ses-
sion bean never needs an activation or passivation method to do anything—it
is pointless to passivate a stateless session bean. Why? Since it's stateless, any
instance of it is as good as any other, so the instances are interchangeable and
there's no need to passivate one to get to another—just use the one available.
It follows that if a bean is never passivated, it will never have to be activated.
But why no body to the
ejbCreate()
method? Well, our bean isn't doing
anything extra. This would only be used if our example were more complicated
and we needed to do application-specific initializations. For example, if the
bean had to connect to a database (and did not use entity beans), it might es-
tablish the JDBC connection in
ejbCreate
and close it in
ejbRemove()
.
Similarly, we can have an empty
ejbRemove()
method.
Next we add our own methods, the ones that provide the application
functionality. For our MoneyBean application, we'll add
save()
and
debt()
methods which will use an
SAMoney
class by calling its
save()
and
debt()
methods. Example 22.1 is the listing of the
SessionBean
.
Example 22.1
Listing of our implementation of a
SessionBean
package com.jadol.budgetpro;
import net.multitool.util.*;
import javax.ejb.*;
/**
* Actual implementation of the EJB
*/
public class
MoneyEJBean
implements SessionBean
{
protected SessionContext sessionContext;
// typical; just not used now
public Cost
save(double amt, double rate, double paymnt)
throws java.rmi.RemoteException
{
return SAMoney.save(amt, rate, paymnt);
} // save