Spring 批处理 2.1.8 运行单个作业实例,当石英 cron 触发器触发时 [英] Spring batch 2.1.8 Run a single instance of a job, when a quartz cron trigger fires

查看:44
本文介绍了Spring 批处理 2.1.8 运行单个作业实例,当石英 cron 触发器触发时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个弹簧批石英装置.我有两个作业配置并并行运行.作业正在读取文件,然后将一些数据写入数据库.这是棘手的部分,文件名是在我的执行侦听器的 beforeJob() 方法中计算的.每个作业完成后,afterJob() 将计算下一个文件名.文件名具有以下模式 xxxxxx.nnnn 其中 nn.. 是数字,有时序列可能缺少数字,因此我试图跳过"那些缺失的段落,当我找到现有数字时启动作业.
我想知道是否可以限制每次 cron 触发器触发时启动的作业数量?我想在 chron 触发器触发后启动单个作业.

I have a spring batch quartz set up. I have two jobs configured and running in parallel. The jobs are reading a file and then writing some data to the db. Here is the tricky part, the file names are calculated within the beforeJob() method of my execution listener. After each job finishes, the afterJob() would then calculate the next fileName. File names have the following pattern xxxxxx.nnnn where nn.. are numbers and sometimes the sequence can have numbers missing, therefore I am attempting "jump" over those missing passages and when I find an existing number to launch the job.
I want to know if it's possible to restrict the number of jobs launched each time the cron trigger fires? I want to have a single job launched after a chron trigger fires.

例如:

  1. 在 12.30 PM 启动作业.
  2. 作业试图找到正确的文件
  3. 作业因 FileNotFound 而失败(配置为不可跳过的异常)
  4. 作业完成后,文件名计数器递增

现在,当触发器触发时,我会异步执行 4 个或更多相同类型的作业.在我的批处理设置中,我有两个 配置为在 5 分钟的间隔内每小时一个接一个地启动.这些作业都属于示例中提供的流程.结论:是否可以在 cron 触发器触发后启动单个作业并让两种作业类型并行运行.

Right now, when the trigger fires, I get like 4 or more jobs of the same type being executed asynchronously. In my batch set-up I have two <jobs> configured to launch one after the other each hour within a 5 minutes interval of one another. The jobs both fallow the flow provided in the example. In conclusion : Is it possible to launch a single job after a cron trigger fires and have both job types run in parallel.

彼得

推荐答案

您的工作应该在执行之前知道要处理的文件.例如.文件名应作为作业参数传递.删除 JobExecutionListener 并添加 StepExecutionListener 以通过 StepExecution#getJobParameters() 访问作业参数.一份工作 = 一份文件.

Your job should know what file to process before it is executed. E.g. the file name should be passed as job parameter. Remove JobExecutionListener and add StepExecutionListener to access job parameters via StepExecution#getJobParameters(). One job = one file.

现在从您的调度程序中,您要确保在某个时间点只运行一项作业.您可以通过两种方式实现这一点:

Now from your scheduler you want to make sure that only one job is run at one point of time. You can achieve this in two ways:

• 使用异步任务执行器.在这种情况下,每次启动作业时,它都不会在后台执行(除非您的调度程序每次都不会在新线程中触发计时器事件).

• Using the async task executor. In this case each time you launch the job, it will not be executed in a background (unless your scheduler does not fire timer events each time in new thread).

<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
    <property name="taskExecutor" ref="taskExecutor" />
</bean>

<bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor" />

• 如果您对其他需要在后台执行的作业使用相同的作业启动器,则需要使用 ThreadPoolTask​​Executor 但您可以手动检查作业是否正在运行:

• If you use the same job launcher for other jobs, which need to be executed in background, you need to use ThreadPoolTaskExecutor but you can manually check if the job is running:

for (final JobInstance jobInstance : jobExplorer.getJobInstances(jobName(), 0, LOOK_BEHIND_INSTANCES)) {
    for (final JobExecution jobExecution : jobExplorer.getJobExecutions(jobInstance)) {
        final BatchStatus status = jobExecution.getStatus();

        if (status.isRunning()) {
            // Ops:
            break;
        }
    }
}

这篇关于Spring 批处理 2.1.8 运行单个作业实例,当石英 cron 触发器触发时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