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

lep*_*ome 5 java spring spring-batch

我查看了文档以及谷歌尝试学习的许多内容,但到目前为止,我已经空手而归:

1)是否有可设置的参数可以让我实现以下目的?如果有,我该如何配置?

我想设置批处理作业,这样除了"正常"优先级之外,我还可以选择运行"高"优先级作业,这会阻止其他人排队.在"正常"优先工作中,FIFO很好.我希望持续记录所提交的工作及其状态,最好是自动重试失败.

我特别使用Spring-Batch 3.0.3和Spring 4.0.6.我正在从JBoss AS 7.1.1服务器上的Web服务提交作业.

2)如果没有开箱即用的实现,我可以写一些东西(taskExecutor?)来实现这个目标吗?我怎么能这样做?

我得到了建议的ThreadPoolExecutor,但Job类仍然难以处理,因为我找不到指定作业类的位置.(由于多种原因,我正在使用jXML进行配置,而不是使用注释进行编程.)无论我做什么,部署总是继续使用org.springframework.batch.core.job.flow.FlowJob然后可以转换到不同的工作类.

spu*_*one 0

您可以使用带有 PriorityBlockingQueue 的 ExecutorService 来处理此问题,例如:

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

然后,对于您拥有的尽可能多的工作:

Job job = new Job(desiredPriority);
pool.execute(job);
Run Code Online (Sandbox Code Playgroud)

这些将由 PriorityBlockingQueue 根据您传递给它们的优先级进行排序。

您的 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
    }
}
Run Code Online (Sandbox Code Playgroud)