title: GlassFish running EJBs on different physical servers
body: <p>I have a Java EE 6 web application running on <code>GlassFish</code> 3.1. The application uses 2 instances of a local <code>Singleton EJB</code> MySingleton. Each instance of MySingleton connects to a third party software through an API.</p>
  
  <p><strong>MySingleton.java</strong></p>
  
  <pre><code>@Singleton
  @LocalBean
  public class MySingleton { 
      @PersistenceContext private EntityManager em;    
      private ThirdPartyAPI thirdPartyAPI;
      ...
  }
  </code></pre>
  
  <p><strong>MySingletonManager.java</strong></p>
  
  <pre><code>@Singleton
  @LocalBean
  public class MySingletonManager { 
      @EJB private MySingleton mySingletonA;
      @EJB private MySingleton mySingletonB;        ///Aargh! They can't run on the same server!!
      ...
  }
  </code></pre>
  
  <p>Here are the constraints:</p>
  
  <ol>
  <li>the third party software provider requires that its software runs each instance of <code>ThirdPartyAPI</code> on a different physical server</li>
  <li>MySingleton uses an injected instance of <code>EntityManager</code>, needed for queries and transactions on the same <code>PersistenceContext</code>.</li>
  </ol>
  
  <p>In order to satisfy constraint 1, I think that I need to access the Singleton EJB remotely: I will need to tell the application server something like "run mySingletonA on Server A, mySingletonB on Server B". For this I see that there is the possibility to invoke a remote EJB through <code>JNDI</code>.</p>
  
  <p>In order to satisfy constraint 2, I think that I need to use GlassFish <code>clustering</code>, because the PersitenceContext needs to be shared across the 2 instances of MySingleton.</p>
  
  <p>Unfortunately, I am not able to find any references on how to use the 2 tecnhologies together (remote EJB and clustering).</p>
  
  <p>I am looking for hints or suggestions about the preferred architecture for this scenario, and eventually some implementation guidelines.</p>