西元2006年07月16日

Interceptor 與 inv.proceed() 的關係

寫了一個小程式測試了一下, @Interceptors 他的操作方式

@Stateless
@Interceptors({AuditInterceptor.class, SecurityInterceptor.class})




public class AuditInterceptor {
@AroundInvoke public Object auditOperation(InvocationContext inv)
throws Exception {
try {
System.out.println("audit "+inv.getMethod().getName());
Object result = inv.proceed();
System.out.println("test after proceed");
return result;
} catch ( Exception ex ) {
throw ex;
} finally {
System.out.println("finally end message");
}

}
}




import javax.interceptor.*;
public class SecurityInterceptor {
@AroundInvoke public Object securitySome(InvocationContext inv)
throws Exception {
try {
System.out.println("security"+inv.getMethod().getName());
Object result = inv.proceed();
System.out.println("test after proceed in security");
return result;
} catch ( Exception ex ) {
throw ex;
} finally {
System.out.println("Final in security");
}

}

}




執行的結果是


audit getHello|#]

securitygetHello|#]

Hello in getHello()|#]

test after proceed in security|#]

Final in security|#]

test after proceed|#]

finally end message|#]

換句話說, 當我們透過 InvocationContext proceed 之後,
會將 process 指到下一個 Interceptor
當沒有下一個 Interceptor 的時候

就會到了 Bean Method() 的操作

等到 Bean Method() 結束之後
才會開始處理 proceed 之後的功能
一直等到你執行 reutnr Object ( inv.proceed() ) .

雖然 return 該 Object ( inv.proceed() ) 之後,
我們也可以利用 finally 來處理最後的相關事務

11:25 發表於 EJB3 | 永久網址 | 留言 (0) | Email this

西元2006年07月10日

Interceptor 攔截 basic concept



EJB3 為了可以支援 AOP ( swanky : AOP Presentation - http://www.ice.ntnu.edu.tw/~swanky/presentation/PL_AOP.pdf )
所以建立了Interceptors 的觀念.

不過, 雖然是放在 ejb3 api 的製作之中, 但是他的 package 為 javax.interceptor.*, 換句話說, 未來有機會可能會移出 ejb-api .

其實, 熟稔 AOP 概念的人 ( AspectJ, AspectWerkz ) 通常會對於 EJB3 目前僅提供 Interceptor 實作的方法, 覺得比較陽春.
但是, 也可以這麼思考, 利用 Interceptor 應該就可以達到一般人的需求 !

在 Interceptor, 只有 InvocationContext Interface, 以及 四個 Annotation Types.
* AroundInvoke
* ExcludeClassInterceptors
* ExcludeDefaultInterceptors
* Interceptors

我們在呼叫任何一個 EJB Bean Class 的時候, 可以利用 @Interceptors 來設定

@Interceptors(MyCallbackHandler.class)
public class MyEJB {
....
}


或是利用 deployment descriptor ( ejb-jar.xml ) 來定義 interceptors


<ejb-jar>
<sessions>
...
</sessions>
<interceptors>
<interceptor>
<interceptor-class>com.softleader.interceptors.MyCallbackHandler</interceptor-class>
<pre-destroy>
<lifecycle-callback-method>someInteceptorMethod</lifecycle-callback-method>
</pre-destroy>
</interceptor>
</interceptors>
<assembly-descriptor>
<interceptor-binding>
<ejb-name>*</ejb-name>
<interceptor-class>com.softleader.interceptors.MyCallbackHandler</interceptor-class>
</interceptor-binding>
<interceptor-binding>
<ejb-name>SlessEJB</ejb-name>
<exclude-default-interceptors>true</exclude-default-interceptors>
</interceptor-binding>
<interceptor-binding>
<ejb-name>SlessEJB2</ejb-name>
<exclude-default-interceptors>true</exclude-default-interceptors>
</interceptor-binding>
</assembly-descriptor>
</ejb-jar>

非常明顯的, 我們可以看到, 我們需要利用 interceptor-binding 將 interceptor-class 指向到所有(*) 或單一(EJBName) 的結合.
以及是否去除 ( exclude-default-interceptors ) 預設的 binding 原則.


一個簡單的 Interceptor 的撰寫方式

import javax.interceptor.InvocationContext;
public class MyCallbackHandler {
public void someInteceptorMethod(InvocationContext invCtx) {
System.out.println("Hello Inteceptor in pre-destroy callback methods ");
try {
invCtx.proceed();
} catch(Exception e) {
e.printStackTrace();
}
}
}


當呼叫 InvocationContext.proceed() 之後, 將會進行下一個 interceptor 的作業.

所以我們必須利用 InvocationContext 來取得我們攔截下來取得相關的資訊, 進行相關的處理.

Map getContextData()
Method getMethod()
Object[] getParameters()
Object getTarget()
Object proceed()
void setParameters()

除了控制生命週期的攔截之外, 我們可以利用 ArroundInvoke() 進行相關的攔截

@AroundInvoke

Object aroundInvoke(InvocationContext ctx) throws Exception {

Common.aroundInvokeCalled(ctx, "testArroundInvoke");

Object instance = ctx.getTarget();

if( instance instanceof SomeEJB) {

((SomeEJB) instance).aroundInvokeCalled = true;

}

return ctx.proceed();

}

這樣就可以建立基本的 AOP 實作了

07:00 發表於 EJB3 | 永久網址 | 留言 (0) | Email this

西元2006年02月19日

use action/servlet to call EJB3

現在我們撰寫 WebApplication, 現在應該都是採用 MVC 的架構,
就算不是利用 Opensource 如 Struts, Webwork, Tapestry 等等,
也可能是利用 JSF, 甚至自己撰寫的 Servlet 作為 Model2 的 Controller.

這在學習 ejb3 有一個好處, 因為目前無法在 jsp 撰寫 @annotations,
我們可以透過 controller 的 servlet 呼叫 action 或任何 java class,
如此一來, 在 java source 之中,
只需要撰寫


package com.softleader.web.servlet;

import javax.ejb.EJB;

public class HelloServlet {

@EJB
private MySessionLocal mysession;

public String doGet( xxxxx ) {

String name = request.getParameter("name");

return mysession.getHello(name);
}

}



透過 @EJB 的 annotation, 我們可以不用在使用複雜的

InitialContext ic = new InitialContext();
ic.lookup("ejb/MySession");

再去轉型, 容易發生導致 ClassCastException 的錯誤 :)

