One Time

Updated at:

A one-time schedule runs a task once at a specified time. After the task is complete, SchedulerX automatically deletes it, so you do not need to delete the task from the client. One-time schedules are suitable for scenarios such as handling timed-out unpaid orders or automatically closing scheduled calendar reminders. This topic describes how to create a one-time scheduled task using an API.

Benefits

  • Precise scheduling: Unlike delayed messages, one-time tasks in SchedulerX are not limited by a fixed delay period. You can schedule a task to run at any point in the future, which provides greater flexibility and ease of use.

  • Multiple task types: One-time tasks in SchedulerX support all task types, such as Java, HTTP, and Shell. They also support all distributed models, such as standalone, broadcast, sharding, and MapReduce.

  • Visualized operations: One-time tasks in SchedulerX provide a visual interface for monitoring and queries, similar to other tasks. You can modify task parameters before the scheduled time. Automatic retries for failed tasks are also supported.

Create a one-time scheduled task using an API

  1. Add the OpenAPI SDK dependency to the pom.xml file of your application.

  2. The following sample code shows how to use the SDK to call the API. Set the timeType parameter to 5 to enable one-time scheduling.

    Note

    For more information about configuring the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables, see Configuration methods.

    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.schedulerx2.model.v20190430.CreateJobRequest;
    import com.aliyuncs.schedulerx2.model.v20190430.CreateJobResponse;
    
    public class CreateJavaJob {
    
        public static void main(String[] args) throws Exception {
            // The endpoint of OpenAPI. Specify the endpoint based on the list of supported regions and the region where you purchased the service.
            String regionId = "public";
          	/**
             * 1. The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Use a Resource Access Management (RAM) user to call APIs or perform routine O&M.
             * 2. Do not save the AccessKey ID and AccessKey secret in your project code. This prevents AccessKey pair leaks and protects all resources in your account.
             * 3. This example loads the AccessKey ID and AccessKey secret from environment variables for identity verification. You must configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables in advance.
             * You can use other encryption methods to store your identity information as needed.
             */
            String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
            String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
            
    				// The product name.
            String productName ="schedulerx2";
            // Specify the domain name based on the list of supported regions.
            String domain ="schedulerx.aliyuncs.com";
            // Create an OpenAPI client.
            DefaultProfile.addEndpoint(regionId, productName, domain);
            DefaultProfile defaultProfile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
            DefaultAcsClient client = new DefaultAcsClient(defaultProfile);
            
            CreateJobRequest request = new CreateJobRequest();
            request.setJobType("java");
            request.setExecuteMode("standalone");
            request.setDescription("test");
            request.setName("one-time task test");
            request.setClassName("com.alibaba.schedulerx.test.processor.HelloWorldJob3");
            // timeType=5 indicates a one-time task.
            request.setTimeType(5);
            //yyyy-MM-dd HH:mm:ss
            request.setTimeExpression("2021-12-15 12:11:00");
            request.setNamespace("433d8b23-06e9-408c-aaaa-90d4d1b9a4af");
            request.setGroupId("xueren_sub");
            // Monitoring and alerts
            request.setTimeoutEnable(true);
            request.setTimeoutKillEnable(true);
            request.setFailEnable(true);
            request.setTimeout(12300L);
            // Advanced configuration. Configure automatic retries for failed tasks.
            request.setMaxAttempt(3);
            request.setAttemptInterval(30);
            CreateJobResponse response = client.getAcsResponse(request);
            if (response.getSuccess()) {
                System.out.println("jobId=" + response.getData().getJobId());
            } else {
                System.out.println(response.getMessage());
            }
        }   
    }
  3. After you create the task using the API, log on to the console to view the task.

    1. Log on to the Distributed Task Scheduling Platform.

    2. In the navigation pane on the left, click Application Management.

    One-time task