创建无状态的Session Bean
从编程的角度看,创建无状态的Session Bean和创建有状态的是一样简单的。除了在配置工具里修改一个设置外,仅有的一点不同是在bean的初始设计阶段,无状态的Session Bean并不记得方法调用之间的任何东西,bean需要的任何消息都必须由客户端获得。虽然无状态的Session Bean并不记得面向session的数据,不过可以在一个无状态的session bean中存放数据,只是不能存放与客户端相关的数据。
在HelloWorldSession的例子中,该bean在方法调用之间仍记得一个问候的字符串。例如,你调用setGreeting来修改欢迎词,当你调用getGreeting时,该session会记得保存的欢迎词。
列表6.5“Hello World”session bean Remote 接口(无状态版本)
Listing 6.5 Source Code for StatelessHello.java
package usingj2ee.hello;
import java.rmi.*;
import javax.ejb.*;
/** Defines the methods you can call on a StatelessHello object */
public interface StatelessHello extends EJBObject
{
/** Returns a greeting for the named object */
public String greet(String thingToGreet) throws RemoteException;
}
在这个例子中,Remote接口仅提供了一个greet方法,该方法接收一个参数并且返回一个欢迎词。例如,如果传送“World”参数给greet,greet方法将返回“Hello World!”。
列表6.6展示了StatelessHello bean的Home接口。
Listing 6.6 Source Code for StatelessHelloHome.java
package usingj2ee.hello;
import java.rmi.*;
import javax.ejb.*;
/** Defines the methods for creating a StatelessHelloWorld */
public interface StatelessHelloHome extends EJBHome
{
/** Creates a StatelessHello session bean. A stateless session bean
can't have a create method that takes parameters. */
public StatelessHello create() throws RemoteException, CreateException;
}
无状态的session bean仅拥有一个create方法,而且该方法不能接受任何参数。这看起来有些奇怪,不过如果考虑到无状态session bean的含义你就会明白了。这种bean不能记住某个客户的任何信息,实际上,为了性能上的原因,容器也许会不时地让不同的session处理某个客户的方法调用。由于session并不需要记住某个客户的信息,因此使用另一个bean来处理负载并不会带来任何问题。
如果bean的create方法接受任何的参数,session bean实例之间的行为将会有所不同,因为你为create方法提供不同的值。
实现无状态session bean与有状态的session bean是一样简单的。列表7中的是StatelessHelloImpl类,它实现了Remote和Home接口。
Listing 6.7 Source Code for StatelessHelloImpl.java
package usingj2ee.hello;
import java.rmi.*;
import java.util.*;
import javax.ejb.*;
/** The implementation class for the StatelessHello bean */
public class StatelessHelloImpl implements SessionBean
{
/** The session context provided by the EJB container. A session bean must
hold on to the context it is given. */
private SessionContext context;
/** An EJB must have a public, parameterless constructor */
public StatelessHelloImpl()
{
}
/** Called by the EJB container to set this session's context */
public void setSessionContext(SessionContext aContext)
{
context = aContext;
}
/** Called by the EJB container when a client calls the create() method in
the Home interface */
public void ejbCreate()
throws CreateException
{
}
/** Called by the EJB container to wake this session bean up after it
has been put to sleep with the ejbPassivate method. */
public void ejbActivate()
{
}
/** Called by the EJB container to tell this session bean that it is being
suspended from use (it's being put to sleep). */
public void ejbPassivate()
{
}
/** Called by the EJB container to tell this session bean that it has been
removed, either because the client invoked the remove() method or the
container has timed the session out. */
public void ejbRemove()
{
}
/** Returns a greeting for the named object */
public String greet(String thingToGreet)
{
return "Hello "+thingToGreet+"!";
}
}