title: Easy way to access to a remote ejb located in another server
body: <p>This is my first question in Stack Overflow so I expect it will not be too much simple. I've been looking around Internet for a good solution but by now I don't have it.</p>
  
  <p>I am a very begginer to EJB, JNDI and Java EE world in general, but on the last months I've been able to do some acceptable things in this environment. Now I am focusing a problem at work and by now the solution is not as good as I would like.</p>
  
  <p>The scenario is this: I have a EAR application running in Glassfih 3.1.2. I have declared EJBs within this EAR app with stateless beans offering methods through a remote Interface.</p>
  
  <p>This is my Remote Bean running in a server called server1, for example</p>
  
  <pre><code>package com.booreg;
  
  import javax.ejb.LocalBean;
  import javax.ejb.Stateless;
  
  import com.booreg.IMyRemoteBean;
  
  @Stateless
  @LocalBean
  public class MyRemoteBean implements IMyRemoteBean
  {
      @Override
      public String helloWorld()
      {
          return "Hi what's up boy";
      }
  }
  </code></pre>
  
  <p>This is the interface for it</p>
  
  <pre><code>package com.booreg;
  
  import javax.ejb.Remote;
  
  @Remote
  public interface IMyRemoteBean
  {
      public String helloWorld();
  }
  </code></pre>
  
  <p>Then I have a second EAR app that <strong>must run imperatively on another server</strong>, called server2. The second APP is using JSF and Managed Beans. We have a Managed Bean acting as a remote client of MyRemoteBeanRemote, as this:</p>
  
  <pre><code>package com.nucleus;
  
  import javax.ejb.EJB;
  import javax.faces.bean.ManagedBean;
  import javax.faces.bean.ViewScoped;
  
  import com.booreg.IMyRemoteBean;
  
  @ManagedBean
  @ViewScoped
  public class MyManagedBean
  {
      @EJB( name="TheRef") IMyRemoteBean myRemoteBean;
  
      public String getPhrase() { return myRemoteBean.helloWorld(); }
  }
  </code></pre>
  
  <p>I've arrived to the point that this works declaring an ejb-ref inside WEB-INF/sun-web.xml file in my web project. </p>
  
  <pre><code>&lt;ejb-ref&gt;
      &lt;ejb-ref-name&gt;TheRef&lt;/ejb-ref-name&gt;
      &lt;jndi-name&gt;corbaname:iiop:server1:3700#java:global/booreg/booreg.ejb/MyRemoteBean!com.booreg.IMyRemoteBean&lt;/jndi-name&gt;
  &lt;/ejb-ref&gt;
  </code></pre>
  
  <p>I understand that with this sun-web.xml file the jndi-name makes the second app to know where to locate the ejb implementation located at first app. But here I have some questions:</p>
  
  <ol>
  <li>It's necessary to declare one ejb-ref entry for each EJB interface I have in my project ?</li>
  <li>How can I avoid making a static reference to server/port (server1:3700 during development) inside sun-web.xml ? When this will go into production I will have to change manually the sever name for each  ?? This sounds bizarre. Can I use some kind of variable at server side to specify server/port ?</li>
  </ol>
  
  <p>I expect I have explained myself well enough.</p>
  
  <p>Many thanks</p>
  
  <p><strong>Update:</strong> finally, thanks to this <a href="https://stackoverflow.com/questions/3614802/how-can-i-deploy-session-bean-on-another-computer-with-client-jsp-servlet">link</a> I see that is possible to make references to the ejb server (server1) creating a jndi.properties file inside my classpath. This file should contain lines like this.</p>
  
  <pre><code>java.naming.factory.initial=com.sun.enterprise.naming.SerialInitContextFactory
  java.naming.factory.url.pkgs=com.sun.enterprise.naming
  java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl
  org.omg.CORBA.ORBInitialHost=server1
  org.omg.CORBA.ORBInitialPort=3700
  </code></pre>
  
  <p>But I am still facing problems. When deploying the application appear the next exception at server1 an I can't deploy the application.</p>
  
  <pre><code>ADVERTENCIA: IOP00100006: Class com.sun.jersey.server.impl.cdi.CDIExtension is not Serializable
  org.omg.CORBA.BAD_PARAM: ADVERTENCIA: IOP00100006: Class com.sun.jersey.server.impl.cdi.CDIExtension is not Serializable  vmcid: SUN  minor code: 6 completed: Maybe
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
      at com.sun.corba.ee.spi.orbutil.logex.corba.CorbaExtension.makeException(CorbaExtension.java:248)
      at com.sun.corba.ee.spi.orbutil.logex.corba.CorbaExtension.makeException(CorbaExtension.java:95)
      at com.sun.corba.ee.spi.orbutil.logex.WrapperGenerator.handleFullLogging(WrapperGenerator.java:387)
      at com.sun.corba.ee.spi.orbutil.logex.WrapperGenerator.access$400(WrapperGenerator.java:107)
      at com.sun.corba.ee.spi.orbutil.logex.WrapperGenerator$2.invoke(WrapperGenerator.java:511)
      at com.sun.corba.ee.spi.orbutil.proxy.CompositeInvocationHandlerImpl.invoke(CompositeInvocationHandlerImpl.java:99)
      at $Proxy117.notSerializable(Unknown Source)
      at com.sun.corba.ee.impl.orbutil.ORBUtility.throwNotSerializableForCorba(ORBUtility.java:783)
      at com.sun.corba.ee.impl.encoding.CDROutputStream_1_0.write_abstract_interface(CDROutputStream_1_0.java:697)
      at com.sun.corba.ee.impl.encoding.CDROutputObject.write_abstract_interface(CDROutputObject.java:545)
      at com.sun.corba.ee.impl.javax.rmi.CORBA.Util.writeAbstractObject(Util.java:493)
      ...
  </code></pre>
  
  <p>Anybody has any idea ?</p>