All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.ltsopensource.spring.tasktracker.JobDispatcher Maven / Gradle / Ivy

The newest version!
package com.github.ltsopensource.spring.tasktracker;

import com.github.ltsopensource.core.commons.utils.StringUtils;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.tasktracker.Result;
import com.github.ltsopensource.tasktracker.runner.JobContext;
import com.github.ltsopensource.tasktracker.runner.JobRunner;

/**
 * @author Robert HG ([email protected]) on 10/20/15.
 */
public class JobDispatcher implements JobRunner {

    private String shardField = "taskId";

    @Override
    public Result run(JobContext jobContext) throws Throwable {

        Job job = jobContext.getJob();

        String value;
        if (shardField.equals("taskId")) {
            value = job.getTaskId();
        } else {
            value = job.getParam(shardField);
        }

        JobRunner jobRunner = null;
        if (StringUtils.isNotEmpty(value)) {
            jobRunner = JobRunnerHolder.getJobRunner(value);
        }
        if (jobRunner == null) {
            jobRunner = JobRunnerHolder.getJobRunner("_LTS_DEFAULT");

            if (jobRunner == null) {
                throw new JobDispatchException("Can not find JobRunner by Shard Value : [" + value + "]");
            }
        }
        return jobRunner.run(jobContext);
    }

    public void setShardField(String shardField) {
        if (StringUtils.isNotEmpty(shardField)) {
            this.shardField = shardField;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy