/** * Get an executor that always executes tasks in order. * * @return an ordered executor */ @Override public ArtemisExecutor getExecutor() { return new OrderedExecutor(parent); }
@Override public final void execute(Runnable run) { task(run); }
@Test public void shouldShutdownNowDoNotExecuteFurtherTasks() throws InterruptedException { final long timeoutMillis = TimeUnit.SECONDS.toMillis(10); final ExecutorService executorService = Executors.newSingleThreadExecutor(); try { final OrderedExecutor executor = new OrderedExecutor(executorService); final CountDownLatch executed = new CountDownLatch(1); executor.execute(executed::countDown); Assert.assertTrue("The task must be executed in " + timeoutMillis + " ms", executed.await(timeoutMillis, TimeUnit.MILLISECONDS)); executor.shutdownNow(); Assert.assertEquals("There are no remaining tasks to be executed", 0, executor.remaining()); //from now on new tasks won't be executed final CountDownLatch afterDeatchExecution = new CountDownLatch(1); executor.execute(afterDeatchExecution::countDown); Assert.assertFalse("After shutdownNow no new tasks can be executed", afterDeatchExecution.await(100, TimeUnit.MILLISECONDS)); //to avoid memory leaks the executor must take care of the new submitted tasks immediatly Assert.assertEquals("Any new task submitted after death must be collected", 0, executor.remaining()); } finally { executorService.shutdown(); } }
@Test public void shutdownNowOnDelegateExecutor() throws InterruptedException { final ExecutorService executorService = Executors.newSingleThreadExecutor(); try { final OrderedExecutor executor = new OrderedExecutor(executorService); final CountDownLatch latch = new CountDownLatch(1); final AtomicInteger numberOfTasks = new AtomicInteger(0); final CountDownLatch ran = new CountDownLatch(1); executor.execute(() -> { try { latch.await(1, TimeUnit.MINUTES); numberOfTasks.set(executor.shutdownNow()); ran.countDown(); } catch (Exception e) { e.printStackTrace(); } }); for (int i = 0; i < 100; i++) { executor.execute(() -> System.out.println("Dont worry, this will never happen")); } latch.countDown(); ran.await(1, TimeUnit.SECONDS); Assert.assertEquals(100, numberOfTasks.get()); Assert.assertEquals(ProcessorBase.STATE_FORCED_SHUTDOWN, executor.status()); Assert.assertEquals(0, executor.remaining()); } finally { executorService.shutdown(); } }
@Override public synchronized void initialize() throws ActiveMQException { if (state == STATE.INITIALIZED) return; synchronized (stateGuard) { if (state == STATE.CLOSING) throw new ActiveMQIllegalStateException(); try { state = STATE.INITIALIZED; latch = new CountDownLatch(1); setThreadPools(); topology.setExecutor(new OrderedExecutor(threadPool)); instantiateLoadBalancingPolicy(); if (discoveryGroupConfiguration != null) { discoveryGroup = createDiscoveryGroup(nodeID, discoveryGroupConfiguration); discoveryGroup.registerListener(this); discoveryGroup.start(); } } catch (Exception e) { state = null; throw ActiveMQClientMessageBundle.BUNDLE.failedToInitialiseSessionFactory(e); } } }
@Override public final void execute(Runnable run) { task(run); }
/** * Get an executor that always executes tasks in order. * * @return an ordered executor */ @Override public ArtemisExecutor getExecutor() { return new OrderedExecutor(parent); }
@Override public final void execute(Runnable run) { task(run); }
/** * Get an executor that always executes tasks in order. * * @return an ordered executor */ @Override public ArtemisExecutor getExecutor() { return new OrderedExecutor(parent); }
@Override public final void execute(Runnable run) { task(run); }
/** * Get an executor that always executes tasks in order. * * @return an ordered executor */ @Override public ArtemisExecutor getExecutor() { return new OrderedExecutor(parent); }
@Override public final void execute(Runnable run) { task(run); }
/** * Get an executor that always executes tasks in order. * * @return an ordered executor */ @Override public ArtemisExecutor getExecutor() { return new OrderedExecutor(parent); }
@Override public final void execute(Runnable run) { task(run); }
/** * Get an executor that always executes tasks in order. * * @return an ordered executor */ @Override public ArtemisExecutor getExecutor() { return new OrderedExecutor(parent); }
@Override public final void execute(Runnable run) { task(run); }
/** * Get an executor that always executes tasks in order. * * @return an ordered executor */ @Override public ArtemisExecutor getExecutor() { return new OrderedExecutor(parent); }
@Override public Acceptor createAcceptor(final String name, final ClusterConnection connection, final Map<String, Object> configuration, final BufferHandler handler, final ServerConnectionLifeCycleListener listener, final Executor threadPool, final ScheduledExecutorService scheduledThreadPool, final Map<String, ProtocolManager> protocolMap) { Executor failureExecutor = new OrderedExecutor(threadPool); return new NettyAcceptor(name, connection, configuration, handler, listener, scheduledThreadPool, failureExecutor, protocolMap); } }
@Test public void shouldExecuteTasksInOrder() throws InterruptedException { final int threads = 3; final int tasks = 100; final long timeoutMillis = TimeUnit.SECONDS.toMillis(10); final ExecutorService executorService = Executors.newFixedThreadPool(threads); try { final ArtemisExecutor executor = new OrderedExecutor(executorService); //it can be not thread safe too final List<Integer> results = new ArrayList<>(tasks); final List<Integer> expectedResults = new ArrayList<>(tasks); final CountDownLatch executed = new CountDownLatch(tasks); for (int i = 0; i < tasks; i++) { final int value = i; executor.execute(() -> { results.add(value); executed.countDown(); }); expectedResults.add(value); } Assert.assertTrue("The tasks must be executed in " + timeoutMillis + " ms", executed.await(timeoutMillis, TimeUnit.MILLISECONDS)); Assert.assertArrayEquals("The processing of tasks must be ordered", expectedResults.toArray(), results.toArray()); } finally { executorService.shutdown(); } }
@Override public synchronized void initialize() throws ActiveMQException { if (state == STATE.INITIALIZED) return; synchronized (stateGuard) { if (state == STATE.CLOSING) throw new ActiveMQIllegalStateException(); try { state = STATE.INITIALIZED; latch = new CountDownLatch(1); setThreadPools(); topology.setExecutor(new OrderedExecutor(threadPool)); instantiateLoadBalancingPolicy(); if (discoveryGroupConfiguration != null) { discoveryGroup = createDiscoveryGroup(nodeID, discoveryGroupConfiguration); discoveryGroup.registerListener(this); discoveryGroup.start(); } } catch (Exception e) { state = null; throw ActiveMQClientMessageBundle.BUNDLE.failedToInitialiseSessionFactory(e); } } }