Reputation: 633
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.
For example:
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.
Peter
Upvotes: 2
Views: 8761
Reputation: 10639
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" />
• 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;
}
}
}
Upvotes: 4