<b>循速渐进学用Session Bean(三)</b>[Java编程]
本文“<b>循速渐进学用Session Bean(三)</b>[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
成立无状况的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+"!";
}
}
以上是“<b>循速渐进学用Session Bean(三)</b>[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |