唉,打雷下雨收衣服落~~
有状态session bean, 曾经有菜鸟告诉过我们它们是一对一的关系
非常直接, 只有一点需要注意,标注了@Stateful的Bean不要忘了系列化
另外一点是在JSP中使用时,记得把lookup得到的SFSB实例放在session中,下次用这个实例的时候从session取,而不是重新lookup一次。
Calculator cal
=
(Calculator) session.getAttribute(
"
sfsb_cal
"
);
if
(cal
==
null
) {
try
{
InitialContext ctx
=
new
InitialContext();
cal
=
(Calculator) ctx.lookup(
"
EJB3Trail/StatefulCalculator/local
"
);
session.setAttribute (
"
sfsb_cal
"
, cal);
}
catch
(Exception e) {
e.printStackTrace ();
}
}
//
Make use of the cal object
下一讲很重要:Session Bean 的生命周期
第四天结束了,外面哗啦啦下起雨来,下次见吧。
当你需要在对象 创建时初始化某些变量
或者在对象销毁时释放某些资源的时候,
你可以对这个POJO的进行标注,然后服务就回自动回调 这些经过 特殊 标注的方法。
你不用实现那些ejbCreate()...等等无聊的哪怕仅仅是空的方法。再也不用了。没有人强迫你。只需要在你想要用的方法上标注,仅此而已
废话少说,有下面这几个标注需要记得
@PostConstruct
@PreDestroy
@PrePassivate
@PostActivate这四个我一下就记得了,明显和EJB2中的那几个方法一一对应~
@Init,这个要单独说一下:
This annotation designates initialization methods for a stateful session bean. It is different from the @PostConstruct annotation in that there can be multiple methods tagged with @Init in a stateful session bean. However, each bean instance can only have one @Init method invoked. The EJB 3.0 container determines which @Init method to invoke depending on how the bean is created (see the EJB 3.0 specification for details). The @PostConstruct method is called after the @Init method.
说完了~
哈哈。看懂了吗?
说的是@Init是SFSB专用的,而且可以有好几个,但是容器最终要调哪个我们也不知道,要想知道的话就去看EJB Spec.
我们只能告诉你这个方法在@PostConstruct之后被调用。
晕了~不知道便罢,还偏要摆出一幅很酷的样子~ ~
算了,不去惹他,让我们看最后一个叫@Remove的标签(之前已经见识过了),
我们用@Remove告诉服务,灭掉该实例吧,我们已经用完不再需要了!
这个和前面几个都不一样哦,前面几个是-“实例”在过期或无效时服务自动回调的~,这个是我们主动请求地。
@Stateful
public
class
SessionCalculator
implements
Calculator {
//
@Remove
public
void
stopSession () {
//
Call to this method signals the container
//
to remove this bean instance and terminates
//
the session.
}
//
}
The following code shows how the @Remove method is called in the JSP page. Note that we also empty the HttpSession cache to remove the invalid stub.
//
"cal" is the stub of the stateful session bean.
//
It is cached in the HttpSession's "lifecycle_cal" attribute
if
(
"
Logout
"
.equals(request.getParameter(
"
action
"
))) {
cal.stopSession ();
session.setAttribute (
"
lifecycle_cal
"
,
null
);
//
}
SFSB在被@Remove后,然后重新lookup,就会建新的实例。这时@PostConstruct会被调用
如果觉得这些方法很烦人,可以把它们放在一边,然后用@CallbackListener标注就行了
Separate life cycle methods into another class
If all those callback methods seem to clutter up your session bean class, you can also separate out the callback methods into a separate callback listener class. You need to annotate the session bean class with the @CallbackListener tag and specify the listener class name in the annotation parameter.
@Stateful
@CallbackListener(CalculatorCallbackListener.
class
)
public
class
SessionCalculator
implements
Calculator {
//
}
The listener class contains all the annotated callback methods for this bean. Each callback method now takes the bean instance as input parameter. The container pasess the bean instance that causes the callback event to the callback method at runtime.
public
class
CalculatorCallbackListener {
@PostConstruct
public
initialize (CalculatorBean cal) {
//
}
@PreDestroy
public
exit (CalculatorBean cal) {
//
}
}
第五天完了,要睡觉了,下次见吧。