[Kotlin + SpringBatch5] SpringBatch5의 다양한 파라미터 지원 - Job 생성해서 테스트 및 메타테이블 확인, identifying 용도 확인

반응형
728x90
반응형

SpringBatch5의 다양한 파라미터 지원

https://devfunny.tistory.com/930

 

SpringBatch5 변경사항 정리 (vs SpringBatch4)

SpringBatch 5.0 이전 SpringBatch 공부할때 SpringBatch 4.0 버전이였다. 최근, SpringBatch 복습을 위해 새로 프로젝트를 셋팅하면서 업데이트된 SpringBatch 5.0을 선택했는데, 생각보다 4.0 버전에 달라진 사항들

devfunny.tistory.com

 

위 SpringBatch4 vs SpringBatch5 포스팅에서도 설명했지만, SpringBatch5 부터 다양한 파라미터를 사용할수 있게 되었다.

 

기존 SpringBatch4

Long, Double, Sring, Date 타입의 파라미터만 가능

 

다양한 파라미터를 지원하게 되면서, 선언 방식도 달라지고 메타테이블(BATCH_JOB_EXECUTION_PARAMS)의 컬럼 정보도 변경되었으므로 직접 Job을 생성해서 어떻게 데이터가 들어가게되는지 확인해보자.

 

 

예제코드

JobParameterConfiguration.kt
package com.batch

import lombok.RequiredArgsConstructor
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.launch.support.RunIdIncrementer
import org.springframework.batch.core.repository.JobRepository
import org.springframework.batch.core.step.builder.StepBuilder
import org.springframework.batch.repeat.RepeatStatus
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.transaction.PlatformTransactionManager

@Configuration
@RequiredArgsConstructor
class JobParameterConfiguration {
    @Bean
    fun jobParameterTestJob(jobRepository: JobRepository, platformTransactionManager: PlatformTransactionManager): Job {
        return JobBuilder("jobParameterTestJob", jobRepository) /* step start */
            .start(jobParameterTestStep1(jobRepository, platformTransactionManager))
            .next(jobParameterTestStep2(jobRepository, platformTransactionManager))
            .incrementer(RunIdIncrementer())
            .build()
    }

    @Bean
    fun jobParameterTestStep1(jobRepository: JobRepository, platformTransactionManager: PlatformTransactionManager): Step {
        return StepBuilder("jobParameterTestStep1", jobRepository)
            .tasklet({ contribution, chunkContext ->
                println("jobParameterTestStep1 was executed")

                /* CASE1. contribution */
                val jobParameters = contribution.stepExecution
                    .jobExecution
                    .jobParameters

                val name = jobParameters.getString("name")
                val seq = jobParameters.getLong("seq")
                val doubleParam = jobParameters.getDouble("double")

                println("$name, $seq, $doubleParam")

                /* CASE2. chunkContext */
                val jobParameters1 = chunkContext.stepContext.getJobParameters()
                val name2 = jobParameters1["name"] as String

                RepeatStatus.FINISHED
            }, platformTransactionManager)
            .build()
    }

    @Bean
    fun jobParameterTestStep2(jobRepository: JobRepository, platformTransactionManager: PlatformTransactionManager): Step {
        return StepBuilder("jobParameterTestStep2", jobRepository)
            .tasklet({ contribution, chunkContext ->
                println("jobParameterTestStep2 was executed")
                RepeatStatus.FINISHED
            }, platformTransactionManager)
            .build()
    }
}

 

실행 (Program arguments)
--job.name=jobParameterTestJob name=seohae seq=1,java.lang.Long double=1.5,java.lang.Double

 

1) jobParameterTestStep1()에서 파라미터를 꺼내는 방법

▶ CASE1. contribution 사용

val jobParameters = contribution.stepExecution
                    .jobExecution
                    .jobParameters

val name = jobParameters.getString("name")

 

▶ CASE2. chunkContext 사용

val jobParameters1 = chunkContext.stepContext.getJobParameters()
val name2 = jobParameters1["name"] as String

 

2) JobLauncherApplicationRunner.java의 execute() 메서드 확인

name, double, seq 파라미터가 들어온 것을 확인할 수 있다.

