@Interceptor 铪铪铪铪铪铪铪

news/2024/6/17 6:01:33

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



 

    ...


 

    

       com.softleader.interceptors.MyCallbackHandler

      

         someInteceptorMethod

        

    

 

 

     

         *

         com.softleader.interceptors.MyCallbackHandler

       

     

         SlessEJB

         true

     

     

         SlessEJB2

         true

     

 


非常明顯的, 我們可以看到, 我們需要利用 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 實作了




http://www.niftyadmin.cn/n/3653819.html

相关文章

@Interceptors and inv.proceed()铪铪

寫了一個小程式測試了一下, Interceptors 他的流程StatelessInterceptors({AuditInterceptor.class, SecurityInterceptor.class})public class AuditInterceptor {AroundInvoke public Object auditOperation(InvocationContext inv) throws Exception {try {System.out.print…

PLSQL 经常自动断开失去连接的解决过程

问题背景:情况是这样的,很多开发同事的plsql上班时间开着8个小时,有时候他们出去抽烟后或者中午吃完饭,回来在plsql上面执行就报错无响应,然后卡住了半天动弹不了,非得重新登录plsql才生效,我猜…

JasperForge 正式開張

搞 Java Web solution 的, 在需要用到 Report Engine 的時候, 通常會先想.. 我能不能使用 jasperReport 作為我的 ReportEngine, 利用 iReport 當作我的 template Report Designer. 很高興的, JasperSoft 將這些都整合起來放到了 JasperForge, 更提出了一個新的 BI 專案, Jaspe…

zabbix3 0 从坚持到用起 开始配置管理界面

--> 背景,源码安装完成zabbix-server3.0已经安装完成,接下来需要进行基础配置,zabbix-server安装参考:http://blog.csdn.net/mchdba/article/details/51263871,接下来可以开始配置zabbix管理界面。1,打开…

is DTO an antipattern in EJB 3.0 ?

這個問題應該是從 Raghu Kodalis blog 開始的http://www.jroller.com/page/raghukodali?entrydto_an_antipattern_in_ejbRaghu 的論述主要在於, Design Pattern 中的 Data Transfer Objects (DTO) 原本是要避免重新取得舊有的 EJB2.x 的遠端呼叫時間, 讓 DTO 封裝資料後送到客…

ORA-12537 TNS connection closed错误处理过程

1,同事说oracle测试换了连接不上了,报错如下[oraclepldb236 admin]$ rlwrap sqlplus powerdesk/pd141118PD236SQL*Plus: Release 11.2.0.1.0 Production on Mon Nov 23 14:16:31 2015Copyright (c) 1982, 2009, Oracle. All rights reserved.ERROR:ORA-…

JSF1.2 in JavaEE5 

Chapter 1 JSF 1.2 overviewJSF ( Java Server Faces ) 在 JavaEE5 之中, 是採用 1.2 的版本. 目前的 RI 僅有 SUN 釋出的版本, 並且可以使用在 NetBeans 5.5 與 Glassfish 之中. 主要來說, 簡化了相關的開發方式, 以及 Scoped Managed Bean 可以利用 Annotation 的方式快速存取…

BPM introduction

以往, 大家對於流程控管, 似乎都是稱之為 Workflow, 然而, 專門控制流程的會稱之為 FlowEngine. 所以想要了解接下來我所引導介紹的章節, 大家可以先行閱讀 TSS 的這篇文章. BPEL and Java .在 Java Opensource 的流程控管系統中, 當中有幾個重要的 flow engine .jBPM ( http:/…