Q

How can I communicate to EJB in another server?

I want to communicate to an EJB in another server. Since this EJB to EJB call is between servers, it is showing an error at the time of look up. My server is JBoss. I am stuck with this problem -- any advice?
One difference between accessing EJBs from local clients vs. remote clients is in the construction of the InitialContext for the bean. Remote clients typically obtain an InitialContext via an InitialContext factory, with server-specific credentials and URL, as in the following example:
   Properties props = new Properties();
   props.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,
             "weblogic.jndi.WLInitialContextFactory");
   props.put(javax.naming.Context.PROVIDER_URL, "t3://myhost:7001");
   props.put(javax.naming.Context.SECURITY_PRINCIPAL, "jdoe");
   props.put(javax.naming.Context.SECURITY_CREDENTIALS, "foobar");
   javax.naming.Context context =
      new javax.naming.InitialContext(props);
   Object homeObject = context.lookup("java:comp/env/myejb");
  
   MyEJBHome home = null;

   if (usingIIOP == true)
   {
      home = (MyEJBHome)PortableRemoteObject.narrow(homeObject,
                                                    MyEJBHome.class);
   }
   else
   {
      home = (MyEJBHome)homeObject;
   }

   MyEJB myEJB = MyEJBHome.create();
Internal clients of an EJB, such as servlets, can simply create an InitialContext using the default constructor, as shown here:
   javax.naming.Context context =
      new javax.naming.InitialContext();
   Object homeObject = context.lookup("java:comp/env/myejb");
   MyEJBHome home = (MyEJBHome)homeObject;
   MyEJB myEJB = MyEJBHome.create();

This was first published in April 2004

Dig deeper on Java Web Services

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchSoftwareQuality

SearchCloudApplications

SearchAWS

TheServerSide

SearchWinDevelopment

Close