17:25 發表於 EJB3 | 永久網址 | 留言 (0) | Email this

西元2006年02月17日

Oreilly EJB3 book

Oreilly 預計要在 2006 年五月推出 EJB3 ( 其實也是他們 ejb, ejb1.1, ejb2, ejb2.1 後的第五個 ejb 版本 )

相關的說明如下
http://www.oreilly.com/catalog/entjbeans5/desc.html

雖然賣出了 15 萬本, 但是我覺得之前的版本讓初學 ejb 的人會有些茫然. 我不太喜歡他的編排模式 :)
Anyway.. 我希望做的是比較偏向實作面的作者. 讓每個人可以透過程式與架設系統中,
學習各式各樣的觀念. 畢竟 HeadFirst series 以詼諧的手法來描述專業的知識, 我也應該要為自己的風格創下一個定位點.

與其東湊西拼的一本書, 我目前能夠參考的僅有 Specification ( 還在 Final Draft ... ) 另外不斷實作相關的範例程式. 以及思考相關的解釋與修飾相關的文句. 最重要的是.. 我喜歡把事情做到最好 ~

06:49 發表於 EJB3 | 永久網址 | 留言 (0) | Email this

西元2006年02月15日

NetBeans J2EE 5 to Glassfish

昨天下載了 NetBeans J2EE 5 ( netbeans-javaee5-daily-bin-200602012300-1_Feb_2006_2300.zip )
發現他的核心已經改為 NetBeans IDE 5.5

就 EJB3.0 整合度與核心建立上面來看, 這個版本已經漸趨穩定. 不過有幾個問題, 我認為應該更像是 J2EE 5.0 的方式 !

1) Call EJB Reference

目前無法直接取得 EJB3 , 必須自行撰寫 @EJB SomeEJBBI someEJB3; 來取得相關的 EJB.

2) Datasource

直接產生的 codes 是 context.lookup . 我的期望是 @Resource

3) Mail

直接產生的 codes 也是 context.lookup . 我的期望還是 @Resource


總之~ NetBeans J2EE 5 如果是選擇 j2ee5 的 support . 我建議可以忽視 j2ee1.4 之前的 撰寫模式 .

02:34 發表於 EJB3 | 永久網址 | 留言 (0) | Email this

西元2006年02月14日

實戰 ejb3.0 的點滴

"實戰 ejb3.0" , 預計 2006/04/30 準備截稿, 目前的問題有幾個

1) 該使用哪一個 Application Server + IDE 做為範例

Eclipse + JBoss 4.0.3 和 NetBeans + Glassfish 都讓我感到頭痛,
基本上, JBoss4.0.3 沒有 Glassfish 這麼完整, 畢竟 Glassfish 應該是 J2EE 5.0 的 RI, 利用 Oracle EJB3.0 的實作,
加上 SUN 在後面不斷 support, 然而, NetBeans J2EE 5 連 Beta 版本都尚未 Release.

JBoss4.0.3 目前連 @Resource, @EJB 這些 IoC 的 特性都尚未支援, 
就算 Eclipse 是當今最紅的 IDE, 但是 ejb3 相關的整合性仍舊很低

我目前的想法, 無論是誰, 反正先放到 Appendix 都可以讓讀者讀到

2) 範例程式的內容

我利用 maven 做為範例 sample build 的方式,
基本上, 是因為 Maven 的 multiproject 可以讓 Enterprise Application 開發可以多元化 .

不過.. 到底要做出什麼呢
我目前已經推翻手機銷售網站的企劃, 決定推出類似商城的範例系統

13:00 發表於 EJB3 | 永久網址 | 留言 (0) | Email this