運用反射實現ejb動態委派

來源:互聯網
上載者:User

每個bean可能會有很多方法,一般我們通過一個delegate來調用sessionbean中的方法,而非直接調用sessionbean,delegate中只是簡單的對每個相對應的sessionbean的public方法的簡單封裝,在調用的時候省去了每次對home的尋找和ejb對象的create,但是可能我們的bean會有很多方法,如果每個bean都寫這樣一個delegate,這樣工作量就會很大,而且也不便於以後系統的移植,比如說,原來使用ejb實現,現在要改用jdo直接操作資料庫,而通過運用java的reflect技術,就能較好地實現這些要求。首先,定義了一個FacadeDelegate的抽象類別,用來實現對sessionbean的home的尋找,代碼如下:

import javax.ejb.*;
   import testejb.util.common.*;
   import testejb.util.resource.*;
   public abstract class FacadeDelegate{
    private static String type = Resource.RemoteType;
    public FacadeDelegate() {
    }
    public EJBHome getHome(String jindiName,Class className)
    {
     EJBHome home = null;
     ServerLocatorAdapter adapter = ServerLocatorAdapter.getInstance();
     try
     {
      home = (EJBHome)adapter.getHome(type, jindiName, className);
     }
     catch(Exception e)
     {
      System.err.println(e.getMessage() + jindiName + className.toString());
     }
     return home;
    }
   }

其中ServerLocatorAdapter是一個用來根據是local還是remote調用ejb對象而通過不同的方法尋找home的類,如果type為local則調用LocalServerLocate中的方法,如果type為remote則調用RemoteServerLocate中的方法,獲得home。代碼如下:

import java.util.*;
   import java.lang.reflect.*;
   import testejb.util.resource.*;
   public class ServerLocatorAdapter {
    private Map cache;//用來緩衝home
    private static ServerLocatorAdapter me;
    public static ServerLocatorAdapter getInstance()
    {
     if(me == null)
      me = new ServerLocatorAdapter();
     return me;
    }
    //取得home
   public Object getHome(String type,String jndiHomeName,Class className) throws Exception
    {
     Object home = null;
     if(cache.containsKey(jndiHomeName))
      return cache.get(jndiHomeName);
     if(Resource.LocalType.equals(type))
     {
      home = getLocalHome(jndiHomeName,className);
      cache.put(jndiHomeName,home);
      return home;
     }
     if(Resource.RemoteType.equals(type))
     {
      home = getRemoteHome(jndiHomeName,className);
      cache.put(jndiHomeName,home);
      return home;
     }
     return home;
    }
    //取得local home
    private Object getLocalHome(String jndiHomeName,Class className) throws Exception
    {
   Class myClass = Class.forName(Resource.LocalClass);
   // Resource. LocalClass =”testejb.util.common. LocalServerLocator
   Method method = myClass.getMethod(Resource.LocalConstractMethod,null);
   // Resource. LocalConstractMethod =” getInstance”
     LocalServerLocator local = null;
     local = (LocalServerLocator)method.invoke(myClass,null);
     return local.getLocalHome(jndiHomeName,className);
   }
   //取得remote home
    private Object getRemoteHome(String jndiHomeName,Class className) throws Exception
    {
   Class myClass = Class.forName(Resource.RemoteClass);
   // Resource.RemoteClass =”testejb.util.common.RemoteServerLocator”
   Method method = myClass.getMethod(Resource.RemoteConstractMethod,null);
   // Resource.RemoteConstractMethod=” getInstance”
     RemoteServerLocator remote = null;
     remote = (RemoteServerLocator)method.invoke(myClass,null);
     return remote.getHome(jndiHomeName,className);
    }
    private ServerLocatorAdapter() {
     // 為cache提供安全執行緒的保證
     cache = Collections.synchronizedMap(new HashMap());
    }
   }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.