Search code examples
ejbwebspherewebsphere-8

lookup to EJBHome from Outside the container via Standalone applicatiion failed


I have an EJB application which is Deployed in WebSphere 8

I am trying to connect it from Standalone java program following way

public static void main (String[] args) throws Exception
{
    Properties props = new Properties();
    props.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
    props.put(javax.naming.Context.PROVIDER_URL, "iiop//localhost:2809");
    Context ctx = new InitialContext(props);
    Object o = ctx.lookup("MyAppHome");

}

But this is leading to below exception

javax.naming.NameNotFoundException: Name "MyAppHome" not found in context 
"serverlocal:CELLROOT/SERVERROOT".
at com.ibm.ws.naming.ipbase.NameSpace.lookupInternal(NameSpace.java:1228)

Further looking into it I found following supporting link https://www.ibm.com/developerworks/community/forums/html/threadTopic?id=ac01caaf-d2aa-4f3f-93b3-6f3d4dec3e6b

Here the answerer suggested using fully qualified bean name

java:global/ProjectName/ModuleName/BeanName!FullyQualif‌​iedNameOfRemoteInterface or BeanName#FullyQualif‌​iedNameOfRemoteInterface.

If it's a correct fix. Where can I find the Project name, Module Name and Bean Name? I do have xmi files in the project. Thanks!


Solution

  • The easiest way to determine the JNDI name for an EJB in WebSphere is to look for the binding information in the server logs. The JNDI names associated with each EJB interface are logged using the message ID CNTR0167I. Fore example:

    CNTR0167I: The server is binding the com.example.DatabaseBean interface of the DatabaseBean enterprise bean in the TestProject.war module of the TestProject application. The binding location is: java:global/TestProject/DatabaseBean!com.example.DatabaseBean
    

    Notice in this example that there does not appear to be both a project name (i.e. application name) and module name, because this test project was a standalone WAR module with an EJB packaged in it. Project/Application name is only used for EAR files.

    Also note that a CNTR0167I is logged for JNDI names in both the java:global and SERVERROOT name contexts. So, in the error scenario above, it could be that the EJB was bound to ejb/MyAppHome and not just MyAppHome, so you can also determine the correct SERVERROOT JNDI name from the CNTR0167I messages as well. Either JNDI name may be used to lookup an EJB.