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?
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();
Dig Deeper on Topics Archive
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.