Akashic Records

Spring Batch DataBase 설정 본문

Spring.io

Spring Batch DataBase 설정

Andrew's Akashic Records 2023. 4. 21. 15:26
728x90

DataSource, entityManagerFactory, transactionManager 및 JPA를 설정하는 코드 예제입니다.

@Configuration
@EnableBatchProcessing
@EnableScheduling
@EnableTransactionManagement
public class BatchConfiguration {

    // 1. Spring Batch & Quartz Configuration
    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.batch-quartz")
    public DataSourceProperties batchQuartzDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    public DataSource batchQuartzDataSource() {
        return batchQuartzDataSourceProperties().initializeDataSourceBuilder().build();
    }

    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean batchQuartzEntityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(batchQuartzDataSource());
        em.setPackagesToScan("com.example.batchquartz");
        em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        return em;
    }

    @Bean
    @Primary
    public PlatformTransactionManager batchQuartzTransactionManager(EntityManagerFactory batchQuartzEntityManagerFactory) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(batchQuartzEntityManagerFactory);
        return transactionManager;
    }

    // 2. IMS Configuration
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.ims")
    public DataSourceProperties imsDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    public DataSource imsDataSource() {
        return imsDataSourceProperties().initializeDataSourceBuilder().build();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean imsEntityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(imsDataSource());
        em.setPackagesToScan("com.example.ims");
        em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        return em;
    }

    @Bean
    public PlatformTransactionManager imsTransactionManager(EntityManagerFactory imsEntityManagerFactory) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(imsEntityManagerFactory);
        return transactionManager;
    }

    // 3. Push Configuration
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.push")
    public DataSourceProperties pushDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    public DataSource pushDataSource() {
        return pushDataSourceProperties().initializeDataSourceBuilder().build();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean pushEntityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(pushDataSource());
        em.setPackagesToScan("com.example.push");
        em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        return em;
    }

    @Bean
    public PlatformTransactionManager pushTransactionManager(EntityManagerFactory pushEntityManagerFactory) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(pushEntityManagerFactory);
        return transactionManager;
    }
}

 

이제 Spring Batch 작업에서 여러 DataSource 및 EntityManagerFactory를 사용할 수 있습니다. pushJob은 ims 데이터를 JPA로 읽어 push 테이블에 저장하는 기능을 포함해야 합니다. 이를 위해 pushJob을 생성하고 읽기 및 쓰기 작업을 실행할 때 ims와 push의 EntityManagerFactory를 사용하는 Reader 및 Writer를 정의해야 합니다. 이를 구현하려면 Spring Batch의 ItemReader 및 ItemWriter를 사용하여 pushJob을 구현할 수 있습니다. 여기서는 JPA를 사용하여 ims 데이터를 읽고 push 테이블에 저장하는 예제를 제공합니다.

@Configuration
public class PushJobConfiguration {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    @Qualifier("imsEntityManagerFactory")
    private EntityManagerFactory imsEntityManagerFactory;

    @Autowired
    @Qualifier("pushEntityManagerFactory")
    private EntityManagerFactory pushEntityManagerFactory;

    @Bean
    public Job pushJob() {
        return jobBuilderFactory.get("pushJob")
                .start(pushStep())
                .build();
    }

    @Bean
    public Step pushStep() {
        return stepBuilderFactory.get("pushStep")
                .<ImsEntity, PushEntity>chunk(100)
                .reader(imsReader())
                .processor(imsToPushProcessor())
                .writer(pushWriter())
                .build();
    }

    @Bean
    public ItemReader<ImsEntity> imsReader() {
        JpaPagingItemReader<ImsEntity> reader = new JpaPagingItemReader<>();
        reader.setEntityManagerFactory(imsEntityManagerFactory);
        reader.setQueryString("SELECT i FROM ImsEntity i");

        return reader;
    }

    @Bean
    public ItemProcessor<ImsEntity, PushEntity> imsToPushProcessor() {
        return imsEntity -> {
            // 데이터 변환 로직 작성
            PushEntity pushEntity = new PushEntity();
            // ... (imsEntity 데이터를 pushEntity 데이터로 변환)
            return pushEntity;
        };
    }

    @Bean
    public ItemWriter<PushEntity> pushWriter() {
        JpaItemWriter<PushEntity> writer = new JpaItemWriter<>();
        writer.setEntityManagerFactory(pushEntityManagerFactory);
        return writer;
    }
}

 

이 예제에서는 `ImsEntity`와 `PushEntity` 클래스가 각각 ims와 push 데이터를 나타낸다고 가정합니다. `ImsEntity`를 읽어서 `PushEntity`로 변환하고 이를 push 테이블에 저장합니다. 이 작업을 수행하기 위해, `JpaPagingItemReader`를 사용하여 ims 데이터를 읽고, `ItemProcessor`를 사용하여 데이터를 변환하며, `JpaItemWriter`를 사용하여 push 테이블에 데이터를 저장합니다. 이 구성을 사용하면, Spring Batch는 자동으로 여러 데이터 소스와 EntityManagerFactory를 관리하고 트랜잭션을 처리합니다.

 
728x90
Comments