[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가 되어 있는 것을 확인할 수 있다.