Implement processor interfaces and classes
A simple job is associated with a single handler and supports both job sharding and step-by-step execution:
Use the sharding feature for concurrent execution when resource utilization is not a concern.
Use the step-by-step feature to divide a job into multiple steps that are executed sequentially.
Implement the processor interface
/**
* Simple job handler interface
*/
public interface ISimpleJobHandler extends IJobHandler {
/**
* Handles the request.
*
* @param context
* @return
*/
ClientCommonResult handle(JobExecuteContext context);
}Code description:
JobExecuteContext is the request context for a triggered job. You can retrieve information about the job from this context, such as:
Custom parameters that are set in the console when you create a job. Use the
getCustomParam(String key)method to retrieve these parameters.If job sharding is enabled, use the
getSharding()method to retrieve the part number of the shard assigned to the current machine.
For more information, see the source code of the com.alipay.antschedulerclient.model.JobExecuteContext class.
Implementation class
The class must implement the following three methods:
handle: Executes the business logic.
getThreadPool: Retrieves a thread pool to execute the job. If a thread pool is not configured, the client's built-in thread pool is used.
new ThreadPoolExecutor(20,300,1,TimeUnit.HOURS,new ArrayBlockingQueue<Runnable>(100){})getName: Retrieves the name of the executor. This name is important because the client uses this name to find a matching executor when it receives a job trigger request.
If a job needs to run in multiple steps, you must write multiple implementation classes and enable the step-by-step feature in the console.
Code example
public class AlwaysSuccessHandler implements ISimpleJobHandler {
public static final String NAME = "ALWAYS_SUCCESS_JOB";
@Override
public ClientCommonResult handle(JobExecuteContext context) throws InterruptedException {
// Handle the business logic
return ClientCommonResult.buildSuccessResult();
}
@Override
public ThreadPoolExecutor getThreadPool() {
return null;
}
@Override
public String getName() {
return NAME;
}
}