private synchronized void setThreadPools() { if (threadPool != null) { return; } else if (useGlobalPools) { threadPool = ActiveMQClient.getGlobalThreadPool(); scheduledThreadPool = ActiveMQClient.getGlobalScheduledThreadPool(); } else { this.shutdownPool = true; ThreadFactory factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() { @Override public ThreadFactory run() { return new ActiveMQThreadFactory("ActiveMQ-client-factory-threads-" + System.identityHashCode(this), true, ClientSessionFactoryImpl.class.getClassLoader()); } }); if (threadPoolMaxSize == -1) { threadPool = Executors.newCachedThreadPool(factory); } else { threadPool = new ActiveMQThreadPoolExecutor(0, threadPoolMaxSize, 60L, TimeUnit.SECONDS, factory); } factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() { @Override public ThreadFactory run() { return new ActiveMQThreadFactory("ActiveMQ-client-factory-pinger-threads-" + System.identityHashCode(this), true, ClientSessionFactoryImpl.class.getClassLoader()); } }); scheduledThreadPool = Executors.newScheduledThreadPool(scheduledThreadPoolMaxSize, factory); } this.updateArrayActor = new Actor<>(threadPool, this::internalUpdateArray); }
private synchronized void setThreadPools() { if (threadPool != null) { return; } else if (useGlobalPools) { threadPool = ActiveMQClient.getGlobalThreadPool(); scheduledThreadPool = ActiveMQClient.getGlobalScheduledThreadPool(); } else { this.shutdownPool = true; ThreadFactory factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() { @Override public ThreadFactory run() { return new ActiveMQThreadFactory("ActiveMQ-client-factory-threads-" + System.identityHashCode(this), true, ClientSessionFactoryImpl.class.getClassLoader()); } }); if (threadPoolMaxSize == -1) { threadPool = Executors.newCachedThreadPool(factory); } else { threadPool = new ActiveMQThreadPoolExecutor(0, threadPoolMaxSize, 60L, TimeUnit.SECONDS, factory); } factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() { @Override public ThreadFactory run() { return new ActiveMQThreadFactory("ActiveMQ-client-factory-pinger-threads-" + System.identityHashCode(this), true, ClientSessionFactoryImpl.class.getClassLoader()); } }); scheduledThreadPool = Executors.newScheduledThreadPool(scheduledThreadPoolMaxSize, factory); } this.updateArrayActor = new Actor<>(threadPool, this::internalUpdateArray); }
private synchronized void setThreadPools() { if (threadPool != null) { return; } else if (useGlobalPools) { threadPool = ActiveMQClient.getGlobalThreadPool(); scheduledThreadPool = ActiveMQClient.getGlobalScheduledThreadPool(); } else { this.shutdownPool = true; ThreadFactory factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() { @Override public ThreadFactory run() { return new ActiveMQThreadFactory("ActiveMQ-client-factory-threads-" + System.identityHashCode(this), true, ClientSessionFactoryImpl.class.getClassLoader()); } }); if (threadPoolMaxSize == -1) { threadPool = Executors.newCachedThreadPool(factory); } else { threadPool = new ActiveMQThreadPoolExecutor(0, threadPoolMaxSize, 60L, TimeUnit.SECONDS, factory); } factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() { @Override public ThreadFactory run() { return new ActiveMQThreadFactory("ActiveMQ-client-factory-pinger-threads-" + System.identityHashCode(this), true, ClientSessionFactoryImpl.class.getClassLoader()); } }); scheduledThreadPool = Executors.newScheduledThreadPool(scheduledThreadPoolMaxSize, factory); } this.updateArrayActor = new Actor<>(threadPool, this::internalUpdateArray); }
private synchronized void setThreadPools() { if (threadPool != null) { return; } else if (useGlobalPools) { threadPool = ActiveMQClient.getGlobalThreadPool(); scheduledThreadPool = ActiveMQClient.getGlobalScheduledThreadPool(); } else { this.shutdownPool = true; ThreadFactory factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() { @Override public ThreadFactory run() { return new ActiveMQThreadFactory("ActiveMQ-client-factory-threads-" + System.identityHashCode(this), true, ClientSessionFactoryImpl.class.getClassLoader()); } }); if (threadPoolMaxSize == -1) { threadPool = Executors.newCachedThreadPool(factory); } else { threadPool = new ActiveMQThreadPoolExecutor(0, threadPoolMaxSize, 60L, TimeUnit.SECONDS, factory); } factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() { @Override public ThreadFactory run() { return new ActiveMQThreadFactory("ActiveMQ-client-factory-pinger-threads-" + System.identityHashCode(this), true, ClientSessionFactoryImpl.class.getClassLoader()); } }); scheduledThreadPool = Executors.newScheduledThreadPool(scheduledThreadPoolMaxSize, factory); } this.updateArrayActor = new Actor<>(threadPool, this::internalUpdateArray); }
private synchronized void setThreadPools() { if (threadPool != null) { return; } else if (useGlobalPools) { threadPool = ActiveMQClient.getGlobalThreadPool(); scheduledThreadPool = ActiveMQClient.getGlobalScheduledThreadPool(); } else { this.shutdownPool = true; ThreadFactory factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() { @Override public ThreadFactory run() { return new ActiveMQThreadFactory("ActiveMQ-client-factory-threads-" + System.identityHashCode(this), true, ClientSessionFactoryImpl.class.getClassLoader()); } }); if (threadPoolMaxSize == -1) { threadPool = Executors.newCachedThreadPool(factory); } else { threadPool = new ActiveMQThreadPoolExecutor(0, threadPoolMaxSize, 60L, TimeUnit.SECONDS, factory); } factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() { @Override public ThreadFactory run() { return new ActiveMQThreadFactory("ActiveMQ-client-factory-pinger-threads-" + System.identityHashCode(this), true, ClientSessionFactoryImpl.class.getClassLoader()); } }); scheduledThreadPool = Executors.newScheduledThreadPool(scheduledThreadPoolMaxSize, factory); } this.updateArrayActor = new Actor<>(threadPool, this::internalUpdateArray); }
private synchronized void setThreadPools() { if (threadPool != null) { return; } else if (useGlobalPools) { threadPool = ActiveMQClient.getGlobalThreadPool(); scheduledThreadPool = ActiveMQClient.getGlobalScheduledThreadPool(); } else { this.shutdownPool = true; ThreadFactory factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() { @Override public ThreadFactory run() { return new ActiveMQThreadFactory("ActiveMQ-client-factory-threads-" + System.identityHashCode(this), true, ClientSessionFactoryImpl.class.getClassLoader()); } }); if (threadPoolMaxSize == -1) { threadPool = Executors.newCachedThreadPool(factory); } else { threadPool = new ActiveMQThreadPoolExecutor(0, threadPoolMaxSize, 60L, TimeUnit.SECONDS, factory); } factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() { @Override public ThreadFactory run() { return new ActiveMQThreadFactory("ActiveMQ-client-factory-pinger-threads-" + System.identityHashCode(this), true, ClientSessionFactoryImpl.class.getClassLoader()); } }); scheduledThreadPool = Executors.newScheduledThreadPool(scheduledThreadPoolMaxSize, factory); } this.updateArrayActor = new Actor<>(threadPool, this::internalUpdateArray); }
private synchronized void setThreadPools() { if (threadPool != null) { return; } else if (useGlobalPools) { threadPool = ActiveMQClient.getGlobalThreadPool(); scheduledThreadPool = ActiveMQClient.getGlobalScheduledThreadPool(); } else { this.shutdownPool = true; ThreadFactory factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() { @Override public ThreadFactory run() { return new ActiveMQThreadFactory("ActiveMQ-client-factory-threads-" + System.identityHashCode(this), true, ClientSessionFactoryImpl.class.getClassLoader()); } }); if (threadPoolMaxSize == -1) { threadPool = Executors.newCachedThreadPool(factory); } else { threadPool = new ActiveMQThreadPoolExecutor(0, threadPoolMaxSize, 60L, TimeUnit.SECONDS, factory); } factory = AccessController.doPrivileged(new PrivilegedAction<ThreadFactory>() { @Override public ThreadFactory run() { return new ActiveMQThreadFactory("ActiveMQ-client-factory-pinger-threads-" + System.identityHashCode(this), true, ClientSessionFactoryImpl.class.getClassLoader()); } }); scheduledThreadPool = Executors.newScheduledThreadPool(scheduledThreadPoolMaxSize, factory); } this.updateArrayActor = new Actor<>(threadPool, this::internalUpdateArray); }
scheduledThreadPoolField.setAccessible(true); ThreadPoolExecutor threadPool = (ThreadPoolExecutor) ActiveMQClient.getGlobalThreadPool();
@Test public void testInjectPools() throws Exception { ActiveMQClient.clearThreadPools(); ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(1, ActiveMQThreadFactory.defaultThreadFactory()); ActiveMQClient.injectPools(poolExecutor, scheduledThreadPoolExecutor); final CountDownLatch inUse = new CountDownLatch(1); final CountDownLatch neverLeave = new CountDownLatch(1); ActiveMQClient.getGlobalThreadPool().execute(new Runnable() { @Override public void run() { System.err.println("Hello!"); try { inUse.countDown(); neverLeave.await(); } catch (Exception e) { e.printStackTrace(); neverLeave.countDown(); } } }); Assert.assertTrue(inUse.await(10, TimeUnit.SECONDS)); poolExecutor.shutdownNow(); scheduledThreadPoolExecutor.shutdownNow(); Assert.assertTrue(neverLeave.await(10, TimeUnit.SECONDS)); Assert.assertTrue(inUse.await(10, TimeUnit.SECONDS)); Assert.assertTrue(neverLeave.await(10, TimeUnit.SECONDS)); ActiveMQClient.clearThreadPools(100, TimeUnit.MILLISECONDS); }
@Test public void testShutdownPoolInUse() throws Exception { ActiveMQClient.setGlobalThreadPoolProperties(10, 1); ActiveMQClient.clearThreadPools(); final CountDownLatch inUse = new CountDownLatch(1); final CountDownLatch neverLeave = new CountDownLatch(1); ActiveMQClient.getGlobalThreadPool().execute(new Runnable() { @Override public void run() { System.err.println("Hello!"); try { inUse.countDown(); neverLeave.await(); } catch (Exception e) { e.printStackTrace(); neverLeave.countDown(); } } }); Assert.assertTrue(inUse.await(10, TimeUnit.SECONDS)); ActiveMQClient.clearThreadPools(100, TimeUnit.MILLISECONDS); Assert.assertTrue(neverLeave.await(10, TimeUnit.SECONDS)); }