Spring package 2.1.8 Run one instance of a job when a quartz trigger fires

I have spring batch quartz. I have two tasks configured and work in parallel. Jobs read the file and then write some data in db. Here's the tricky part, file names are computed in the beforeJob () method of my executive executor. After completing each job, the afterJob () function will calculate the next filename. File names have the following pattern xxxxxx.nnnn , where nn .. are numbers, and sometimes numbers may be missing in the sequence, so I try to "jump" over these missing passages and when I find an existing number to start the task.
I want to know if it is possible to limit the number of jobs run each time the cron trigger starts? I want one task to start after the trigger fires.

For instance:

  • At 12.30 work begins.
  • The task is trying to find the correct file.
  • Job failed with FileNotFound (which is configured as an exception that excludes a failure)
  • After the job, the fileName counter is incremented

Right now, when the trigger fires, I get as 4 or more jobs of the same type, executed asynchronously. In my batch setup, I have two <jobs> configured to run one after another every hour for 5 minutes one after another. Jobs both lower the flow shown in the example. In conclusion: is it possible to start one task after starting the cron trigger and start both types of tasks in parallel.

Peter

+2
source share
1 answer

Your job should know which file to process before executing it. For instance. the file name should be passed as a 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 task is executed at a time. You can achieve this in two ways:

• Using the async task executor. In this case, every time you run a task, it will not run in the background (unless your scheduler fires timer events every time in a 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 launcher for other tasks that need to be run in the background, you need to use ThreadPoolTaskExecutor , but you can manually check if the task 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; } } } 
+4
source

Source: https://habr.com/ru/post/1402437/


All Articles