BATCHEE-131 shutdown BatchEE-CLI on Ctrl-C
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 @@
* 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 @@
if (lifecycle != null) {
lifecycleInstance = createLifecycle(loader);
state = lifecycleInstance.start();
+
+ registerShutdownHook(lifecycleInstance, state);
} else {
lifecycleInstance = null;
state = null;
@@ -195,6 +196,19 @@
}
}
+ 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)) {
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 @@
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.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 @@
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 @@
@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 {