package testing.initialcontext; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import javax.naming.InitialContext; import javax.naming.NamingException; public class TestInitialContext { /** * @param args */ public static void main( String[] args ) { System.out.println( "---------- main() start. ---------"); Clazz clazz = new Clazz(); clazz.doIt(); System.out.println( "---------- main() Done. ---------"); } private static class Clazz { public void doIt() { // it seems the problem occurs more often the more Threads are used ExecutorService newFixedThreadPool = Executors.newFixedThreadPool( 4 ); newFixedThreadPool.submit( new Task() ); newFixedThreadPool.submit( new Task() ); newFixedThreadPool.submit( new Task() ); newFixedThreadPool.submit( new Task() ); } private class Task implements Runnable { @Override public void run() { System.out.println( "---------- Task start. ---------"); try { InitialContext ctx = new InitialContext(); // it seems that the queue needs to be existent, otherwise the error is not occurring // so this queue needs to be created on Glassfish first ctx.lookup( "queue/myTestQueue" ); } catch ( NamingException e ) { e.printStackTrace(); } System.out.println( "---------- Task done. ---------"); } } } }