title: Problems accessing EJB in JBoss AS 7.x from Tomcat 6
body: <p>I am trying to make some remote calls to some EJB running inside a JBoss AS 7.1.1 final from my Servlet running under Tomcat 6.
  The EJBs are closed source and accessed from some given jars (closed code), which use a Delegate-Pattern to wrap the access to the stateless Beans.</p>
  
  <p>After days of struggling with the right configuration for the jndi-lookup, I found different configurations, which all make different problems.</p>
  
  <p><strong>Solution A:</strong></p>
  
  <p>Since Tomcat 6 has it's own jndi, simply putting a jndi.properties (like it works fine if accessing the EJBs from a simple java application) in the classpath, did not work. Instead I pass the properties in an hashtable to the delegate's constructor like this:</p>
  
  <pre><code>Hashtable&lt;String, String&gt; jndiProperties = new Hashtable&lt;String, String&gt;();
  jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
  jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.as.naming.interfaces:org.jboss.ejb.client.naming");
  jndiProperties.put(Context.PROVIDER_URL, "remote://10.10.10.10:4447");
  jndiProperties.put(Context.SECURITY_PRINCIPAL, "user");
  jndiProperties.put(Context.SECURITY_CREDENTIALS, "password");
  
  Delegate someDelegate = new DelegateImpl(jndiProperties);
  </code></pre>
  
  <p>This leads to the following exception (which does not occur when doing the same from a simple java application):</p>
  
  <pre><code>java.lang.IllegalStateException: No EJB receiver available for handling [appName:eex,modulename:eex-ejb,distinctname:] combination for invocation context 
  </code></pre>
  
  <p>Some research advised me to try....</p>
  
  <p><strong>Solution B:</strong></p>
  
  <p>I added the following line:</p>
  
  <pre><code>jndiProperties.put("jboss.naming.client.ejb.context", "true");
  </code></pre>
  
  <p>Now the lookup and remote call works fine, but after some calls I get the following exception (which also occurs when doing the same from a simple java application):</p>
  
  <pre><code>org.jboss.remoting3.ProtocolException: Too many channels open
  </code></pre>
  
  <p>As far as I understand, with jboss.naming.client.ejb.context set to true, a new EJBClientContext is created for every lookup on JBoss AS side and it seems, that the Delegate does not close it correctly. There's a detailed explanation on how to correctly close all contexts under <a href="https://docs.jboss.org/author/display/WFLY8/Scoped+EJB+client+contexts" rel="nofollow">https://docs.jboss.org/author/display/WFLY8/Scoped+EJB+client+contexts</a>.</p>
  
  <p>Since I don't own the Delegate-source-code, there is no way to close the contexts correctly.</p>
  
  <p>So I tried...</p>
  
  <p><strong>Solution C:</strong></p>
  
  <p>There's a whole different way to do the lookup: by providing a jboss-ejb-client.xml instead of a jndi.properties. This works well together with tomcat and the lookup is fine with this configuration:</p>
  
  <pre><code>remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
  remote.connections=default
  remote.connection.default.port=4447
  remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
  remote.connection.default.host=10.10.10.10
  remote.connection.default.username=user
  remote.connection.default.password=password
  </code></pre>
  
  <p>However, with this configuration I run into the following exception from time to time (I can not reproduce it so far, the exception just occurs sometimes):</p>
  
  <pre><code>ERROR: JBREM000200: Remote connection failed: the timeout for the connection expired
  ...and some lines later...
  Caused by: java.io.IOException: Channel Channel ID 841ded9c (outbound) of Remoting connection 00dcc89a to null has been closed
  </code></pre>
  
  <p>Any help is highly appreciated...</p>