Create a simple Spring Batch and batch launch it from a web request.
Add spring-boot-starter-batch to the dependency to use SpringBatch.
Also, since DB is used for Job management, an appropriate JDBC is also added.
pom.xml
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <scope>runtime</scope>
</dependency>
** Basically created by referring to here **
Create an appropriate processing class as @Service
@Service
public class MyBatchService {
	public void execute(Long id, String name, Date reqDate) throws InterruptedException {
		//Appropriate batch processing
		Thread.sleep(5000);
	}
}
The liver of batch processing. This time, easily create one one-step (tasklet) job. Set in Java configuration class.
@Configuration
@EnableBatchProcessing //(1)
public class BatchConfig {
	@Autowired
	private JobBuilderFactory jobBuilderFactory; //(2)
	@Autowired
	private StepBuilderFactory stepBuilderFactory; //(2)
	@Autowired
	private MyBatchService service;
	@Bean
	public JobLauncher jobLauncher1(JobRepository jobRepository) { //(2),(3)
		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
		//The number of concurrent executions is 3.Wait for more queues.
		taskExecutor.setCorePoolSize(3); //(4)
		// java.lang.IllegalStateException: ThreadPoolTaskExecutor not initialized
		taskExecutor.initialize(); //(5)
		SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
		jobLauncher.setJobRepository(jobRepository);
		jobLauncher.setTaskExecutor(taskExecutor);
		return jobLauncher;
	}
	@Bean
	public Job job(Step step1) { //(6)
		return jobBuilderFactory.get("batchjob")
					.incrementer(new RunIdIncrementer())
					.start(step1)
					.build();
	}
	@Bean
	public Step step1(Tasklet tasklet1) { //(6)
		return stepBuilderFactory.get("step1")
					.tasklet(tasklet1)
					.build();
	}
	@Bean
	@StepScope //(7)
	public Tasklet tasklet1( //(6)
			@Value("#{jobParameters['id']}") Long id, //(8)
			@Value("#{jobParameters['name']}") String name,
			@Value("#{jobParameters['reqDate']}") Date reqDate
			) {
        //(9)
		MethodInvokingTaskletAdapter tasklet = new MethodInvokingTaskletAdapter();
		tasklet.setTargetObject(service);
		tasklet.setTargetMethod("execute");
		tasklet.setArguments(new Object[] {id, name, reqDate});
		return tasklet;
	}
}
| (1) | Indicates that it is a Spring Batch setting@EnableBatchProcessingTo@ConfigurationGrant with. | 
| (2) | Some beans are included in the context by default and can be autowired. | 
| (3) | Job execution environment settings. | 
| (4) | Number of concurrent executions with CorePoolSize(Number of threads)To set. If it exceeds this, it will wait in the queue and will be executed sequentially as soon as threads become available. | 
| (5) | I have to initialize"java.lang.IllegalStateException: ThreadPoolTaskExecutor not initialized"Exception occurs. | 
| (6) | Job, Step,Tasklet settings. | 
| (7) | If you set run-time parameters, you cannot create beans at startup.@StepScopeTo change the timing of bean creation. | 
| (8) | Settings to retrieve objects from jobParameters. | 
| (9) | Run with Invoke. | 
@Service
public class MyServletService {
	@Autowired
	private JobLauncher jobLauncher1;
	@Autowired
	private Job job;
	public void request(Long id, String name) throws JobExecutionException {
		// (10)
		JobParameters params = new JobParametersBuilder()
								.addLong("id", id)
								.addString("name", name)
								.addDate("reqDate", Calendar.getInstance().getTime())
								.toJobParameters();
		jobLauncher1.run(job, params);
	}
}
| (10) | The parameters to be passed to Job are collectively passed to JobParameters. | 
Add settings to application.yml.
application.yml
spring:
  datasource: # (11)
    url: jdbc:postgresql://localhost:5432/kurukuruz1
    username: u-s-e-r
    password: p-a-s-s
    driver-class-name: org.postgresql.Driver
  batch:
    initialize-schema: always # (12)
    job:
      enabled: false # (13)
| (11) | Set the connection information of the DB to be used. | 
| (12) | Setting to execute DDL of the table used by Spring Batch. By defaultembeddedSo, when using PostgreSQL, DDL is not executed, so change it explicitly. | 
| (13) | By defaulttrueJob is executed when Spring is started, but it cannot be executed at startup because there are no Job Parameters at startup. Therefore, change it explicitly. | 
Spring Batch re-introduction --I tried to organize how to use it myself --Qiita BLOG.IK.AM --Mistakenly misunderstood ThreadPoolTaskExecutor settings Spring Batch sample code (Java / Gradle) --Qoosky [Spring Batch] Do not execute Job automatically Control method when jobs other than the specified Job are automatically executed --Qiita Kojioniruku-Hello World in Spring Boot Batch
Recommended Posts