唉,打雷下雨收衣服落~~
有狀態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)?{
????????
//
?
?
????}
}
第五天完了,要睡覺了,下次見吧。