Repository: incubator-batchee Updated Branches: refs/heads/master 563c55eab -> 30aa2a4d7
BATCHEE-131 shutdown BatchEE-CLI on Ctrl-C Project: http://git-wip-us.apache.org/repos/asf/incubator-batchee/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-batchee/commit/60e7e575 Tree: http://git-wip-us.apache.org/repos/asf/incubator-batchee/tree/60e7e575 Diff: http://git-wip-us.apache.org/repos/asf/incubator-batchee/diff/60e7e575 Branch: refs/heads/master Commit: 60e7e575a2c7c6195bafb7c86d3289bd53e8b26f Parents: 563c55e Author: Mark Struberg <[email protected]> Authored: Mon Jul 9 13:54:47 2018 +0200 Committer: Mark Struberg <[email protected]> Committed: Mon Jul 9 13:54:47 2018 +0200 ---------------------------------------------------------------------- .../batchee/cli/command/JobOperatorCommand.java | 18 ++++++++++++++++-- .../cli/lifecycle/impl/OpenEJBLifecycle.java | 13 +++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/60e7e575/tools/cli/src/main/java/org/apache/batchee/cli/command/JobOperatorCommand.java ---------------------------------------------------------------------- diff --git a/tools/cli/src/main/java/org/apache/batchee/cli/command/JobOperatorCommand.java b/tools/cli/src/main/java/org/apache/batchee/cli/command/JobOperatorCommand.java index bec981d..bb10fe7 100644 --- a/tools/cli/src/main/java/org/apache/batchee/cli/command/JobOperatorCommand.java +++ b/tools/cli/src/main/java/org/apache/batchee/cli/command/JobOperatorCommand.java @@ -48,8 +48,7 @@ import static java.lang.Thread.currentThread; * Note: the classloader is created from libs command, it is handy to organize batches * by folders to be able to run them contextual using this command. */ -public abstract class JobOperatorCommand implements Runnable { - // Remote config +public abstract class JobOperatorCommand implements Runnable { // Remote config @Option(name = "url", description = "when using JAXRS the batchee resource url") protected String baseUrl = null; @@ -178,6 +177,8 @@ public abstract class JobOperatorCommand implements Runnable { if (lifecycle != null) { lifecycleInstance = createLifecycle(loader); state = lifecycleInstance.start(); + + registerShutdownHook(lifecycleInstance, state); } else { lifecycleInstance = null; state = null; @@ -195,6 +196,19 @@ public abstract class JobOperatorCommand implements Runnable { } } + private void registerShutdownHook(final Lifecycle<Object> lifecycleInstance, final Object state) { + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + // as System.out as the Logger might already be resetted. + // Additionally we want to give this message to the person hitting Ctrl-C + // and not + System.out.println("\n Shutting down the JBatch engine started...\n"); + lifecycleInstance.stop(state); + } + }); + } + private Lifecycle<Object> createLifecycle(final ClassLoader loader) { // some shortcuts are nicer to use from CLI if ("openejb".equalsIgnoreCase(lifecycle)) { http://git-wip-us.apache.org/repos/asf/incubator-batchee/blob/60e7e575/tools/cli/src/main/java/org/apache/batchee/cli/lifecycle/impl/OpenEJBLifecycle.java ---------------------------------------------------------------------- diff --git a/tools/cli/src/main/java/org/apache/batchee/cli/lifecycle/impl/OpenEJBLifecycle.java b/tools/cli/src/main/java/org/apache/batchee/cli/lifecycle/impl/OpenEJBLifecycle.java index 3608f24..3ebc210 100644 --- a/tools/cli/src/main/java/org/apache/batchee/cli/lifecycle/impl/OpenEJBLifecycle.java +++ b/tools/cli/src/main/java/org/apache/batchee/cli/lifecycle/impl/OpenEJBLifecycle.java @@ -18,6 +18,8 @@ package org.apache.batchee.cli.lifecycle.impl; import org.apache.batchee.cli.classloader.ChildFirstURLClassLoader; import org.apache.batchee.container.exception.BatchContainerRuntimeException; +import org.apache.batchee.container.services.ServicesManager; +import org.apache.batchee.spi.BatchThreadPoolService; import org.apache.openejb.OpenEjbContainer; import org.apache.openejb.UndeployException; import org.apache.openejb.assembler.classic.AppInfo; @@ -39,12 +41,15 @@ import java.util.Map; import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicBoolean; // EJBContainer doesn't support war deployment public class OpenEJBLifecycle extends LifecycleBase<Object> { private Assembler assembler = null; private AppInfo info = null; + private AtomicBoolean running = new AtomicBoolean(false); + @Override public Object start() { final Map<String, Object> config = configuration("openejb"); @@ -78,6 +83,7 @@ public class OpenEJBLifecycle extends LifecycleBase<Object> { assembler = SystemInstance.get().getComponent(Assembler.class); assembler.createApplication(info, loader); + running.set(true); return initialContext; } catch (final Exception e) { throw new BatchContainerRuntimeException(e); @@ -94,6 +100,13 @@ public class OpenEJBLifecycle extends LifecycleBase<Object> { @Override public void stop(final Object state) { + if (!running.compareAndSet(true, false)) { + return; + } + + BatchThreadPoolService threadPoolService = ServicesManager.find().service(BatchThreadPoolService.class); + threadPoolService.shutdown(); + if (state != null) { if (assembler != null && info != null) { try {
