[Spring Batch] 배치 처리2 : 테이블 조건
2024. 9. 18. 19:19ㆍ프로그래밍(Backend)/Spring Batch
WinEntity의 win의 수가 10이상일 경우 reward를 지급해주는 secondJob 구현
테이블 : WinEntity
Entity 정의
@Entity
@Data
public class WinEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private Long win;
private Boolean reward;
}
Repository 정의
@Repository
public interface WinRepository extends JpaRepository<WinEntity, Long> {
Page<WinEntity> findByWinGreaterThanEqual(Long win, Pageable pageable);
}
Job 정의
@Configuration
@RequiredArgsConstructor
public class SecondBatch {
private final JobRepository jobRepository;
private final PlatformTransactionManager platformTransactionManager;
private final WinRepository winRepository;
@Bean
public Job secondJob(){
return new JobBuilder("secondJob", jobRepository)
.start(secondStep())
.build();
}
}
Step
@Bean
public Step secondStep(){
return new StepBuilder("secondStep", jobRepository)
.<WinEntity, WinEntity>chunk(10, platformTransactionManager)
.reader(winReader())
.processor(trueProcessor())
.writer(winWriter())
.build();
}
Step에서 chunk(청크)는 한번에 작업할 단위를 의미한다.
Read
@Bean
public RepositoryItemReader<WinEntity> winReader(){
return new RepositoryItemReaderBuilder<WinEntity>()
.name("winReader")
.pageSize(10)
.methodName("findByWinGreaterThanEqual")
.arguments(Collections.singletonList(10L))
.repository(winRepository)
.sorts(Map.of("id", Sort.Direction.ASC))
.build();
}
Process
@Bean
public ItemProcessor<WinEntity, WinEntity> trueProcessor(){
return item -> {
item.setReward(true);
return item;
};
}
Writer
@Bean
public RepositoryItemWriter<WinEntity> winWriter(){
return new RepositoryItemWriterBuilder<WinEntity>()
.repository(winRepository)
.methodName("save")
.build();
}
secondJop 전체 코드
@Configuration
@RequiredArgsConstructor
public class SecondBatch {
private final JobRepository jobRepository;
private final PlatformTransactionManager platformTransactionManager;
private final WinRepository winRepository;
@Bean
public Job secondJob(){
return new JobBuilder("secondJob", jobRepository)
.start(secondStep())
.build();
}
@Bean
public Step secondStep(){
return new StepBuilder("secondStep", jobRepository)
.<WinEntity, WinEntity>chunk(10, platformTransactionManager)
.reader(winReader())
.processor(trueProcessor())
.writer(winWriter())
.build();
}
@Bean
public RepositoryItemReader<WinEntity> winReader(){
return new RepositoryItemReaderBuilder<WinEntity>()
.name("winReader")
.pageSize(10)
.methodName("findByWinGreaterThanEqual")
.arguments(Collections.singletonList(10L))
.repository(winRepository)
.sorts(Map.of("id", Sort.Direction.ASC))
.build();
}
@Bean
public ItemProcessor<WinEntity, WinEntity> trueProcessor(){
return item -> {
item.setReward(true);
return item;
};
}
@Bean
public RepositoryItemWriter<WinEntity> winWriter(){
return new RepositoryItemWriterBuilder<WinEntity>()
.repository(winRepository)
.methodName("save")
.build();
}
}
실행 결과
controller를 통해 실행
@GetMapping("/second")
public String secondApi(@RequestParam String value) throws Exception {
JobParameters jobParameters = new JobParametersBuilder()
.addString("date",value)
.toJobParameters();
jobLauncher.run(jobRegistry.getJob("secondJob"), jobParameters);
return "ok";
}
localhost:8080/second?value=a를 통해 get 호출
secondJop을 수행한다.
win 컬럼을 확인해서 10 이상일 경우 reward가 true가 되어 있는 것을 확인할 수 있다.
'프로그래밍(Backend) > Spring Batch' 카테고리의 다른 글
[Spring Batch] 배치 처리 : 엑셀 to 테이블 (1) | 2024.09.26 |
---|---|
[Spring Batch] 배치 처리 : 실행 및 스케쥴 (0) | 2024.09.18 |
[Spring Batch] batch process : 테이블 to 테이블 (0) | 2024.09.17 |
[Spring Batch] MetaData 테이블 (2) | 2024.09.16 |
[Spring Batch] DB 연결 Config 클래스 작성 (1) | 2024.09.16 |