#11 spring batch: JobRepository
This commit is contained in:
@@ -15,7 +15,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
각 Step 의 StepExecution 에 저장되고 Step 간 공유 안됨
|
||||
각 Job 의 JobExecution 에 저장되고 Job 간 공유 안됨, 해당 Job 의 Step 간 서로 공유
|
||||
*/
|
||||
@Configuration
|
||||
//@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class ExecutionContextConfiguration {
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package io.springbatch.basic.jobrepository;
|
||||
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
|
||||
import org.springframework.boot.autoconfigure.batch.BasicBatchConfigurer;
|
||||
import org.springframework.boot.autoconfigure.batch.BatchProperties;
|
||||
import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
//@Configuration
|
||||
public class CustomBatchConfigurer extends BasicBatchConfigurer {
|
||||
|
||||
private final DataSource dataSource;
|
||||
|
||||
protected CustomBatchConfigurer(BatchProperties properties, DataSource dataSource, TransactionManagerCustomizers transactionManagerCustomizers) {
|
||||
super(properties, dataSource, transactionManagerCustomizers);
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JobRepository createJobRepository() throws Exception {
|
||||
|
||||
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
|
||||
factory.setDataSource(dataSource);
|
||||
factory.setTransactionManager(getTransactionManager());
|
||||
factory.setIsolationLevelForCreate("ISOLATION_READ_COMMITTED");
|
||||
// factory.setTablePrefix("TEST_");
|
||||
|
||||
return factory.getObject();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package io.springbatch.basic.jobrepository;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobExecutionListener;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
|
||||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/*
|
||||
JobRepository
|
||||
|
||||
배치 작업 중의 정보를 저장하는 저장소
|
||||
|
||||
@EnableBatchProcessing 어노테이션 선언시 JobRepository 자동으로 빈 등록
|
||||
|
||||
JDBC 방식 - JobRepositoryFactoryBean
|
||||
- 트랜잭션 처리
|
||||
- SERIALIZABLE 격리수준
|
||||
In Memory 방식 - MapJopRepositoryFactoryBean
|
||||
*/
|
||||
//@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class JobRepositoryConfiguration {
|
||||
|
||||
private final JobBuilderFactory jobBuilderFactory;
|
||||
private final StepBuilderFactory stepBuilderFactory;
|
||||
private final JobExecutionListener jobRepositoryListener;
|
||||
|
||||
@Bean
|
||||
public Job batchJob() {
|
||||
return jobBuilderFactory.get("batchJob")
|
||||
.start(step1())
|
||||
.next(step2())
|
||||
.listener(jobRepositoryListener)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step step1() {
|
||||
return stepBuilderFactory.get("step1")
|
||||
.tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("step1 was executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step step2() {
|
||||
return stepBuilderFactory.get("step2")
|
||||
.tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("step2 was executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package io.springbatch.basic.jobrepository;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.*;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
//@Component
|
||||
@RequiredArgsConstructor
|
||||
public class JobRepositoryListener implements JobExecutionListener {
|
||||
|
||||
private final JobRepository jobRepository;
|
||||
|
||||
@Override
|
||||
public void beforeJob(JobExecution jobExecution) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterJob(JobExecution jobExecution) {
|
||||
String jobName = jobExecution.getJobInstance().getJobName();
|
||||
|
||||
JobParameters jobParameters = new JobParametersBuilder()
|
||||
.addString("requestDate", "20220605").toJobParameters();
|
||||
|
||||
JobExecution lastJobExecution = jobRepository.getLastJobExecution(jobName, jobParameters);
|
||||
if (lastJobExecution != null) {
|
||||
for (StepExecution stepExecution : lastJobExecution.getStepExecutions()) {
|
||||
BatchStatus status = stepExecution.getStatus();
|
||||
ExitStatus exitStatus = stepExecution.getExitStatus();
|
||||
String stepName = stepExecution.getStepName();
|
||||
System.out.println("status = " + status);
|
||||
System.out.println("stepExecution = " + stepExecution);
|
||||
System.out.println("stepName = " + stepName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user