How can I access an EJB in WebSphere on one machine from a Java program on another?

How can I access an EJB in WebSphere on one machine from a Java program on another?

How can I access an EJB which exists in WebSphere (WAS Server) on one machine from a Java program on another machine in the same network?

    Requires Free Membership to View

    When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to keep you informed on recent service-oriented architecture (SOA) and SOA-related topics such as integration, governance, Web services, Cloud and more.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchSOA.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchSOA.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

You are describing the exact problem EJB was originally intended to solve. Up to and including EJB 2.x, with an EJB successfully deployed to the WAS, you simply perform a JNDI lookup of the EJB from a remote client and then make calls to the EJB as if it were running in the same VM. This requires you to know the EJB vendor's lookup URL, the JNDI name of the EJB, security credentials, etc. With EJB 3.0, all of the look-up information and functionality will be separated from the client code.

The following demonstrates a typical scenario for finding an EJB and calling a method on it:


java.util.Properties env = new java.util.Properties();

   env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,

           "com.anejbvendor.InitCtxFactory");

   env.put(javax.naming.Context.PROVIDER_URL,

           "ejb.server.jndi.url");

   java.security.Identity id = getIdentity();

   env.put(Context.SECURITY_PRINCIPAL, id);

   javax.naming.Context initial = new javax.naming.InitialContext(env);

   Object objref =

      (javax.naming.Context)initial.lookup("java:comp/env/ejb/MyEBJ");

   MyEJBHome home =

      (MyEJBHome)javax.rmi.PortableRemoteObject.narrow(objref,

                                                       MyEJBHome.class);

   MyEJB myEJB = home.create();

   myEJB.sayHello();

This was first published in June 2005