This article is a direct description of what I did when creating a batch process in Eclipse, first trying to create a simple framework.
The image of batch processing itself is that processing time is long (inputting a large amount of data, processing, and outputting) as a separate process. So, I didn't have the idea of jobs or tasks, and I imagined something like executing a long process in the main function. However, when actually using the functions of Spring Framework, it was necessary to think about jobs and tasks. So, first, execute the job from the main function, execute the task from the job, and make up to the point where the exit code is set.
First, let's create the following batch process. ・ BatchTestApplication (main) => Start BatchTestJob. (Using Spring Framework) ・ BatchTetJob => Start BatchTestTasklet. ・ BatchTestTasklet => Output "BatchTestTasklet Start OK" (set the exit code to 1)
Point: Setting the exit code in batch processing is a bit complicated. (1) Implement the ExitCodeGenerator interface in the BatchTestTasklet class. (2) Override the getExitCode method and return the return value in it. (3) The return value of (2) is set in the return value of SpringApplication.exit.
package com.example.demo;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext;
@SpringBootApplication public class BatchTestAppApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(BatchTestAppApplication.class, args);
int iRet = SpringApplication.exit(context);
System.out.println(iRet);
System.exit(iRet);
}
}
package com.example.demo;
import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.core.launch.support.RunIdIncrementer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component;
@Component @EnableBatchProcessing public class BatchTestJob {
@Autowired
private JobBuilderFactory jobFactory;
@Autowired
private StepBuilderFactory stepFactory;
@Autowired
private BatchTestTasklet batchTestTasklet;
@Bean
public Step step1() {
return stepFactory
.get("step1")
.tasklet(batchTestTasklet)
.build();
}
@Bean
public Job job(Step step1) {
return jobFactory
.get("job")
.incrementer(new RunIdIncrementer())
.start(step1)
.build();
}
}
package com.example.demo;
import org.springframework.batch.core.StepContribution; import org.springframework.batch.core.scope.context.ChunkContext; import org.springframework.batch.core.step.tasklet.Tasklet; import org.springframework.batch.repeat.RepeatStatus; import org.springframework.boot.ExitCodeGenerator; import org.springframework.stereotype.Component;
@Component public class BatchTestTasklet implements Tasklet, ExitCodeGenerator {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("BatchTestTasklet Start OK");
return RepeatStatus.FINISHED;
}
@Override
public int getExitCode() {
return 1;
}
}
Recommended Posts