简体   繁体   English

如何设置基于优先级运行批处理作业?

[英]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? 1)是否有可设置的参数可以让我实现以下目的? 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. 在“正常”优先工作中,FIFO很好。 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. 我特别使用Spring-Batch 3.0.3和Spring 4.0.6。 I'm submitting jobs from a webservice on a JBoss AS 7.1.1 server. 我正在从JBoss AS 7.1.1服务器上的Web服务提交作业。

2) If there isn't an out-of the box implementation, could I write something (a taskExecutor?) to achieve this goal? 2)如果没有开箱即用的实现,我可以写一些东西(taskExecutor?)来实现这个目标吗? 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. 我得到了建议的ThreadPoolExecutor,但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. (由于多种原因,我正在使用jXML进行配置,而不是使用注释进行编程。)无论我做什么,部署总是继续使用org.springframework.batch.core.job.flow.FlowJob然后可以转换到不同的工作类。

You can use an ExecutorService with a PriorityBlockingQueue to handle this, something like: 您可以使用带有PriorityBlockingQueue的ExecutorService来处理此问题,例如:

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. 这些将由PriorityBlockingQueue根据您传递给它们的优先级进行排序。

Your Job class will implement Runnable and Comparable: 您的Job类将实现Runnable和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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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