2011-12-01 30 views
1

我想從獨立的Java客戶機調用EJB,得到下面的錯誤。EJB3客戶機查找

查找代碼

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 com.ibm.ws .naming.jndicos.CNContextImpl.checkForUrlContext(CNContextImpl.java:471) at com.ibm.ws.naming.util.WsnInitCtx.lo okup(WsnInitCtx.java:160)在 com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:179)在 javax.naming.InitialContext.lookup(未知來源)在 com.test.EJBClientTest 。主要(EJBClientTest.java:18)

環境

RAD 7.5,EJB3。 Websphere Application Server 7.0。

回答

2

ejbremote方案在WebSphere Application Server中不存在(即使「ejblocal」確實存在)。嘗試使用ejb/前綴而不是ejbremote:

有關詳細信息,請參閱信息中心中的EJB application bindings overview主題。

+0

它的工作就像一個魔法。你可以給一些參考讀物的一些指示,以瞭解更多關於此? – Rafi

+0

我已添加信息中心鏈接,但如果您有其他問題,請告知我。 –

0

您需要有存根文件來調用EJB,因此首先生成存根文件。在websphere中,在appserver bin文件夾createEJBStubs中有可用的實用程序。

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

你也應該在你的classpath適當的WebSphere瓶,以作出上述呼籲。

+0

我有使用其他名稱,如java:global,ejblocal,那些沒有工作,並給出一些奇怪的錯誤。這對我有用.initCtx.lookup(「com.smbcgroup.gcms.utility.bl.survey.CustomerSurveyManageQstRemote」); //默認情況下,您的遠程接口的JNDI名稱 – zaga250

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

}