Giriş
İşlerin sonucunu görmek için şöyle yaparız.
incrementer metodu
Açıklaması şöyle. Genelde RunIdIncrementer kullanılır
Şöyle yaparız.
Şöyle yaparız.
- Flow tipinden parametre verilirse JobFlowBuilder veya
- Step tipinden parametre verilirse SimpleJobBuilder nesnesi döner.
Örnek
Step parametresi verildiği için SimpleJobBuilder nesnesi döner. Şöyle yaparız.
İşlerin sonucunu görmek için şöyle yaparız.
SELECT * FROM BATCH_STEP_EXECUTION WHERE (JOB_EXECUTION_ID = 54;
Restartable
Açıklaması şöyle
When job is running and we try to start it again that is consider as restartable.
Kullanım
Bu işin adımları eğer birden fazla Step'ten oluşuyorsa flow() ile tanımlanır. JobFlowBuilder nesnesi döner.
incrementer metodu
Açıklaması şöyle. Genelde RunIdIncrementer kullanılır
The contract of JobParametersIncrementer is that, given a JobParameters object, it will return the 'next' JobParameters object by incrementing any necessary values it may contain. This strategy is useful because the framework has no way of knowing what changes to the JobParameters make it the 'next' instance. For example, if the only value in JobParameters is a date, and the next instance should be created, should that value be incremented by one day? Or one week (if the job is weekly for instance)? The same can be said for any numerical values that help to identify the Job ...Örnek
Şöyle yaparız.
public Job customJob(String someParam) throws Exception {
return jobBuilderFactory.get("personProcessor")
.incrementer(new RunIdIncrementer())
.listener(listener())
.flow(personPorcessStep(someParam))
.end()
.build();
}
ÖrnekŞöyle yaparız.
@Bean
public Job exportUserJob() {
return jobBuilderFactory.get("exportUserJob2")
.incrementer(new RunIdIncrementer())
.flow(step1())
.end()
.build();
}
listener metodu
JobExecutionListener yazısına taşıdım
next metodu
start() ile başlatılan Step'ten sonra çalışacak Step belirtilir.
Örnek
Şöyle yaparız. Burada 3 tane step bağlanıyor.
@Bean
public Job job(JobCompletionNotificationListener listener,
Step stepNewsReport,
Step stepRealNewsReport,
Step stepFakeNewsReport){
return jobBuilderFactory
.get("job")
.incrementer(new RunIdIncrementer())
.listener(listener)
.start(stepNewsReport)
.next(stepRealNewsReport)
.next(stepFakeNewsReport)
.build();
}
Örnek
Şöyle yaparız
@Bean
public Job customerDataProcessingJob() {
return jobBuilderFactory.get("CustomerDataProcessingJob")
.start(readFileStep())
.next(validateDataStep())
.next(enrichDataStep())
.next(writeDataStep())
.build();
}
split metoud
Açıklaması şöyle
Spring Batch provides built-in support for scaling and parallel processing. Developers can configure a job to execute in parallel on multiple threads or machines, allowing for faster and more efficient batch processing.
Şöyle yaparız
@Configuration
@EnableBatchProcessing
public class TaskExecutorConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(4);
executor.setMaxPoolSize(4);
executor.setThreadNamePrefix("batch-");
executor.initialize();
return executor;
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.tasklet((contribution, chunkContext) -> {
System.out.println("Step 1 running in thread: " + Thread.currentThread().getName());
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Step step2() {
return stepBuilderFactory.get("step2")
.tasklet((contribution, chunkContext) -> {
System.out.println("Step 2 running in thread: " + Thread.currentThread().getName());
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Step step3() {
return stepBuilderFactory.get("step3")
.tasklet((contribution, chunkContext) -> {
System.out.println("Step 3 running in thread: " + Thread.currentThread().getName());
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Flow flow2() {
return new FlowBuilder<Flow>("flow2")
.start(step2())
.build();
}
@Bean
public Flow flow3() {
return new FlowBuilder<Flow>("flow3")
.start(step3())
.build();
}
@Bean
public Job parallelJob() {
return jobBuilderFactory.get("parallelJob")
.start(step1())
.split(taskExecutor())
.add(flow2(), flow2())
.end()
.build();
}
}
Açıklaması şöyle
In the example, we have defined three steps (`step1()`, `step2()`, and `step3()`) that simply print a message to the console indicating the thread in which they are running. We have also defined a `TaskExecutor` bean that creates a `ThreadPoolTaskExecutor` with a core pool size of 4 threads and a maximum pool size of 4 threads.The `parallelJob()` function configures a Spring Batch job to execute in parallel on multiple threads using the `split()` method. The `split()` method takes a `TaskExecutor` as an argument, which tells Spring Batch to execute the next steps (`step2()` and `step3()`) in parallel using the specified executor. Finally, the `end()` method is called to terminate the parallel execution and end the job.When we run this job, we should see the following output in the console:Step 1 running in thread: batch-1Step 2 running in thread: batch-2Step 3 running in thread: batch-3This indicates that each step is running in a separate thread, allowing for parallel execution and improved performance.
start metodu
- Step tipinden parametre verilirse SimpleJobBuilder nesnesi döner.
Örnek
Step parametresi verildiği için SimpleJobBuilder nesnesi döner. Şöyle yaparız.
@Configuration
public class JobConfiguration {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Bean
public JdbcPagingItemReader<Customer> customerPagingItemReader(){
...
}
@Bean
public StaxEventItemWriter<Customer> customerItemWriter() throws Exception{
...
}
@Bean
public Step step1() throws Exception {
...
}
@Bean
public Job job() throws Exception {
return jobBuilderFactory.get("job")
.start(step1())
.build();
}
}
Örnek
Şöyle yaparız
@SpringBootApplication
@EnableBatchProcessing
@Slf4j
public class DataBatchApplication implements CommandLineRunner {
@Autowired
JobBuilderFactory jobBuilderFactory;
@Autowired
StepBuilderFactory stepBuilderFactory;
@Bean(name = "ExcelFileProcessingJob")
public Job job () throws Exception {
return this.jobBuilderFactory.get("ExcelFileProcessingJob")
.start(fileProcessingStep())
.build();
}
@Bean
public Step fileProcessingStep() {
return this.stepBuilderFactory.get("readFileStep")
.<FileDTO, FileDTO>chunk(1)
.reader(fileReader(null))
.processor(fileProcessor())
.writer(flatFileWriter())
.listener(myStepListerner())
.build();
}
}
Hiç yorum yok:
Yorum Gönder