简体   繁体   English

Spring Batch查找开始的工作执行

[英]Spring Batch find starting job executions

I have a Spring Batch application with one job. 我有一个工作的Spring Batch应用程序。 Job executions order is critical to me, so JobLauncher configured as a single thread job pool. 作业执行顺序对我至关重要,因此JobLauncher配置为单线程作业池。 In other words if few executions were triggered, one execution will be running, and other gonna wait in the queue. 换句话说,如果触发了很少的执行,则一个执行将在运行,而其他执行将在队列中等待。

Job executions table will look like that: 作业执行表将如下所示:

        ╒═════╤═════════════════════╤════════════╤════════════╤═════╕
        │ ... │ START_TIME          │ END_TIME   │ STATUS     │ ... │
        ╞═════╪═════════════════════╪════════════╪════════════╪═════╡
        │ ... │ 2019-07-11 11:03:08 │ NULL       │ STARTED    │ ... │
        ├─────┼─────────────────────┼────────────┼────────────┼─────┤
        │ ... │ NULL                │ NULL       │ STARTING   │ ... │
        └─────┴─────────────────────┴────────────┴────────────┴─────┘

If JVM crashes, I need to recover from that state and do some audit, so I need to find all STARTED and STARTING executions. 如果JVM崩溃,我需要从该状态恢复并进行一些审核,因此我需要查找所有STARTEDSTARTING执行。

Find the first one is not a problem, I can do it with JobExplorer : 找到第一个不是问题,我可以用JobExplorer

Set<JobExecution> executions = jobExplorer.findRunningJobExecutions("jobName");

It will give me only STARTED execution with present START_TIME . 它将仅在当前START_TIME给我STARTED执行。 But how to find all STARTING executions? 但是,如何找到所有STARTING执行程序?

The only way I've found so far is to iterate over the job instances, then find STARTING execution for every instance. 到目前为止,我发现的唯一方法是遍历作业实例,然后为每个实例查找STARTING执行。 Something like this: 像这样:

        jobExplorer.findJobInstancesByJobName("jobName", 0, 100)
                .forEach(jobInstance -> {
                    jobExplorer.getJobExecutions(jobInstance)
                            .stream()
                            .filter(execution -> STARTING.equals(execution.getStatus()))
                            .forEach(execution -> {
                                // do the job
                            });
                });

The problem here that you always doing a full scan with a limit (I specified 100). 这里的问题是您始终以一定的限制进行完全扫描(我指定为100)。 Are there any better way like jobExplorer.findStartingJobExecutions("jobName") in Spring Batch? Spring Batch中有没有像jobExplorer.findStartingJobExecutions("jobName")这样更好的方法?

I see no public APIs to get a job execution by status. 我看不到任何公共API可以按状态获得作业执行。 What you can do is (in pseudo code): 您可以做的是(使用伪代码):

JobInstance jobInstance = jobInstanceDao.getJobInstance(jobName, jobParameters);
List<JobExecution> jobExecutions =  jobExplorer.findJobExecutions(jobInstance);
// iterate over job execution and filter by status

This will avoid loading all job instances (one hundred at a time). 这样可以避免加载所有作业实例(一次加载一百个)。

That said, I think the JobRepository interface could add a method to get a job instance by name and parameters (similar to isJobInstanceExists ) to avoid using the jobInstanceDao as shown in my previous snippet. 就是说,我认为JobRepository接口可以添加一种通过名称和参数(类似于isJobInstanceExists )获取作业实例的方法,以避免使用我的上一片段中所示的jobInstanceDao

EDIT: 编辑:

I guess your initial requirement is to mark a job execution as failed to be able to restart it. 我猜您最初的要求是将作业执行标记为无法重新启动。 For this, you can manually change the status of job execution (as well as its step executions) to FAILED and set its END_TIME to a non null value (This is explained here: https://docs.spring.io/spring-batch/4.1.x/reference/html/job.html#aborting-a-job ). 为此,您可以将作业执行的状态(及其步骤执行)手动更改为FAILED并将其END_TIME设置为非null值(在此处进行解释: https : //docs.spring.io/spring-batch /4.1.x/reference/html/job.html#aborting-a-job )。 With that, you will be able to restart failed executions. 这样,您将能够重新启动失败的执行。

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

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