https://docs.spring.io/spring-batch/docs/current/reference/html/job.html#javaConfig
Configuring and Running a Job
If a group of Jobs share similar but not identical configurations, it may help to define a “parent” Job from which the concrete Job instances can inherit properties. Similar to class inheritance in Java, a “child” Job combines its elements and attr
docs.spring.io
틀린 해석이 있다면 알려주세요 🍃
Java Configuration
There are three components for the Java-based configuration: the @EnableBatchProcessing annotation and two builders.
The @EnableBatchProcessing annotation works similarly to the other @Enable* annotations in the Spring family. In this case, @EnableBatchProcessing provides a base configuration for building batch jobs. Within this base configuration, an instance of StepScope and JobScope are created, in addition to a number of beans being made available to be autowired:
@EnableBatchProcessing 을 사용하면 아래의 기본 구성을 일괄 제공한다
- JobRepository: a bean named jobRepository
- JobLauncher: a bean named jobLauncher
- JobRegistry: a bean named jobRegistry
- JobExplorer: a bean named jobExplorer
- JobOperator: a bean named jobOperator
기본 구현은 앞의 목록에 언급된 빈의 제공하며 DataSource 및 PlatformTransactionManager가 Context 내에서 bean으로 제공되어야한다
DataSource 및 Transaction 관리자는 JobRepository 및 JobExplorer 인스턴스에서 사용된다
기본적으로 dataSource라는 데이터 소스와 transactionManager라는 트랜잭션 관리자가 사용된다
@EnableBatchProcessing 주석의 속성을 사용하여 이러한 빈을 사용자 정의할 수 있다.
아래 코드는 custom 데이터 소스 및 트랜잭션 관리자를 제공하는 방법을 보여준다.
@Configuration
@EnableBatchProcessing(dataSourceRef = "batchDataSource", transactionManagerRef = "batchTransactionManager")
public class MyJobConfiguration {
@Bean
public DataSource batchDataSource() {
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.generateUniqueName(true).build();
}
@Bean
public JdbcTransactionManager batchTransactionManager(DataSource dataSource) {
return new JdbcTransactionManager(dataSource);
}
public Job job(JobRepository jobRepository) {
return new JobBuilder("myJob", jobRepository)
//define job flow as needed
.build();
}
}
** @EnableBatchProcessing은 하나의 구성 configuration class 에만 애노테이션이 있어야한다
애노테이션이 달린 클래스가 있으면 위의 구성을 가지게 된다
5.0버전 부터 DefaultBatchConfiguration 클래스를 통해 기본 인프라 빈을 구성하는 프로그래밍 방식의 대안이 제공된다.
이 클래스는 @EnableBatchProcessing에서 제공하는 것과 동일한 빈을 제공하며 배치 작업을 구성하기 위한 기본 클래스로 사용할 수 있다.
다음 코드는 이를 사용하는 방법에 대한 일반적인 예시이다
@Configuration
class MyJobConfiguration extends DefaultBatchConfiguration {
@Bean
public Job job(JobRepository jobRepository) {
return new JobBuilder("job", jobRepository)
// define job flow as needed
.build();
}
}
데이터 소스 및 트랜잭션 관리자는 애플리케이션 컨텍스트에서 확인되고 작업 저장소 및 작업 탐색기에서 설정된다. 필요한 setter를 재정의하여 인프라 빈의 구성을 custom할 수 있다.
인스턴스에 대한 문자 인코딩 custom
@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;
}
}
** DefaultBatchConfiguration은 @EnableBatchProcessing 과 함께 사용하면 안된다
@EnableBatchProcessing을 통해 Spring Batch를 구성하는 선언적 방법을 사용하거나 DefaultBatchConfiguration을 확장하는 프로그래밍 방식을 사용해야 하지만 동시에 두 가지 방법을 모두 사용할 수는 없다 (중요!!!!)