Amazon Ads

2014年1月24日 星期五

【筆記】EJB使用Deployment Description來實現Interceptor

在Session Bean或Message Driven Bean的某些方法被呼叫時,可以使用Interceptor在呼叫前後,做一些其他的處理,他像是EJB的AOP (Aspect Oriented Programming) 。

Interceptor可以使用Annotation或使用Deployment Descriptor來宣告,下面的範例包含了一個Singleton Session Bean、一個要做為Interceptor的類別、一個宣告Interceptor的Deployment Descriptor文件,另外還有一個做為客戶端程式的Servlet,我使用GlassFish 3.1來做為我的伺服器,並使用Deployment Descriptor來宣告Interceptor。

SingletonSessionBean.java
package jk.idv.ssb;

import java.util.Date;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.LocalBean;
import javax.ejb.Singleton;

@Singleton(name="SingletonSessionBean")
@LocalBean
public class SingletonSessionBean
{
 private final static String MY_NAME = "SingletonSessionBean";
 
 private String username = "[no man]";
 
 @PostConstruct
 private void init()
 {
  System.out.println("    " + MY_NAME + " - Initialized");
 }
 
 @PreDestroy
 private void destroy()
 {
  System.out.println("*** " + MY_NAME + " - Destroyed");
 }
 
 public String retrieveMessage()
 {
  Date theCurrentTime = new Date();
  return "Name saved in " + MY_NAME + " is " + username + " "
  + theCurrentTime;
 }
 
 public void saveName(final String inName)
 {
  this.username = inName;
 }
}


上列程式碼中,未使用@Interceptors來宣告這個Singleton Session Bean相關的Interceptor。

LogInterceptor.java
package idv.jk.interceptor;

package idv.jk.interceptor;

import javax.interceptor.InvocationContext;

public class LogInterceptor
{
 public Object logMethod(InvocationContext inCtx) throws Exception
 {
  System.out.println(" LogInterceptor intercepting: " +
    inCtx.getTarget().getClass().getSimpleName() +
    "." + inCtx.getMethod().getName());
  return inCtx.proceed();
 }
}


上列的程式碼中,也沒有使用@Interceptor來宣告此類別為一個Interceptor。
SingletonClientServlet.java
package idv.jk.client;

import java.io.IOException;
import java.io.PrintWriter;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jk.idv.ssb.SingletonSessionBean;

/**
 * Servlet implementation class SingletonClientServlet
 */
@WebServlet("/SingletonClientServlet")
public class SingletonClientServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
       
 @EJB
 private SingletonSessionBean singletonSessionBean;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SingletonClientServlet() {
        super();
    }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  String theParam = request.getParameter("name");
  
  PrintWriter pw = response.getWriter();
  
  String msg = singletonSessionBean.retrieveMessage();
  pw.println("Get message from singleton session bean: ");
  pw.println(msg);
  
  if(theParam != null)
  {
   singletonSessionBean.saveName(theParam);
  }
  msg = singletonSessionBean.retrieveMessage();
  pw.println("Check message from singleton session bean: ");
  pw.println(msg);
  pw.println("Finished invoking singleton session beans!");
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 }

}

上列的Servlet使用注入的方式來使用Singleton Session Bean的方法,並在瀏覽器中印出一些我需要的資訊。

最後是名為ejb-jar.xml的Deployment Descriptor:

 
 
  
   idv.jk.interceptor.LogInterceptor
   
    logMethod
   
  
 
 
  
   SingletonSessionBean
   idv.jk.interceptor.LogInterceptor
  
 


將Web應用程式部署到GlassFish後,再打開瀏覽器輸入:http://localhost:8888/BusinessMethodInterceptorsDD/SingletonClientServlet?name=Javakid,應該可以看到下列的結果:

Get message from singleton session bean: 
Name saved in SingletonSessionBean is [no man] Fri Jan 24 00:05:32 CST 2014
Check message from singleton session bean: 
Name saved in SingletonSessionBean is Javakid Fri Jan 24 00:05:32 CST 2014
Finished invoking singleton session beans!

在Console中,也會看到一些訊息被印出來:


完整的程式由此下載

參考資料:

http://www.slideshare.net/krizsan/ocp-jbcd-6-study-notes,p.97