HTTP job
SchedulerX HTTP jobs require an agent for scheduling.
Prerequisites
serverless mode: Triggers HTTP jobs through cloud functions or a serverless architecture. This mode is suitable for public API calls and lightweight jobs.
agent mode: Executes HTTP jobs by installing the SchedulerX agent on the target device. You must deploy the SchedulerX agent in advance. This mode is suitable for internal service calls and complex processing scenarios.
When creating an HTTP job in the MSE console, you can select either serverless or agent for the execution mode parameter.
Execution modes
The following table compares SchedulerX HTTP jobs and MSE-XXLJOB HTTP jobs:
SchedulerX Serverless (Deprecated) | SchedulerX agent | MSE-XXL-JOB | |
Client integration required | No. Requests are initiated by the server. | Yes. The server sends a command to the client, which initiates the request. | No. Requests are initiated by the server. |
Request method | Currently, only GET and POST are supported. Support for other methods will be added based on user demand. | ||
Request definition | Supports only cookie settings and POST parameter settings. | Supports all HTTP request parameter settings, including Header, Query, and Body. | |
Response definition |
| ||
Second-level scheduling | No, only minute-level scheduling is supported. | Yes | Yes |
Internal URL support | No | Yes | |
Execution without a domain name | No | Yes, supports integration with Kubernetes Services without requiring a gateway or domain name. | |
Broadcast sharding support | No | Yes | |
Task name parsing | If the task name contains Chinese characters, the backend can decode it by using | ||
How to create an HTTP job
You can create an HTTP job by using either the GET or POST request method.
Step 1: Configure basic information
GET
Deploy an accessible HTTP service.
If you have an accessible HTTP service, proceed to Step 2 to create the HTTP job in the MSE console. Have the URL, request method (such as GET or POST), and other configuration details for the target HTTP service ready.
If you have not deployed an HTTP service, refer to the following sample Java code for developing an HTTP endpoint:
@GET @Path("hi") @Produces(MediaType.APPLICATION_JSON) public RestResult hi(@QueryParam("user") String user) { TestVo vo = new TestVo(); vo.setName(user); RestResult result = new RestResult(); result.setCode(200); result.setData(vo); return result; }
Create an HTTP job in the MSE console.
Configure the parameters for the GET method as follows. For more information about how to create a scheduled job, see Create a scheduled job. Set task name to
http_get, select HTTP for task type, enter the target request URL in the full URL field, select GET for request method, and select Custom JSON for response parsing mode. Set return check key tocodeand return check value to200. Set execution timeout to10seconds and select serverless for execution mode.
POST
Deploy an accessible HTTP service.
If you have an accessible HTTP service, proceed to Step 2 to create the HTTP job in the MSE console. Have the URL, request method (such as GET or POST), and other configuration details for the target HTTP service ready.
If you have not deployed an HTTP service, refer to the following sample Java code for developing an HTTP endpoint:
import com.alibaba.schedulerx.common.constants.CommonConstants; @POST @Path("createUser") @Produces(MediaType.APPLICATION_JSON) public RestResult createUser(@FormParam("userId") String userId, @FormParam("userName") String userName) { TestVo vo = new TestVo(); System.out.println("userId=" + userId + ", userName=" + userName); vo.setName(userName); RestResult result = new RestResult(); result.setCode(200); result.setData(vo); return result; }
Create an HTTP job in the MSE console.
Configure the parameters for the POST method as follows. For more information about how to create a scheduled job, see Create a scheduled job. Set task name to
http_post, select HTTP for task type, enter the target request URL in the full URL field, and select POST for request method. Select Custom JSON for response parsing mode, set return check key tocode, and set return check value to200. Set execution timeout to10seconds. Selectapplication/x-www-form-urlencodedfor ContentType, enter the parameters in the formatkey1=value1&key2=value2in the POST parameters field, and select serverless for execution mode.
The following table describes the parameters for serverless HTTP and agent HTTP jobs.
The configuration parameters for the serverless and agent execution modes are the same in the MSE console.
Parameter | Description |
task name | A custom name for the job. |
Description | A brief description of the job for easier identification and searching. |
application ID | The application to which the job belongs. You can select an application from the drop-down list. |
task type | The type of processor for the job. In this scenario, select HTTP. |
full URL | Enter the full URL, which must start with |
request method | Select a request method. Valid values: GET and POST. |
response parsing mode | Select a response parsing mode. The following modes are supported:
|
If you select HTTP response code for response parsing mode: | |
HTTP response code | The HTTP response code that indicates a successful job. The default value is 200. |
If you select Custom JSON for response parsing mode: | |
return check key | The key in the JSON response to check for success. Only JSON return values are supported. |
return check value | The value in the JSON response that indicates success. Only JSON return values are supported. |
If you select Custom string for response parsing mode: | |
Custom string | The custom string to match. |
execution timeout |
|
ContentType | If request method is set to POST, specify the data format of the request body. The following data formats are supported:
|
POST parameters | If request method is set to POST, specify the POST form parameters. Examples:
|
cookie | For example: |
execution mode |
|
Advanced configuration | |
task failure retry count | The number of times to retry a job after it fails. The default value is 0. |
task failure retry interval | The interval between retries after a job fails. The default value is 30. Unit: seconds. |
task concurrency | The maximum number of concurrent instances for a job. A value of 1 means that instances cannot run concurrently. If this limit is exceeded, new instances are skipped. |
cleanup policy | The policy for cleaning up job instance history. The default policy is to keep the latest N records, where N is the value of retention count.
|
retention count | The number of job instance history records to retain. The default value is 300. |
Step 2: Configure scheduling settings
On the scheduling settings wizard page, set the scheduling and advanced parameters, and then click Next.
The following table describes the scheduling parameters.
Parameter | Description |
schedule type |
|
cron expression (applies only to the cron schedule type) | Enter a cron expression. You can enter an expression directly or use a tool to generate and validate one. |
fixed rate (applies only to the fixed_rate schedule type) | The fixed rate at which the job is scheduled. Unit: seconds. The value must be 60 or greater. For example, a value of 200 means the job is scheduled to run every 200 seconds. |
fixed delay (applies only to the second_delay schedule type) | The fixed delay before the job is triggered. Unit: seconds. The value must be in the range of 1 to 60. For example, a value of 5 means the job is triggered after a 5-second delay. |
scheduling time (applies only to the one_time schedule type) | Select a date and time. For example, |
Advanced configuration | |
data time offset | The offset of the data time relative to the scheduling time. You can retrieve this value from the context at runtime. |
time zone | Select a time zone based on your business requirements. You can select from common countries or regions, or use a standard GMT time zone. |
calendar | Specifies the calendar for the job schedule.
|
Effective Time | The time at which the job becomes active.
|
Step 3: Configure notification settings
HTTP jobs support failure alerts. If an issue such as a timeout or an unexpected return value occurs, you can set alert conditions when you create the job to receive corresponding notifications.
On the notification settings wizard page, set the alert parameters and contacts, and then click Complete.
Alert parameters include timeout alert (enabled by default; requires setting a timeout in seconds), timeout termination (disabled by default), success notification (disabled by default), failure alert (enabled by default; requires setting the consecutive failure count), and no available agent alert (enabled by default). For Notification Channels and Contacts, you can select Application Group Contacts or Custom.
After the job is created, navigate to the job management page. In the Actions column for the target job, click run once.
A successful execution is indicated by the following result. After the job runs successfully, you can view the execution result on the job instance history page. Click the target instance. On the details panel that appears, the basic information tab displays fields such as the instance ID, task name, start time, end time, and scheduling time. The result message
{"code":200,"data":{"name":"armon"}}indicates a successful job run with the expected JSON data.
Get basic job information
The basic information of an HTTP job is included in the request header. To obtain this information, add the following dependency to your client's pom.xml file.
<dependency>
<groupId>com.aliyun.schedulerx</groupId>
<artifactId>schedulerx2-common</artifactId>
<version>1.6.0</version>
</dependency>The following GET method example shows how to obtain basic job information.
import com.alibaba.schedulerx.common.constants.CommonConstants;
@GET
@Path("hi")
@Produces(MediaType.APPLICATION_JSON)
public RestResult hi(@QueryParam("user") String user,
@HeaderParam(CommonConstants.JOB_ID_HEADER) String jobId,
@HeaderParam(CommonConstants.JOB_NAME_HEADER) String jobName) {
TestVo vo = new TestVo();
vo.setName("armon");
// If JobName contains Chinese characters, URL decoding is required.
String decodedJobName = URLDecoder.decode(jobName, "utf-8");
System.out.println("user=" + user + ", jobId=" + jobId + ", jobName=" + decodedJobName);
RestResult result = new RestResult();
result.setCode(200);
result.setData(vo);
return result;
}Job constant definitions
The following table describes the job-related constants available in CommonConstants.
CommonConstants constant | Key | Description |
JOB_ID_HEADER | schedulerx-jobId | Job ID. |
JOB_NAME_HEADER | schedulerx-jobName | Job name. If the name contains non-ASCII characters (like Chinese), it is URL-encoded in the header. |
SCHEDULE_TIMESTAMP_HEADER | schedulerx-scheduleTimestamp | The timestamp of the scheduling time. |
DATA_TIMESTAMP_HEADER | schedulerx-dataTimestamp | The timestamp of the data time. |
GROUP_ID_HEADER | schedulerx-groupId | Application ID. |
USER_HEADER | schedulerx-user | Username. |
MAX_ATTEMPT_HEADER | schedulerx-maxAttempt | The maximum number of retries for the instance. |
ATTEMPT_HEADER | schedulerx-attempt | The current number of retries for the instance. |
JOB_PARAMETERS_HEADER | schedulerx-jobParameters | The job parameters. |
INSTANCE_PARAMETERS_HEADER | schedulerx-instanceParameters | The job instance parameters. An API call is required. |
Verify the result
You can view the execution results of HTTP jobs on the execution list page. For successful results, see GET.
When a task fails, click Details to view the specific reason for the failure:
The returned value is different from the expected value. On the Basic Information tab of the job instance details, the Result or Error Message section displays the error:
The returned value is different from the expected value. The returned JSON contains"success":false, which indicates that the HTTP job failed.Execution timed out. On the Basic Information page of the job instance, the Result or Error Message field shows a specific exception, such as
java.net.SocketTimeoutException. This indicates that the HTTP job failed because of a network timeout. The trigger method is manual.