Spring Batch 5.0

์ตœ์ค€ํ˜ธยท2023๋…„ 4์›” 20์ผ
0

Spring

๋ชฉ๋ก ๋ณด๊ธฐ
46/48
post-thumbnail
post-custom-banner

๐Ÿ“— Spring Batch 5.0 ์ด๋ž€?

Spring Batch๊ฐ€ 4.x ๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜๋‹ค๊ฐ€ Spring Boot 3.0๊ณผ ๋ฒ„์ „์„ ๋งž์ถ˜๊ฑด์ง€ 5.0์œผ๋กœ ์—…๋ฐ์ดํŠธ๋ฅผ ํ–ˆ๋‹ค.
๊ธฐ์กด์˜ ๋ฐฉ์‹๋Œ€๋กœ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด 4.x ๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ํ•˜์ง€๋งŒ ํ† ์ดํ”„๋กœ์ ํŠธ๋Š” ์›๋ž˜ ์ƒˆ๋กœ์šด๊ฑธ ๋„์ „ํ•ด ๋ณด๋Š” ์šฉ๋„๋‹ˆ๊นŒ 5.0์„ ์‚ฌ์šฉํ•ด๋ณด์ž!

๐Ÿ“„ ์†Œ์Šค ์„ค์ •

โœ… application.yml

spring:
  profiles:
    active: ${server.profile:local}
  batch:
    job:
      name: ${job.name:NONE}
logging:
  pattern:
    console: "[%d{HH:mm:ss.SSS}][%-5level][%logger.%method:line%line] - %msg%n"
  level:
    root: info
    com.juno.hyugibatch: debug
--- # local
spring:
  config:
    activate:
      on-profile: local
  h2:
    console:
      enabled: true
      settings:
        web-allow-others: true
      path: /h2-console
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:hyugi
    username: sa
    password:

โœ… DB ์„ค์ •

@Configuration
public class DataSourceConfig {
    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;

    @Bean
    public DataSource getDataSource() {
        DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName(driverClassName);
        dataSourceBuilder.url(url);
        dataSourceBuilder.username(username);
        dataSourceBuilder.password(password);
        return dataSourceBuilder.build();
    }

    @Bean
    public PlatformTransactionManager transactionManagerApp(@Qualifier("getDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

โœ… Job ๋งŒ๋“ค๊ธฐ

package com.juno.hyugibatch.job;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

@Slf4j
@Configuration
public class TestJob {

    @Bean
    public Job testSimpleJob(JobRepository jobRepository, Step testStep){
        return new JobBuilder("testSimpleJob", jobRepository)
                .start(testStep)
                .build();

    }

    @Bean
    public Step testStep(JobRepository jobRepository, Tasklet testTasklet, PlatformTransactionManager platformTransactionManager){
        return new StepBuilder("testStep", jobRepository)
                .tasklet(testTasklet, platformTransactionManager).build();
    }

    @Bean
    public Tasklet testTasklet(){
        return ((contribution, chunkContext) -> {
            System.out.println("ํ…Œ์ŠคํŠธ1");

            return RepeatStatus.FINISHED;
        });
    }
}

๐Ÿ“„ Job ์—ฌ๋Ÿฌ๊ฐœ ์„ค์ •ํ•˜๊ธฐ

โœ… 2๋ฒˆ์งธ Job ๋งŒ๋“ค๊ธฐ

package com.juno.hyugibatch.job;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

@Slf4j
@Configuration
public class TestJob2 {

    @Bean
    public Job testSimpleJob2(JobRepository jobRepository, Step testStep2){
        return new JobBuilder("testSimpleJob2", jobRepository)
                .start(testStep2)
                .build();

    }

    @Bean
    public Step testStep2(JobRepository jobRepository, Tasklet testTasklet2, PlatformTransactionManager platformTransactionManager){
        return new StepBuilder("testStep2", jobRepository)
                .tasklet(testTasklet2, platformTransactionManager).build();
    }

    @Bean
    public Tasklet testTasklet2(){
        return ((contribution, chunkContext) -> {
            System.out.println("ํ…Œ์ŠคํŠธ2");

            return RepeatStatus.FINISHED;
        });
    }
}

โœ… multiple job yml ์„ค์ •

spring:
  batch:
    job:
      name: ${job.name:NONE}

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•ด์ฃผ๋ฉด

java -jar --job.name=[job ์ด๋ฆ„]

๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด job ์ด๋ฆ„์— ๋”ฐ๋ผ ์‹คํ–‰ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

profile
ํ•ด๋‹น ์ฃผ์†Œ๋กœ ์ด์ „ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. https://ililil9482.tistory.com
post-custom-banner

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 9์›” 12์ผ

์•ˆ๋…•ํ•˜์„ธ์š” ์ค€ํ˜ธ๋‹˜
์ €๋Š” ์ด๊ฑธ ๋ด๋„ ๋ชจ๋ฅด๊ฒ ๋Š”๋ฐ ... ํ˜น์‹œ ๊ฐœ์ธ ์ž‘์—…ํ•˜์‹  github ์ฃผ์†Œ ๊ณต๊ฐœ ๊ฐ€๋Šฅํ• ๊นŒ์š”?

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