EJB3客户端查找

时间:2011-12-01 13:24:24

标签: websphere ejb-3.0 jndi

我试图从独立的Java客户端调用EJB,得到以下错误。

查找代码

String localJNDIName = "ejbremote:gcmsnew/gcmsutilbeans.jar/CustomerSurveyManageQstBean#com.smbcgroup.gcms.utility.bl.survey.CustomerSurveyManageQstRemote";
InitialContext ic = new InitialContext();
GCMSBaseRemote bean = (GCMSBaseRemote)ic.lookup(localJNDIName);

异常

  

javax.naming.ConfigurationException:NamingManager.getURLContext   找不到这个方案的工厂:ejbremote at   com.ibm.ws.naming.jndicos.CNContextImpl.checkForUrlContext(CNContextImpl.java:471)     在com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:160)at   com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:179)at at   javax.naming.InitialContext.lookup(未知来源)at   com.test.EJBClientTest.main(EJBClientTest.java:18)

环境

RAD 7.5,EJB3。 Websphere Application Server 7.0。

4 个答案:

答案 0 :(得分:2)

WebSphere Application Server中不存在ejbremote方案(即使“ejblocal”确实存在)。尝试使用 ejb / 前缀而不是 ejbremote:

有关详细信息,请参阅信息中心中的EJB application bindings overview主题。

答案 1 :(得分:1)

由于这是一个独立的(“瘦”)客户端,你应该尝试这样的事情:

   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);

您还应该在类路径中拥有相应的Websphere Jars,以便进行上述调用。

答案 2 :(得分:1)

用于远程查找

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;
    }

}

用于本地查找

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

}

答案 3 :(得分:0)

您需要使用存根文件来调用EJB,因此首先生成存根文件。在websphere中,appserver bin文件夹createEJBStubs中提供了实用程序。