When I investigated how to make a console application with Spring Boot, it says that `` `CommandLineRunneror
ApplicationRunner``` is used when reading the reference guide.
By the way, when I somehow investigated the implementation class of CommandLineRunner
, there was a class called
JobLauncherCommandLineRunnerto run Spring Batch with Spring Boot. The default behavior of this class is to execute all `` `Job` `` in that context. However, it is written that a specific job can be executed by specifying
jobName```. I was wondering how to implement this, so I looked at the source of the relevant part.
JobLauncherCommandLineRunner
public class JobLauncherCommandLineRunner
implements CommandLineRunner, ApplicationEventPublisherAware {
@Autowired(required = false)
public void setJobs(Collection<Job> jobs) {
this.jobs = jobs;
}
@Override
public void run(String... args) throws JobExecutionException {
logger.info("Running default command line with: " + Arrays.asList(args));
launchJobFromProperties(StringUtils.splitArrayElementsIntoProperties(args, "="));
}
private void executeLocalJobs(JobParameters jobParameters)
throws JobExecutionException {
for (Job job : this.jobs) {
if (StringUtils.hasText(this.jobNames)) {
String[] jobsToRun = this.jobNames.split(",");
if (!PatternMatchUtils.simpleMatch(jobsToRun, job.getName())) {
logger.debug("Skipped job: " + job.getName());
continue;
}
}
execute(job, jobParameters);
}
}
After starting Spring Boot, the `run``` method of`
CommandLineRunner is executed. The job is started by the implementation here. Also, the injection of `` `setJbos
will include all Job``` collections. Next, the ```executeLocalJobs``` method is used to start all the
`Jobs one by one. At that time, if
jobName``` is specified, only matching jobs are started and others are skipped.
I wondered how to execute only a specific function with some key in the console application ~ I thought that even if there are multiple `` `CommandLineRunner```, all of them will be executed ~, but if you make it like this look good.
Recommended Posts