我有以下用于查找Remote EJB的util类。它目前在很多地方都是重复的。我怎么能写一次并重用,只传递接口名称告诉它我正在寻找哪个EJB。
我对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();
}
}
答案 0 :(得分:0)
以下是答案(更改APP_NAME
,MODULE_NAME
和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();
}
}