실제로 identifying은 보내지 않았는데 true로 설정되었다. default값을 확인할 수 있었다.

 

참고로 identifying의 default 값이 true인 것은 아래의 문서에 나와있다.

https://spring.io/blog/2022/11/24/spring-batch-5-0-goes-ga

 

Spring Batch 5.0 Goes GA!

It is finally here! Spring Batch 5.0 is now generally available from Maven Central. Spring Batch 5 is the culmination of two years of work, including dozens of improvements, features, and bug fixes by more than 50 contributors! On Behalf of the team, I wou

spring.io

Note how the identification flag is optional and defaults to true.

 

3) Step 안의 name, seq, doubleParam 변수 값 확인

 

 

메타테이블 확인

▶ BATCH_JOB_EXECUTION_PARAMS

 

 

파라미터 LocalDate 타입 추가하기

실행 (Program arguments)
--job.name=jobParameterTestJob name=seohae seq=1,java.lang.Long date=2023-01-01,java.time.LocalDate double=1.5,java.lang.Double
date=2023-01-01,java.time.LocalDate

위와 같이 LocalDate 타입 파라미터를 추가했다.

 

JobParameterConfiguration.kt의 jobParameterTestStep1() 변경
@Bean
fun jobParameterTestStep1(jobRepository: JobRepository, platformTransactionManager: PlatformTransactionManager): Step {
    return StepBuilder("jobParameterTestStep1", jobRepository)
        .tasklet({ contribution, chunkContext ->
            println("jobParameterTestStep1 was executed")

            /* CASE1. contribution */
            val jobParameters = contribution.stepExecution
                .jobExecution
                .jobParameters

            val name = jobParameters.getString("name")
            val seq = jobParameters.getLong("seq")
            val localDate = jobParameters.getLocalDate("date")
            val doubleParam = jobParameters.getDouble("double")

            println("$name, $seq, $localDate, $doubleParam")

            /* CASE2. chunkContext */
            val jobParameters1 = chunkContext.stepContext.getJobParameters()
            val name2 = jobParameters1["name"] as String

            RepeatStatus.FINISHED
        }, platformTransactionManager)
        .build()
}

1) getLocalDate() 메서드 추가

val localDate = jobParameters.getLocalDate("date")

 

▶ BATCH_JOB_EXECUTION_PARAMS

 

 

identifying 테스트 해보기

▶ 모든 파라미터를 false로 셋팅하자.

 

[1] 최초 실행한다.

--job.name=jobParameterTestJob name=seohae,java.lang.String,false seq=1,java.lang.Long,false date=2023-01-01,java.time.LocalDate,false double=1.5,java.lang.Double,false

 

 

[2] name 을 seohae2로 변경해서 수행하자.

  • 실행결과 : 실패
--job.name=jobParameterTestJob name=seohae2,java.lang.String,false seq=1,java.lang.Long,false date=2023-01-01,java.time.LocalDate,false double=1.5,java.lang.Double,false

 

모든 파라미터의 identifying 값을 false 로 지정했기 때문에 name 파라미터를 변경하여도 이 파라미터는 식별자 flag true가 아니므로 배치는 실패한다.

Step already complete or not restartable, so no action to execute: StepExecution: id=37, version=3, name=jobParameterTestStep1, status=COMPLETED, exitStatus=COMPLETED, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0, exitDescription=

 

 

▶ name 파라미터 1개만 true로 해보자.

[1] 최초 실행한다.

--job.name=jobParameterTestJob name=seohae,java.lang.String,true seq=1,java.lang.Long,false date=2023-01-01,java.time.LocalDate,false double=1.5,java.lang.Double,false

 

[2] name 을 seohae2로 변경해서 수행하자.

  • 실행결과 : 성공
--job.name=jobParameterTestJob name=seohae2,java.lang.String,true seq=1,java.lang.Long,false date=2023-01-01,java.time.LocalDate,false double=1.5,java.lang.Double,false

name 파라미터의 identifying 값은 true이기 때문에 name을 seohae -> seohae2로 변경하면 동일 Job으로 보지 않고, Job이 정상 수행된다.

 

 

반응형

Designed by JB FACTORY