Microservices Engine (MSE) XXL-JOB provides the graceful shutdown feature in addition to the features provided by open source XXL-JOB. Before MSE XXL-JOB gracefully shuts down your application, it notifies the server to stop dispatching new jobs and waits for all running jobs are complete. Business interruptions do not occur during this process. This topic describes how to enable graceful shutdown based on Alibaba Cloud XXL-JOB to help you handle actual application restart or shutdown scenarios.
Overview
In actual business scenarios, jobs in an application process are scheduled at a fixed frequency. When an application is released and restarted, running jobs are forcefully stopped, which may lead to incomplete data and a sudden drop in the scheduling success rate. As a result, business data issues occur due to the following reasons:
Job execution interruptions: When a job is running, the application process is terminated and business processing is stopped. This may cause incomplete business data.
Decreased job scheduling success rate: During the application release and restart process, the scheduler distributes jobs to terminated nodes. This causes job scheduling failures and decreases the overall job processing efficiency.
To address the preceding issues in job scheduling, you need to enable graceful shutdown for jobs to ensure seamless business continuity during the rolling deployment restart processes of applications.
Practices for implementing graceful shutdown based on open source XXL-JOB
Prerequisites
The engine version is 2.1.0 or later. For more information about engine versions, see Release notes for XXL-JOB.
The dependency related to the SchedulerX plug-in package is added to the pom.xml file of the client. For more information about XXL-JOB plug-in versions, see Release notes for the XXL-JOB plug-in.
<dependency> <groupId>com.aliyun.schedulerx</groupId> <artifactId>schedulerx3-plugin-xxljob</artifactId> <version>Latest version</version> </dependency>
Procedure
This section describes how to configure and enable the graceful shutdown solution for different business forms and deployment scenarios. The procedure consists of two steps.
Step 1: Perform initial integration with executors by using different frameworks
Different deployment modes of business applications require different methods for initial integration.
Mode 1: Integrate business applications with XXL-JOB executors by using Spring Boot (recommended)
If you integrate a business application with XXL-Job executors by using Spring Boot, the system can automatically perform initial integration for graceful shutdown. Perform the following steps:
Add the Maven dependency of the SchedulerX plug-in. For more information about plug-in versions, see Plug-in version release notes.
<dependency> <groupId>com.aliyun.schedulerx</groupId> <artifactId>schedulerx3-plugin-xxljob</artifactId> <version>Latest version</version> </dependency>Add the application configuration parameter and enable graceful shutdown. For more information about the parameter, see Parameter description.
# Configure graceful shutdown xxl.job.executor.shutdownMode=WAIT_ALL
Mode 2: Integrate business applications with XXL-JOB executors by using Spring
If your business application is a web application that is started by using the Spring framework, you must add the POM dependency and application startup parameter. For more information, see Mode 1: Integrate business applications with XXL-JOB executors by using Spring Boot (recommended). You must also add the following XxlJobExecutorEnhancerInitializer configuration to the web.xml file:
<web-app>
<context-param>
<!-- Spring ApplicationContextInitializer is used to improve the capabilities of XXL-JOB executors. -->
<param-name>globalInitializerClasses</param-name>
<param-value>com.aliyun.schedulerx.xxljob.enhance.XxlJobExecutorEnhancerInitializer</param-value>
</context-param>
</web-app>Mode 3: Integrate business applications with XXL-JOB executors in the frameless mode
If you integrate a business application with XXL-JOB executors in the frameless mode, as mentioned in the use cases of XXL-JOB executors, when you start the business application in pure Java, you can perform initial integration for graceful shutdown by using custom code. You must add the POM dependency and application startup parameters for the business application. For more information, see Mode 1: Integrate business applications with XXL-JOB executors by using Spring Boot (recommended). The following code shows an example.
Before executors start, add
EnhancerLoader.load(xxlJobProp)to load feature enhancements.Before executors start, add
Runtime.getRuntime().addShutdownHook(...)to add a shutdown hook for the current application.
Step 2: Shut down an application
Use kill -15 in a self-built CD process
In a self-built continuous delivery (CD) process, one node is available to stop application processes. This node allows you create a script named stop.sh to stop or exit application processes. The script content must contain the logic for the graceful shutdown of the application. The following code shows the sample script content for application process shutdown.
Use the preStop hook for Kubernetes deployments
The lifecycle management feature of Kubernetes pods can automatically implement graceful shutdown. You can also use the preStop hook to implement the graceful shutdown logic by running exec to execute scripts and using HTTP requests.
Invalid modification: If the application process is the main process PID 1 in a container, the system automatically sends a SIGTERM signal to the main process to gracefully shut down the application.
Custom preStop: If the container has a complex multi-process relationship, you can configure a custom preStop script that uses
kill -15 PIDto stop the application process. You can also call the preconfiguredstop.shscript to exit the application process.
In this solution, the terminationGracePeriodSeconds parameter of the pod specifies the maximum wait time, in seconds, for graceful shutdown. The default value of this parameter is 30. You must configure the parameter based on your business requirements.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app-container
image: my-app-image:latest
lifecycle:
preStop:
exec:
# command: ["/bin/sh", "-c", "kill -15 PID && sleep 30"]
command: ["/bin/sh", "-c", "Script path/stop.sh"]Use automatic integration on the application release platform in Alibaba Cloud
This solution will be available soon.
Parameter description
You must configure the following parameter to enable graceful shutdown for your application. This parameter provides two modes for you to implement graceful shutdown.
# The graceful shutdown mode. Valid values: WAIT_ALL and WAIT_RUNNING.
# If you do not configure this parameter, the original logic of XXL-JOB is applied so that graceful shutdown is disabled by default.
xxl.job.executor.shutdownMode=WAIT_ALLGraceful shutdown mode | Description |
| In this mode, an application exits only after all jobs, including running jobs and jobs in queue, are complete. This mode is recommended. |
| In this mode, an application exits after running jobs to which threads are allocated in the application are complete. Jobs in queue are dropped. |



