问题
I have job configuration as below
@SpringBootApplication
public class Test implements CommandLineRunner {
@Autowired
JobLauncher jobLauncher;
@Autowired
Job job;
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
public static void main(String[] args) {
SpringApplication.run(Test.class, args);
}
@Override
public void run(String... args) throws Exception {
JobParameters params = new JobParametersBuilder()
.addString("JobID", String.valueOf(System.currentTimeMillis()))
.toJobParameters();
jobLauncher.run(job, params);
}
}
Now, the problem is that when i run this Test application, SimpleJobLauncher launches run method before JobParameters are created. From logs
10:12:58.422 - [ main] - INFO SimpleJobLauncher - Job: [SimpleJob: [name=demoJob]] launched with the following parameters: [{}]
10:12:58.466 - [ main] - INFO SimpleStepHandler - Step already complete or not restartable, so no action to execute: StepExecution: id=14, version=3, name=stepOne, status=COMPLETED, exitStatus=COMPLETED, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0, exitDescription=
10:12:58.478 - [ main] - INFO SimpleStepHandler - Step already complete or not restartable, so no action to execute: StepExecution: id=15, version=3, name=stepTwo, status=COMPLETED, exitStatus=COMPLETED, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0, exitDescription=
10:12:58.498 - [ main] - INFO SimpleJobLauncher - Job: [SimpleJob: [name=demoJob]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 44ms
10:12:58.530 - [ main] - INFO SimpleJobLauncher - Job: [SimpleJob: [name=demoJob]] launched with the following parameters: [{JobID=1592381578499}]
As you can see from the logs first demoJob is launched with no parameters
10:12:58.422 - [ main] - INFO SimpleJobLauncher - Job: [SimpleJob: [name=demoJob]] launched with the following parameters: [{}]
After finishing job with no parameters the it launches again with parameters.
10:12:58.530 - [ main] - INFO SimpleJobLauncher - Job: [SimpleJob: [name=demoJob]] launched with the following parameters: [{JobID=1592381578499}]
Lets say if there two jobs in application, then both jobs are launched eventhough i want to run a specific one with specified parameters Is there anyway to control this behaviour so Spring batch only launches only job with parameters i need
回答1:
You can disable automatic execution of jobs at startup by adding a property to application.yml or application.properties
spring.batch.job.enabled: false
来源:https://stackoverflow.com/questions/62424538/spring-batch-launches-simplejoblauncher-run-method-before-boot-run-method