简体   繁体   中英

How to write a generic remote ejb lookup?

I have the following util class for looking up Remote EJB. It's currently duplicated in many places. How can I write it once and reuse and only pass the interface name to tell it which EJB I'm looking for.

I have limited knowledge of Java Generics.

@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();
    }
}

Here is the answer (change APP_NAME , MODULE_NAME and BEAN_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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM