问题
I am trying to invoke a EJB from a standalone Java client, getting the below error.
Lookup code
String localJNDIName = "ejbremote:gcmsnew/gcmsutilbeans.jar/CustomerSurveyManageQstBean#com.smbcgroup.gcms.utility.bl.survey.CustomerSurveyManageQstRemote";
InitialContext ic = new InitialContext();
GCMSBaseRemote bean = (GCMSBaseRemote)ic.lookup(localJNDIName);
Exception
javax.naming.ConfigurationException: NamingManager.getURLContext cannot find the factory for this scheme: ejbremote at com.ibm.ws.naming.jndicos.CNContextImpl.checkForUrlContext(CNContextImpl.java:471) at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:160) at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:179) at javax.naming.InitialContext.lookup(Unknown Source) at com.test.EJBClientTest.main(EJBClientTest.java:18)
Environment
RAD 7.5, EJB3. Websphere Application Server 7.0.
回答1:
The ejbremote scheme does not exist in WebSphere Application Server (even though "ejblocal" does exist). Try using a ejb/ prefix instead of ejbremote:.
For more information, see the EJB application bindings overview topic in the InfoCenter.
回答2:
Since this is a standalone ("thin") client may be you should try something like this:
Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
properties.setProperty(Context.PROVIDER_URL,"corbaloc:iiop:localhost:2809"); //localhost=the host where your EJB is located, 2809=BOOTSTRAP_ADDRESS port
Context initCtx = new InitialContext(properties);
Object homeObject = initCtx.lookup("com.smbcgroup.gcms.utility.bl.survey.CustomerSurveyManageQstRemote"); //by default the JNDI name of your Remote Interface is its full class name
// Narrow to a real object
csmo = (CustomerSurveyManageQstRemote) javax.rmi.PortableRemoteObject.narrow(homeObject, com.smbcgroup.gcms.utility.bl.survey.CustomerSurveyManageQstRemote.class);
You should also have the appropriate Websphere Jars in your classpath in order to make the above calls.
回答3:
For remote lookup
import java.io.IOException;
import java.util.Hashtable;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class ServiceLocator {
static String url = "corbaloc:iiop:localhost:2809";
static String initial = "com.ibm.websphere.naming.WsnInitialContextFactory";
static String jndi = "ejb/enterprise_app_name/ejb_web_project_name.jar/ejb_name#name.of.remote.impl.interface";
private static ServiceLocator serviceLocator = null;
InitialContext context = null;
private ServiceLocator() throws NamingException, IOException {
Hashtable<String,String> env = new Hashtable<String,String> ();
env.put("java.naming.provider.url", url );
env.put("java.naming.factory.initial", initial );
context = new InitialContext(env);
}
public synchronized static ServiceLocator getInstance() throws NamingException, IOException {
if (serviceLocator == null) {
serviceLocator = new ServiceLocator();
}
return serviceLocator;
}
public Object getService(String jndiName) throws NamingException {
return context.lookup(jndiName);
}
public <T>T getRemoteObject(Class<T> remoteInterfaceClass) {
try {
return (T)javax.rmi.PortableRemoteObject.narrow( context.lookup(jndi), remoteInterfaceClass);
} catch (NamingException nexc) {
nexc.printStackTrace();
}
return null;
}
}
For local lookup
import java.io.IOException;
import java.util.Hashtable;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class ServiceLocator {
static String url = "iiop://localhost";
static String initial = "com.ibm.websphere.naming.WsnInitialContextFactory";
static String jndi = "ejblocal:enterprise_app_name/ejb_web_project_name.jar/ejb_name#name.of.local.impl.interface";
private static ServiceLocator serviceLocator = null;
InitialContext context = null;
private ServiceLocator() throws NamingException, IOException {
Hashtable<String,String> env = new Hashtable<String,String> ();
env.put("java.naming.provider.url", url );
env.put("java.naming.factory.initial", initial );
context = new InitialContext(env);
}
public synchronized static ServiceLocator getInstance() throws NamingException, IOException {
if (serviceLocator == null) {
serviceLocator = new ServiceLocator();
}
return serviceLocator;
}
public Object getService(String jndiName) throws NamingException {
return context.lookup(jndiName);
}
public Object getService() throws NamingException {
return context.lookup(jndi);
}
}
回答4:
You need to have the stub file to invoke the EJB, so first generate the stub file. In websphere there is utility available in the appserver bin folder createEJBStubs.
来源:https://stackoverflow.com/questions/8342102/ejb3-client-lookup