How to use CommandLineRunner in Spring Batch of Spring Boot

When I investigated how to make a console application with Spring Boot, it says that `` `CommandLineRunnerorApplicationRunner``` 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

How to use CommandLineRunner in Spring Batch of Spring Boot
How to use Lombok in Spring
How to use ModelMapper (Spring boot)
How to use In-Memory Job repository in Spring Batch
How to call and use API in Java (Spring Boot)
[Sprint Boot] How to use 3 types of SqlParameterSource defined in org.springframework.jdbc.core.namedparam
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
How to add a classpath in Spring Boot
How to use JQuery in js.erb of Rails6
How to bind to property file in Spring Boot
How to create a Spring Boot project in IntelliJ
How to boot by environment with Spring Boot of Maven
How to set environment variables in the properties file of Spring boot application
How to use Spring Data JDBC
How to use setDefaultCloseOperation () of JFrame
How to use InjectorHolder in OpenAM
How to use classes in Java?
How to change application.properties settings at boot time in Spring boot
Use @ControllerAdvice, @ExceptionHandler, HandlerExceptionResolver in Spring Boot to catch exceptions
How to control transactions in Spring Boot without using @Transactional
Multilingual Locale in Java How to use Locale
How to use custom helpers in rails
How to use named volume in docker-compose.yml
How to include Spring Tool in Eclipse 4.6.3?
How to use the same Mapper class in multiple data sources with Spring Boot + MyBatis
How to use Docker in VSCode DevContainer
How to use MySQL in Rails tutorial
How to set and use profile in annotation-based Configuration in Spring framework
[Java] [Maven3] Summary of how to use Maven3
How to use environment variables in RubyOnRails
Understand in 5 minutes !! How to use Docker
How to use credentials.yml.enc introduced in Rails 5.2
How to split Spring Boot message file
Let's find out how to receive in Request Body with REST API of Spring Boot
How to use ExpandableListView in Android Studio
[Spring Boot] I investigated how to implement post-processing of the received request.
How to read Body of Request multiple times with Spring Boot + Spring Security
Procedure to make the value of the property file visible in Spring Boot
How to not start Flyway when running unit tests in Spring Boot
Summary of how to select elements in Selenium
[Rails] How to use select boxes in Ransack
How to use "sign_in" in integration test (RSpec)
How to make Spring Boot Docker Image smaller
The story of raising Spring Boot 1.5 series to 2.1 series
Use Servlet filter in Spring Boot [Spring Boot 1.x, 2.x compatible]
Output of how to use the slice method
How to use enum (introduction of Japanese notation)
How to define multiple orm.xml in Spring4, JPA2.1
[Rails] How to use PostgreSQL in Vagrant environment
[Spring Boot] How to refer to the property file
Summary of what I learned in Spring Batch
Spring Boot --How to set session timeout time
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select