简体   繁体   中英

How do I set up to run batch jobs based on priority?

I have looked through documentation as well as made numerous Google attempts to learn, but so far I've come up empty-handed:

1) Is there a settable parameter that would allow me to achieve the following? If there is, how do I configure it?

I would like to set up batch jobs so that in addition to the "normal" priority, I can have the option to run "high" priority jobs that would bump others in line. Among the "normal" priority jobs, FIFO is fine. I'd like to have persistent records of jobs that were submitted and their status, preferably with automatic retries of failures.

I am working with Spring-Batch 3.0.3 in particular and Spring 4.0.6 in general. I'm submitting jobs from a webservice on a JBoss AS 7.1.1 server.

2) If there isn't an out-of the box implementation, could I write something (a taskExecutor?) to achieve this goal? And how might I do it?

I got the suggested ThreadPoolExecutor to work, but the Job class is still intractable because I can't find where to specify the class of a job. (For a number of reasons, I'm doing the configurations in jXML, rather than programmatically with annotations.) No matter what I do, the deployment always proceeds with org.springframework.batch.core.job.flow.FlowJob and then can't convert to a different job class.

You can use an ExecutorService with a PriorityBlockingQueue to handle this, something like:

int maxThreads = 4;
ExecutorService pool = new ThreadPoolExecutor(1, maxThreads, 1, TimeUnit.SECONDS, new PriorityBlockingQueue<Runnable>());

Then, for as many jobs as you have:

Job job = new Job(desiredPriority);
pool.execute(job);

These will get ordered by the PriorityBlockingQueue based on the priority you passed in to them.

Your Job class will implement Runnable and Comparable:

public class Job implements Runnable, Comparable<Job> {
    private final int priority;

    public Job(int priority) {
        this.priority = priority;
    }

    @Override
    public int compareTo(Job o) {
        // enforces descending order (but this is up to you)
        if (this.priority > o.priority) {
            return -1;
        } else if (this.priority < o.priority) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public void run() {
        // do whatever needs to happen on a thread
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM