본문 바로가기
프로그래밍/spring

Spring Batch 5

by 낭만프로그래머. 2024. 6. 9.

새롭게 변경된것들

  • New Java version baseline
  • Major dependencies upgrade
  • Full GraalVM native support
  • Introduction of the new Observation API from Micrometer
  • Execution context Meta-data improvement
  • New default execution context serialization format
  • SystemCommandTasklet enhancements
  • Add support to use any type as a job parameter
  • Improved job parameters conversion
  • New Annotation Attributes in EnableBatchProcessing
  • New Configuration Class for Infrastructure Beans
  • Transaction Support in JobExplorer and JobOperator
  • Automatic registration of a JobOperator with EnableBatchProcessing
  • Test utilities configuration updates
  • Migration to JUnit Jupiter
  • Java Records Support Improvement
  • UTF-8 by default
  • Java 8 features updates
  • New Maven Bill of Materials
  • Full MariaDB support
  • Support for SAP HANA as a job repository
  • Improved documentation

 

Spring Batch 5에서 일단 Deprecated 된 builder .

Class JobBuilderFactory
java.lang.Object
org.springframework.batch.core.configuration.annotation.JobBuilderFactory
@Deprecated(since="5.0.0",
            forRemoval=true)
public class JobBuilderFactory
extends Object
Deprecated, for removal: This API element is subject to removal in a future version.
Deprecated as of v5.0 and scheduled for removal in v5.2 in favor of using the JobBuilder.
Convenient factory for a JobBuilder that sets the JobRepository automatically.
@Deprecated(since="5.0",
            forRemoval=true)
public JobBuilder(String name)
Deprecated, for removal: This API element is subject to removal in a future version.
use JobBuilder(String, JobRepository)
Create a new builder for a job with the given name.
@Deprecated(since="5.0",
            forRemoval=true)
public StepBuilder(String name)
Deprecated, for removal: This API element is subject to removal in a future version.
use StepBuilder(String, JobRepository)
Initialize a step builder for a step with the given name.
Parameters:
name - the name of the step
Class StepBuilderFactory
java.lang.Object
org.springframework.batch.core.configuration.annotation.StepBuilderFactory
@Deprecated(since="5.0.0",
            forRemoval=true)
public class StepBuilderFactory
extends Object
Deprecated, for removal: This API element is subject to removal in a future version.
Deprecated as of v5.0 and scheduled for removal in v5.2 in favor of using the StepBuilder.
Convenient factory for a StepBuilder which sets the JobRepository automatically.

 

 

JobBuilder Configuration

- EnableBatch annotation 을 사용하는 대신 아래와 같이 사용.

@Configuration
class MyJobConfiguration extends DefaultBatchConfiguration {

	@Bean
	public Job job(JobRepository jobRepository) {
		return new JobBuilder("job", jobRepository)
				// define job flow as needed
				.build();
	}

	@Override
	protected Charset getCharset() {
		return StandardCharsets.ISO_8859_1;
	}
}

 

run.id 를 incrementer 로 사용하고

readerStep 을 사용하는 Job configuration

public Job job(JobRepository jobRepository) {
		return new JobBuilder(JOB_ID,jobRepository)
			.incrementer(new RunIdIncrementer())
			.start(readerStep)
			.build();
	}

 

readerStep configuration

Events 테이블에서 event dto 를 꺼내서 그냥 exampleItemWriter 쪽으로 전달해주는 Step 선언

reader, writer 의 parameter 는 bean 을 전달해도 되고 메소드를 전달해도 되는것같다.

다만 생성시점에 어떻게 주입되는 지는 체크를 좀더 해봐야 할듯

@Bean(value = STEP_DB_READ)
	@JobScope
	public Step readDBStep(
		JobRepository jobRepository, PlatformTransactionManager platformTransactionManager, JobParameter jobParameter
			, JpaPagingItemReader<Events> itemReader) {

		return new StepBuilder(STEP_DB_READ,jobRepository)
			.<Events, Events>chunk(jobParameter.getChunkSize(), platformTransactionManager)
			.reader(itemReader)
			.writer(exampleItemWriter())
			.build();
	}