Use the SLS Java SDK to manage task instances of Scheduled SQL jobs. Supported operations:
-
View running status: Query task execution status (succeeded, failed, or running) within a time range.
-
Retry failed instances: Rerun instances that failed or require historical data recalculation.
-
Stop execution: Force-stop running instances that are stuck or taking too long.
Prerequisites
Simple Log Service is activated.
Code example
The following example lists instances, retrieves instance details, retries a failed instance, and stops a running instance.
package demo;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.*;
import com.aliyun.openservices.log.response.*;
import java.util.concurrent.TimeUnit;
public class Demo {
// The endpoint. Replace this with the endpoint of the project.
private static final String endpoint = "cn-hangzhou.log.aliyuncs.com";
// The AccessKey pair information.
private static final String accessKeyId = "your_access_key_id";
private static final String accessKeySecret = "your_access_key_secret";
// The destination project and the name of the Scheduled SQL job.
private static final String project = "your_project_name";
private static final String jobName = "your_scheduled_sql_job_name";
private static final Client client = new Client(endpoint, accessKeyId, accessKeySecret);
private static String instanceId = "11111";
private static final long fromTime = 1764216000;
private static final long toTime = 1764302400;
private static void testGetJobInstance() throws LogException {
// Get a job instance.
GetJobInstanceResponse getJobInstanceResponse = getJobInstance();
System.out.println("getJobInstance: " + JSONObject.toJSONString(getJobInstanceResponse));
}
private static void testListJobInstance() throws LogException, InterruptedException {
// List job instances.
System.out.println("Waiting for the job instance to start...");
TimeUnit.MINUTES.sleep(5);
ListJobInstancesResponse listJobInstancesResponse = client.listJobInstances(new ListJobInstancesRequest(project, jobName, fromTime, toTime));
if (listJobInstancesResponse.getResults().size() > 0) {
instanceId = listJobInstancesResponse.getResults().get(0).getInstanceId();
} else {
throw new LogException("NoJobInstance", "The job instances have not started. Please wait.", "");
}
System.out.println("List JobInstances: " + JSONObject.toJSONString(listJobInstancesResponse));
}
private static void testRerunJobInstance() throws LogException, InterruptedException {
System.out.println("Rerunning the job instance.......");
// Rerun the job instance.
GetJobInstanceResponse getJobInstanceResponse = getJobInstance();
String state = getJobInstanceResponse.getJobInstance().getState();
if ("SUCCEEDED".equals(state) || "FAILED".equals(state)) {
client.modifyJobInstanceState(new ModifyJobInstanceStateRequest(project, jobName, instanceId, "RUNNING"));
}
}
private static void testStopJobInstance() throws LogException {
System.out.println("Stopping the job instance.......");
// Stop the job instance.
ModifyJobInstanceStateResponse modifyJobInstanceStateResponse = client.modifyJobInstanceState(new ModifyJobInstanceStateRequest(project, jobName, instanceId, "STOPPED"));
}
private static GetJobInstanceResponse getJobInstance() throws LogException {
System.out.println("Getting the job instance.....");
return client.getJobInstance(new GetJobInstanceRequest(project, jobName, instanceId));
}
public static void main(String[] args) throws InterruptedException, LogException {
testListJobInstance();
testGetJobInstance();
testStopJobInstance();
testRerunJobInstance();
}
}
该文章对您有帮助吗?