2016-07-05 14 views
1

我有以下用於查找遠程EJB的util類。目前它在許多地方被複制。我該如何編寫一次並重用,並且只傳遞接口名稱來告訴它我正在尋找哪個EJB。如何編寫一個通用的遠程ejb查找?

我對Java泛型的知識有限。

@ApplicationScoped 
public class ServiceLookupUtil { 

    public static final String JBOSS_EJB_CLIENT = "org.jboss.ejb.client.naming"; 
    public static final String JBOSS_NAMING_CONTEXT = "jboss.naming.client.ejb.context"; 

    private AddressService addressService; 
    private Context context; 
    private Logger log; 


    public ServiceLookupUtil() { 
     super(); 
    } 

    @Inject 
    public ServiceLookupUtil(Logger log) { 
     this.log = log; 
    } 

    public AddressService lookupAddressService() { 

     if (addressService == null) { 
      try { 

       context = getInitialContext(); 

       log.debug("Looking up: " + getLookupName()); 
       addressService = (AddressService) context.lookup(getLookupName()); 
       return addressService; 

      } catch (Exception ex) { 
       log.error("Could not get reference to AddressService ", ex); 
      } finally { 
       if (context != null) { 
        try { 
         context.close(); 
        } catch (NamingException e) { 
         log.error(e.getMessage(), e); 
        } 
       } 
      } 
     } 
     return addressService; 
    } 

    public Context getInitialContext() { 
     try { 
      final Hashtable jndiProperties = new Hashtable(); 
      jndiProperties.put(Context.URL_PKG_PREFIXES, JBOSS_EJB_CLIENT); 
      jndiProperties.put(JBOSS_NAMING_CONTEXT, true); 
      return new InitialContext(jndiProperties); 
     } catch (NamingException e) { 
      log.error(e.getMessage(), e); 
     } 
     return context; 
    } 

    private String getLookupName() { 
     return "ejb:" + AddressService.APP_NAME + "/" + AddressService.MODULE_NAME + "/" + AddressService.BEAN_NAME + "!" + AddressService.class.getName(); 
    } 
} 

回答

0

下面是答案(改變APP_NAMEMODULE_NAMEBEAN_NAME_CONVETION_ADD):

@ApplicationScoped 
public class ServiceLookupUtil { 

    public static final String JBOSS_EJB_CLIENT = "org.jboss.ejb.client.naming"; 
    public static final String JBOSS_NAMING_CONTEXT = "jboss.naming.client.ejb.context"; 

    private Map<String, Object> services = new HashMap<String, Object>(); 

    private Context context; 

    @Inject 
    private Logger log; 

    private static final String APP_NAME = ""; 
    private static final String MODULE_NAME = "module-ejb"; 
    private static final String BEAN_NAME_CONVETION_ADD = "Impl"; 


    public ServiceLookupUtil() { 
     super(); 
    } 

    @SuppressWarnings("unchecked") 
    public <S> S lookupService(Class<S> clazz) { 
     if (services.get(clazz.getName()) == null) { 
      S service = null; 
      try { 
       context = getInitialContext(); 
       String jndi = getLookupName(clazz); 
       log.debug("Looking up: " + jndi); 
       service = (S) context.lookup(jndi); 
       services.put(clazz.getName(), service); 
       return service; 

       } catch (Exception ex) { 
       log.error("Could not get reference to AddressService ", ex); 
       } finally { 
       if (context != null) { 
        try { 
         context.close(); 
         } catch (NamingException e) { 
         log.error(e.getMessage(), e); 
        } 
       } 
      } 
     } 
     return (S)services.get(clazz.getName()); 
    } 

    private Context getInitialContext() { 
     try { 
      final Hashtable jndiProperties = new Hashtable(); 
      jndiProperties.put(Context.URL_PKG_PREFIXES, JBOSS_EJB_CLIENT); 
      jndiProperties.put(JBOSS_NAMING_CONTEXT, true); 
      return new InitialContext(jndiProperties); 
      } catch (NamingException e) { 
      log.error(e.getMessage(), e); 
     } 
     return context; 
    } 

    private <S> String getLookupName(Class<S> clazz) { 
     return "ejb:" + APP_NAME + "/" + MODULE_NAME + "/" + clazz.getSimpleName() + BEAN_NAME_CONVETION_ADD + "!" + clazz.getName(); 
    } 
} 
+0

感謝,答案看起來不錯,只是幾個問題嗎?你爲什麼選擇包含一個使用地圖? 對於每個會話bean,AppName,ModuleName和BeanName是不同的,我已經在接口中將它們定義爲實例變量。我不確定如何在這裏定義它們會起作用。特別是AppName和ModuleName。 –

+0

該地圖用於保存遠程服務,以便您第二次打電話時可用。在應用程序中,「APP_NAME」,「MODULE_NAME」和「BEAN_NAME_CONVETION_ADD」常量在所有會話中都沒有區別,首先用'@ ApplicationScoped'註釋並且因爲它們是常量。請注意,BEAN_NAME_CONVETION_ADD是一個常量,用於將名稱添加到遠程服務實現中。 'ServiceLookupUtil'類僅用於項目的obetener遠程服務。 – Willy