All Products
Document Center

Scheduler Quick Start

Last Updated: Aug 21, 2020

Scheduler depends on SOFAMQ. Therefore, you must enable SOFAMQ before using Scheduler.

Before using Scheduler, ensure that you have completed the environment configuration. For more information, see Prerequisites.

To use Scheduler to implement features of scheduled tasks, follow these steps:

  1. Develop code.
  2. Publish the app to the cloud.
  3. Configure scheduled tasks on the cloud.

Develop code

Maven dependency

Add the following dependency to pom.xml of the module that receives the scheduled task:

  1. <dependency>
  2. <groupId></groupId>
  3. <artifactId>scheduler-enterprise-sofa-boot-starter</artifactId>
  4. </dependency>

Task processor code

  1. public class SchedulerDemo implements ISimpleJobHandler {
  2. ThreadPoolExecutor threadPoolExecutor;
  3. @Override
  4. public String getName() {
  5. return "DEMOAPP_DEMOTASK";
  6. }
  7. @Override
  8. public ThreadPoolExecutor getThreadPool() {
  9. return threadPoolExecutor;
  10. }
  11. @Override
  12. public ClientCommonResult handle(JobExecuteContext context) {
  13. boolean success = true;
  14. // The business logic.
  15. if (success) {
  16. return ClientCommonResult.buildSuccessResult();
  17. } else {
  18. return ClientCommonResult.buildFailResult("handle failed");
  19. }
  20. }
  21. public void setThreadPoolExecutor(ThreadPoolExecutor threadPoolExecutor) {
  22. this.threadPoolExecutor = threadPoolExecutor;
  23. }
  24. }

The task processor implements and all methods in this operation:

  • getName(): returns the scheduled task name, which must be the same as that you configure on the console.
  • getThreadPool(): returns the thread pool used for executing the task. You can inject an independent thread pool into each task processor or enable multiple task processors share the same thread pool. If null is returned, the common default thread pool is used.
  • handle(JobExecuteContext context): runs the task execution logic. This method must return ClientCommonResult, indicating the task execution result.

Spring configuration

You can declare the SchedulerDemo class that implements ISimpleJobHandler as a Spring bean in either of the following ways:

  • Define <bean/> in the Spring XML file. A sample is as follows:
  1. <bean id="schedulerDemo" class="com.antcloud.tutorial.scheduler.SchedulerDemo" />
  • Use the annotation-driven method to define <bean/>.

The server to be connected varies with the environment. Modify parameters in based on the environment requirements. For more information, see Import the SOFARPC Middleware

  • com.antcloud.antvip.endpoint

Publish an app

Publish the app to the cloud. For more information about how to publish a SOFABoot app, see Quick start.

Configure scheduled tasks on the cloud

After publishing the app, log on to the SOFAStack MS console and create and manage scheduled tasks. For more information, see Add and manage scheduled tasks.