Services Framework graceful shutdown
The Services Framework API provides a task with the ability to detect that the Services Framework Liberty server is shutting down. And then to take appropriate action allowing the task to resume execution from the point of shutdown on the next available Services Framework server.
The SampleExtractTask
Services Framework sample task that is included with FTM provides an
example of using this feature of the API. The specifics of using this feature are provided in the following
sections.
The codes that need to be added to the SampleExtractTask task to enable the shutdown of
Services Framework are described in the following sections.
Implementing a new class
TaskShutdownHandler class and overrides
the abstract processShutdown
method.public class MyTaskShutdownHandler extends TaskShutdownHandler
{
@Override
public boolean processShutdown()
{
// Custom shutdown handling code
// Return true to allow the task to be shutdown
return true;
}
}Overriding createTaskShutdownHandler
The Services Framework runtime invokes the createTaskShutdownHandler method
before the task’s runTask method. This step registers the custom
TaskShutdownHandler with the framework and enables the task shutdown or resume feature for
the task.
createTaskShutdownHandler method are shown in the following list.- Declare
MyTaskShutdownHandlerby using a class variable. - Override the
createTaskShutdownHandlermethod of theBaseIPDTaskclass in your custom task class. This method needs to create an instance of the customTask Shutdown Handlerand return it.@Override protected TaskShutdownHandler createTaskShutdownHandler() { this.myShutdownHandler = new MyTaskShutdownHandler(); return this.myShutdownHandler; }
Invoking registerSqlStatementForShutdown
This step enables the shutdown thread to cancel the database query/update/insert operation
at the time of shutdown.
registerSqlStatementForShutdown method of the
TaskShutdownHandler to register a Java PreparedStatement with the
DatabaseStatementService before issuing a call to the
database.public void registerSqlStatementForShutdown(Statement dbStatement,
String sql, String moduleName)When using database accessor classes to access the database, the prepared statements are automatically
registered and unregistered. Therefore, it is not needed to use the
registerSqlStatementForShutdown and deRegisterSqlStatementForShutdown
methods.
com.ibm.ftm.base.util.database.accessors.DatabaseAccessor class.Invoking deRegisterSqlStatementForShutdown
deRegisterSqlStatementForShutdown method when
PreparedStatement is not used to minimize the memory
footprint.public void registerSqlStatementForShutdown(Statement dbStatement)Invoking TaskShutdownHandler.checkAndProcessShutdown
TaskShutdownHandler.checkAndProcessShutdown method from your task execution
code by using the following steps.- Call
checkAndProcessShutdownwhen long-running loops occur and for each iteration of the loop. - Call
checkAndProcessShutdownwhenSQLExceptionor thefinallyclause that is associated with that exception is caught. It is because thecanceloperation on thePreparedStatementsdatabase that is registered withTaskShutdownHandlerthrowsSQLExceptionafter cancellation. - Call
checkAndProcessShutdownto determine whether the shutdown occurred and if so, to process the shutdown. - Call
checkAndProcessShutdownwhen catching aDatabaseAccessorExceptionor in thefinallyclause that is associated with that exception.
When shutdown is complete, the status of the task instance displays Shutdown.
falseif shutdown is not signaled.- If shutdown is signaled, it invokes the
processShutdownmethod that was implemented in the custom task shutdown handler. - If it returns
true,checkAndProcessShutdowndoes not return. Instead, it throws theTaskShutdownExceptionexception to signal the Services Framework runtime that the task completed shutdown processing and is ready to be shutdown.Note: Your task code must not catch this exception. It must be allowed to propagate to the upstream runtime code.
Resuming the task after shutdown
TaskShutdownHandler is
registered with the framework are shown in the following list.- An IBM® MQ message is posted to the Services Framework input queue
(
FXH.SERVICESFRAMEWORK.INPUT.QUEUE) when shutdown processing is complete. This IBM MQ message resumes the task that is being shutdown. - The next available Services Framework engine processes the message and resumes the task execution based on the parameters that the task’s shutdown process creates. For example, the resume-points created at the time of shutdown.
Task shutdown process
- The task’s
createTaskShutdownHandleris called. - When a custom
TaskShutdownHandleris implemented for this task, thecreateTaskShutdownHandlermethod creates an instance of this class and returns it to the Services Framework runtime. This registers the task instance with the Services Framework runtime. It also effectively enables the runtime to execute graceful shutdown or to resume the task. - The custom task code invokes the
TaskShutdownHandler.checkAndProcessShutdownmethod during some iterative processes. Also, database prepared statements are registered withTaskShutdownHandler. - The operator manually deletes the services-engine pod or the Red Hat®
OpenShift® operator
deletes it. The Kubernetes
preStoplifecycle hook is triggered. - The FTM operator issues an HTTP post request to the Services Framework engine.
- The Services Framework shutdown process executes on a distinct thread within the Services Framework engine.
- For each task instance that is registered with the runtime, the
Thread.interruptmethod is called. Also, any database-prepared statements that were registered with theTaskShutdownHandlerare canceled. - The shutdown process waits for each registered task instance status to be set to a value of
shutdownor until the time theShutdown Timeoutcore property elapses. - The HTTP post call returns and the Liberty server shutdown quiesce process runs, and then the server shuts down.
- Back on the task thread, the
TaskShutdownHandler.checkAndProcessShutdownmethod is called and it detects that the thread is interrupted. It runs theprocessShutdownmethod of the customTaskShutdownHandler. This step runs the required shutdown processing for the task.- The
TaskShutdownExceptionexception is thrown. The Services Framework runtime catches this exception.
- A resume-task message is posted. This message enables the next available server to automatically resume
the task. The task instance status is set to
shutdown. - The task execution completes. When Services Framework engine receives new task messages on its input queue after shutdown, the messages are reposted to the Services Framework input queue so that the next available server can process them.