Skip to content
Permalink
Browse files
BATCHEE-131 shutdown BatchEE-CLI on Ctrl-C
  • Loading branch information
struberg committed Jul 9, 2018
1 parent 563c55e commit 60e7e575a2c7c6195bafb7c86d3289bd53e8b26f
Showing 2 changed files with 29 additions and 2 deletions.
@@ -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 @@ public final void run() {
if (lifecycle != null) {
lifecycleInstance = createLifecycle(loader);
state = lifecycleInstance.start();

registerShutdownHook(lifecycleInstance, state);
} else {
lifecycleInstance = null;
state = null;
@@ -195,6 +196,19 @@ public final void run() {
}
}

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)) {
@@ -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 @@ public Object start() {
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 Object start() {

@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 {

0 comments on commit 60e7e57

Please sign in to comment.