Java jobs

Updated at:

You can run scheduled Java tasks in your application processes. This topic describes how to manage Java jobs.

Execution modes

Java jobs support the following execution modes:

  • Standalone: The job is executed on a random worker in the groupId.

  • Broadcast: The job is executed concurrently on all workers in the groupId.

  • Visual MapReduce: A MapReduce model task that requires the Professional Edition. It supports a maximum of 1,000 subtasks. You can view detailed execution records and query operational logs or stacks for subtasks using business keywords.

  • MapReduce: A regular MapReduce model task. It supports parallel processing for many subtasks. You can only query summary information about the subtask execution. Select this mode when the number of subtasks is less than 1,000,000.

  • Sharding: This mode includes static sharding and dynamic batching to handle big data workloads.

Implement the JavaProcessor class for Standalone and Broadcast modes. Implement the MapJobProcessor class for Visual MapReduce, MapReduce, and Sharding modes.

The processor class path is the fully qualified path of the implementation class. For example: com.apache.armon.test.schedulerx.processor.MySimpleJob.

  • If you do not upload a JAR package, SchedulerX searches for the processor implementation class in your application's classpath. Therefore, you must recompile and republish the application each time you modify the job.

  • If you upload a JAR package, SchedulerX hot-reloads the JAR package and processor. You do not need to republish the application after you modify the job.

Programming models

Java jobs support two programming models: JavaProcessor and MapJobProcessor.

  • JavaProcessor

    • Optional: public void preProcess(JobContext context) throws Exception

    • Required: public ProcessResult process(JobContext context) throws Exception

    • Optional: public void postProcess(JobContext context)

    • Optional: public void kill(JobContext context)

  • MapJobProcessor

    • Required: public ProcessResult process(JobContext context) throws Exception

    • Optional: public void postProcess(JobContext context)

    • Optional: public void kill(JobContext context)

    • Required: public ProcessResult map(List<? extends Object> taskList, String taskName)

ProcessResult

Each process must return a ProcessResult to report the execution status, result, and error message of the task.

  • The task succeeded: return new ProcessResult(true).

  • The task failed: return new ProcessResult(false, errorMessage) or throw an exception.

  • The task succeeded and returned a result: return new ProcessResult(true, result). The result is a string that cannot exceed 1000 bytes.

Example: Hello, SchedulerX 2.0 task

@Component
public class MyProcessor1 extends JavaProcessor {

    @Override
    public ProcessResult process(JobContext context) throws Exception {
        //TODO
        System.out.println("Hello, schedulerx2.0!");
        return new ProcessResult(true);
    }
}            

Example: Task that can be killed

@Component
public class MyProcessor2 extends JavaProcessor {
    private volatile boolean stop = false;

    @Override
    public ProcessResult process(JobContext context) throws Exception {
        int N = 10000;
        while (!stop && N >= 0) {
            //TODO
            N--;
        }
        return new ProcessResult(true);
    }

    @Override
    public void kill(JobContext context) {
        stop = true;
    }

    @Override
    public void preProcess(JobContext context) {
        stop = false;  // If the job is started by Spring and the bean is a singleton, you must use preProcess to reset the flag.
    }
}          

Example: Batch processing task using the Map model

/**
 * Perform distributed batch processing on a non-partitioned table.
 * 1. The root task queries a table to get minId and maxId.
 * 2. Construct PageTask and distribute it using the map method.
 * 3. If the next level receives a PageTask, process the data.
 *
 */
@Component
public class ScanSingleTableJobProcessor extends MapJobProcessor {
    private static final int pageSize = 100;

    static class PageTask {
        private int startId;
        private int endId;
        public PageTask(int startId, int endId) {
             this.startId = startId;
             this.endId = endId;
        }
        public int getStartId() {
              return startId;
        }
        public int getEndId() {
              return endId;
        }
    }

    @Override
    public ProcessResult process(JobContext context) {
        String taskName = context.getTaskName();
        Object task = context.getTask();
        if (isRootTask(context)) {
            System.out.println("start root task");
            Pair<Integer, Integer> idPair = queryMinAndMaxId();
            int minId = idPair.getFirst();
            int maxId = idPair.getSecond();
            List<PageTask> taskList = Lists.newArrayList();
            int step = (int) ((maxId - minId) / pageSize); // Calculate the number of pages.
            for (int i = minId; i < maxId; i+=step) {
                taskList.add(new PageTask(i, (i+step > maxId ? maxId : i+step)));
            }
            return map(taskList, "Level1Dispatch"); // The process calls the map method to distribute subtasks.
        } else if (taskName.equals("Level1Dispatch")) {
            PageTask record = (PageTask)task;
            long startId = record.getStartId();
            long endId = record.getEndId();
            //TODO
            return new ProcessResult(true);
        }

        return new ProcessResult(true);
    }

    @Override
    public void postProcess(JobContext context) {
        //TODO
        System.out.println("all tasks is finished.");
    }

    private Pair<Integer, Integer> queryMinAndMaxId() {
        //TODO select min(id),max(id) from xxx
        return null;
    }

}