Shutdown requested via dispatch.
This commit is contained in:
parent
19f3326823
commit
66cbcba33a
5 changed files with 53 additions and 8 deletions
|
|
@ -26,13 +26,16 @@ import org.apache.commons.logging.Log;
|
|||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import com.amazonaws.services.kinesis.clientlibrary.lib.worker.ShutdownReason;
|
||||
import com.amazonaws.services.kinesis.clientlibrary.types.InitializationInput;
|
||||
import com.amazonaws.services.kinesis.clientlibrary.types.ProcessRecordsInput;
|
||||
|
||||
import com.amazonaws.services.kinesis.multilang.messages.CheckpointMessage;
|
||||
import com.amazonaws.services.kinesis.multilang.messages.InitializeMessage;
|
||||
import com.amazonaws.services.kinesis.multilang.messages.Message;
|
||||
import com.amazonaws.services.kinesis.multilang.messages.ProcessRecordsMessage;
|
||||
import com.amazonaws.services.kinesis.multilang.messages.ShutdownMessage;
|
||||
import com.amazonaws.services.kinesis.multilang.messages.ShutdownRequestedMessage;
|
||||
|
||||
import com.amazonaws.services.kinesis.clientlibrary.types.InitializationInput;
|
||||
import com.amazonaws.services.kinesis.clientlibrary.types.ProcessRecordsInput;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
|
|
@ -145,6 +148,13 @@ class MessageWriter {
|
|||
return writeMessage(new ShutdownMessage(reason));
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a {@link ShutdownRequestedMessage} to the subprocess.
|
||||
*/
|
||||
Future<Boolean> writeShutdownRequestedMessage() {
|
||||
return writeMessage(new ShutdownRequestedMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a {@link CheckpointMessage} to the subprocess.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -16,10 +16,13 @@ package com.amazonaws.services.kinesis.multilang;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
|
@ -145,17 +148,16 @@ public class MultiLangDaemon implements Callable<Integer> {
|
|||
config.getRecordProcessorFactory(),
|
||||
executorService);
|
||||
|
||||
Runtime.getRuntime().addShutdownHook(new Thread()
|
||||
{
|
||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
LOG.info("Process terminanted, will initiate shutdown.");
|
||||
try {
|
||||
Future<Void> fut = daemon.worker.requestShutdown();
|
||||
fut.get();
|
||||
fut.get(5000, TimeUnit.MILLISECONDS);
|
||||
LOG.info("Process shutdown is complete.");
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
} catch (InterruptedException | ExecutionException | TimeoutException e) {
|
||||
LOG.error("Encountered an error during shutdown.", e);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ import com.amazonaws.services.kinesis.multilang.messages.InitializeMessage;
|
|||
import com.amazonaws.services.kinesis.multilang.messages.Message;
|
||||
import com.amazonaws.services.kinesis.multilang.messages.ProcessRecordsMessage;
|
||||
import com.amazonaws.services.kinesis.multilang.messages.ShutdownMessage;
|
||||
import com.amazonaws.services.kinesis.multilang.messages.ShutdownRequestedMessage;
|
||||
import com.amazonaws.services.kinesis.multilang.messages.StatusMessage;
|
||||
|
||||
import lombok.extern.apachecommons.CommonsLog;
|
||||
|
|
@ -99,6 +100,18 @@ class MultiLangProtocol {
|
|||
return waitForStatusMessage(ShutdownMessage.ACTION, checkpointer, writeFuture);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a {@link ShutdownRequestedMessage} to the child process's STDIN and waits for the child process to respond with a
|
||||
* {@link StatusMessage} on its STDOUT.
|
||||
*
|
||||
* @param checkpointer A checkpointer.
|
||||
* @return Whether or not this operation succeeded.
|
||||
*/
|
||||
boolean shutdownRequested(IRecordProcessorCheckpointer checkpointer) {
|
||||
Future<Boolean> writeFuture = messageWriter.writeShutdownRequestedMessage();
|
||||
return waitForStatusMessage(ShutdownRequestedMessage.ACTION, checkpointer, writeFuture);
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits for a {@link StatusMessage} for a particular action. If a {@link CheckpointMessage} is received, then this
|
||||
* method will attempt to checkpoint with the provided {@link IRecordProcessorCheckpointer}. This method returns
|
||||
|
|
|
|||
|
|
@ -150,11 +150,14 @@ public class MultiLangRecordProcessor implements IRecordProcessor, IShutdownNoti
|
|||
}
|
||||
try {
|
||||
LOG.info("Requesting a checkpoint on shutdown notification.");
|
||||
// ProcessRecordsInput emptyInput = new ProcessRecordsInput();
|
||||
|
||||
|
||||
checkpointer.checkpoint();
|
||||
} catch (InvalidStateException e) {
|
||||
LOG.error("Checkpoint triggered during shutdown encountered InvalidStateException: " + e.toString());
|
||||
LOG.error("Checkpoint triggered during shutdown encountered InvalidStateException: " + e, e);
|
||||
} catch (ShutdownException e) {
|
||||
LOG.error("Checkpoint triggered during shutdown encountered ShutdownException: " + e.toString());
|
||||
LOG.error("Checkpoint triggered during shutdown encountered ShutdownException: " + e, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
package com.amazonaws.services.kinesis.multilang.messages;
|
||||
|
||||
/**
|
||||
* A message to indicate to the client's process that shutdown is requested.
|
||||
*/
|
||||
public class ShutdownRequestedMessage extends Message {
|
||||
/**
|
||||
* The name used for the action field in {@link Message}.
|
||||
*/
|
||||
public static final String ACTION = "shutdownrequested";
|
||||
|
||||
/**
|
||||
* Convenience constructor.
|
||||
*/
|
||||
public ShutdownRequestedMessage() {
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue