IT/development

[springBoot] spring batch scheduler jpaRead/Writer (feat. DB)

알 수 없는 사용자 2023. 11. 12. 19:16
반응형

목차

    아래 포스팅에서 이어진 내용입니다.

     

    [springBoot] spring scheduler simpleBatch (feat. scheduler)

    목차 Batch1 🙂 package com.dev.lsy.springbatchlog.batch; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; import org.springframework.batch.co

    yaga.tistory.com


    일정시간에 A 테이블의 데이터를 읽어서 가공 후 B 테이블에 insert하는 배치 예제

    JpaBatch1 😄

    package com.test.lsy.springbatchtest2.batch;
    
    import com.test.lsy.springbatchtest2.domain.Dept;
    import com.test.lsy.springbatchtest2.domain.Dept2;
    import lombok.RequiredArgsConstructor;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.batch.core.Job;
    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.item.ItemProcessor;
    import org.springframework.batch.item.database.JpaItemWriter;
    import org.springframework.batch.item.database.JpaPagingItemReader;
    import org.springframework.batch.item.database.builder.JpaPagingItemReaderBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import javax.persistence.EntityManagerFactory;
    
    @Slf4j
    @RequiredArgsConstructor
    @Configuration
    public class JpaBatch1 {
    
        private final JobBuilderFactory jobBuilderFactory;
        private final StepBuilderFactory stepBuilderFactory;
        private final EntityManagerFactory entityManagerFactory;
    
        private int chunkSize = 100;
    
        //step을 호출하는 job 생성
        @Bean
        public Job jpaJob() {
            return jobBuilderFactory.get("jpaPageJob2")
                    .start(jpaStep())
                    .build();
        }
    
        //배치의 단위가 되는 step 작성
        @Bean
        public Step jpaStep() {
            return stepBuilderFactory.get("jpaStep")
                    .<Dept, Dept2>chunk(chunkSize)
                    .reader(jpaReader())
                    .processor(jpaPageJob2_processor())
                    .writer(jpaWriter())
                    .build();
        }
    
        //reader
        @Bean
        public JpaPagingItemReader<Dept> jpaReader() {
            return new JpaPagingItemReaderBuilder<Dept>()
                    .name("jpaReader")
                    .entityManagerFactory(entityManagerFactory)
                    .pageSize(chunkSize)
                    .queryString("SELECT d FROM Dept d order by dept_no asc")
                    .build();
        }
    
        //가공
        private ItemProcessor<Dept, Dept2> jpaPageJob2_processor() {
            return dept -> {
                // 가공한 데이터를 의미(접두어로 "NEW_"를 붙임)
                return new Dept2(dept.getDeptNo(), "NEW_" + dept.getDName(), "NEW_" + dept.getLoc());
            };
        }
    
        //writer
        //dept2 테이블에 저장하는 역할
        @Bean
        public JpaItemWriter<Dept2> jpaWriter() {
            JpaItemWriter<Dept2> jpaItemWriter = new JpaItemWriter<>();
            jpaItemWriter.setEntityManagerFactory(entityManagerFactory);
            return jpaItemWriter;
        }
    }

    Dept 🙂

    package com.test.lsy.springbatchtest2.domain;
    
    import lombok.*;
    
    import javax.persistence.Entity;
    import javax.persistence.Id;
    
    //이건 엔티티
    @Getter
    @Setter
    @ToString
    @AllArgsConstructor
    @NoArgsConstructor
    @Entity
    public class Dept {
    
        @Id
        Integer deptNo;
        String dName;
        String loc;
    }

    DeptRepository 🤗

    package com.test.lsy.springbatchtest2.repository;
    
    import com.test.lsy.springbatchtest2.domain.Dept;
    import org.springframework.data.repository.CrudRepository;
    
    public interface DeptRepository extends CrudRepository<Dept, Long> {
    }

    Scheduler 😏

    package com.test.lsy.springbatchtest2.scheduler;
    
    import lombok.RequiredArgsConstructor;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.batch.core.Job;
    import org.springframework.batch.core.JobParametersBuilder;
    import org.springframework.batch.core.JobParametersInvalidException;
    import org.springframework.batch.core.launch.JobLauncher;
    import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
    import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
    import org.springframework.batch.core.repository.JobRestartException;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    import java.time.LocalDateTime;
    
    @Component
    @RequiredArgsConstructor
    @EnableScheduling
    @Slf4j
    public class Scheduler {
    
        private final JobLauncher jobLauncher;
        private final Job jpaJob;
    	//스케줄은 원하는데로 수정
        @Scheduled(cron = "0 10 19 * * *")
        public void schedule() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
            jobLauncher.run(jpaJob, new JobParametersBuilder()
                    .addString("date", LocalDateTime.now().toString())
                    .toJobParameters()
            );
        }
    }

    개인 스터디 기록을 메모하는 공간이라 틀린점이 있을 수 있습니다.

    틀린 점 있을 경우 댓글 부탁드립니다.


    다음 내용

     

    [springBoot] spring batch scheduler modularization (feat. DB)

    목차 아래 포스팅에서 이어진 내용입니다. [springBoot] spring batch scheduler jpaRead/Writer (feat. DB) 목차 아래 포스팅에서 이어진 내용입니다. [springBoot] spring scheduler simpleBatch (feat. scheduler) 목차 Batch1 🙂 p

    yaga.tistory.com

    반응형