EJB3 Client Lookup
Asked Answered
L

4

1

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.

Lewert answered 1/12, 2011 at 13:24 Comment(0)
K
3

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.

Kannry answered 1/12, 2011 at 14:58 Comment(4)
It worked like a magic. Can you give some pointers to some reference reading to understand more on this?Lewert
I've added an InfoCenter link, but let me know if you have other questions.Kannry
@BrettKail the link seems to be broken.Senarmontite
It appears IBM broke the link. I am no longer familiar enough with the product to find a replacement. Sorry.Kannry
S
1

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.

Standby answered 22/11, 2012 at 9:10 Comment(1)
I have using other names like, java:global, ejblocal, those didnt work and giving some weird errors. This worked for me .initCtx.lookup("com.smbcgroup.gcms.utility.bl.survey.CustomerSurveyManageQstRemote"); //by default the JNDI name of your Remote InterfaceJanettajanette
N
1

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

}
Novokuznetsk answered 5/6, 2013 at 9:28 Comment(0)
L
0

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.

Lewert answered 2/12, 2011 at 10:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.