import java.util.Hashtable; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.jboss.ejb.client.ContextSelector; import org.jboss.ejb.client.EJBClientConfiguration; import org.jboss.ejb.client.EJBClientContext; import org.jboss.ejb.client.PropertiesBasedEJBClientConfiguration; import org.jboss.ejb.client.remoting.ConfigBasedEJBClientContextSelector; import org.junit.Test; import cssb.csSession; /** * Tests multiple connection creation with the EJB Client API (non scoped). Member variables need to be replaced with * values for resp. environment. */ public class ConTestNonScoped { private String user = "USER"; private String password = "PASSWORD"; private String host = "localhost"; private String port = "4447"; private String maxOutboundMsgs = "10"; /** * Creates 41 Connections, for each one looks up a session bean and closes the connection. This fails after 40 * connections with "org.jboss.remoting3.ProtocolException: Too many channels open", irrespective of the value for * maxOutboundMsgs. */ @Test public void multipleConnectionTest() throws Exception { for (int i = 0; i < 42; i++) { InitialContext initialContext = getInitialContext(); System.out.println("Created context " + i); callSessionBean(initialContext); closeContext(initialContext); } } // Adjust the lkpString to get an actual EJB. Replace MySessionBeanInterface with the actual interface class. private void callSessionBean(InitialContext initialContext) throws Exception { String lkpString = "ejb:app-name/module-name//beanName!bean-interface"; Object lookup = initialContext.lookup(lkpString); if (lookup instanceof MySessionBeanInterface.class) { // The error only shows up if the session proxy is actually used ((MySessionBeanInterface) lookup).doSomething(); } } private InitialContext getInitialContext() throws NamingException { Hashtable contextProps = new Hashtable(); contextProps.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); initEJBClientContext(); return new InitialContext(contextProps); } private void initEJBClientContext() throws IllegalArgumentException { EJBClientConfiguration cc = getEJBClientConfiguration(); ContextSelector selector = new ConfigBasedEJBClientContextSelector(cc); EJBClientContext.setSelector(selector); } private EJBClientConfiguration getEJBClientConfiguration() { Properties ejbProps = new Properties(); ejbProps.put("endpoint.name", "client-endpoint"); ejbProps.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false"); ejbProps.put("remote.connections", "default"); ejbProps.put("remote.connection.default.host", host); ejbProps.put("remote.connection.default.port", port); ejbProps.put("remote.connection.default.username", user); ejbProps.put("remote.connection.default.password", password); ejbProps.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT", "false"); ejbProps.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS", "false"); ejbProps.put( "remote.connection.default.channel.options.org.jboss.remoting3.RemotingOptions.MAX_OUTBOUND_MESSAGES", maxOutboundMsgs); return new PropertiesBasedEJBClientConfiguration(ejbProps); } private void closeContext(InitialContext context) { try { Context ejbContext = (Context) context.lookup("ejb:"); if (ejbContext != null) { ejbContext.close(); } } catch (Exception e) { e.printStackTrace(); } try { context.close(); } catch (Exception e) { e.printStackTrace(); } } }