我在websphere中部署了一个ejb模块(EAR1),并希望从WAR访问该ejb,WAR也在同一websphere中部署。
我已经尝试使用以下代码,但不起作用。
public class RACAccessProvider {
private InitialContext myInitialContext;
public synchronized Object locateEJB(final Class clazz) throws ClassCastException, NamingException {
try {
System.out.println("looking up ejb.. for class " + clazz);
Object obj;
final String jndiName = clazz.getName();
obj = myInitialContext.lookup(jndiName);
System.out.println("###lookuop object.." + obj);
return obj;
} catch (final Exception e) {
e.printStackTrace();
}
return null;
}
public RACAccessProvider() {
System.out.println("Setting context in RACAccessProvider constructor...");
final Properties context = new Properties();
context.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
try {
myInitialContext = new InitialContext(context);
} catch (NamingException e) {
e.printStackTrace();
}
}
}
上面的代码挂在线上
obj = myInitialContext.lookup(jndiName);
非常感谢您的帮助。
you need to check if your jndi name is correctly mentioned,
Also if initial context is initialized properly as per below code:
InitialContext ic = new InitialContext();
String snName = "java:comp/env/mail/your_jndiname";
Session session = (javax.mail.Session)ic.lookup(snName);