https://docs.spring.io/spring-batch/docs/current/reference/html/job.html#configuringJobRepository
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
틀린 해석이 있다면 알려주세요 🍫
Configuring a JobRepository
@EnableBatchProcessing, a JobRepository
가 제공된다이를 직접 구성하는 방법에 대해 설명한다
JobRepository는 JobExecution 및 StepExecution과 같은 Spring Batch 내에서 지속되는 다양한 도메인 개체의 기본 CRUD 작업에 사용된다. JobLauncher, Job 및 Step과 같은 많은 주요 프레임워크 기능에 필요하다
batch namespace 는 JobRepository 구현 및 해당 collaborators의 많은 구현 세부 정보를 abstracts한다. 다음 예와 같이 몇 가지 구성 옵션을 사용할 수 있다
<job-repository id="jobRepository"
data-source="dataSource"
transaction-manager="transactionManager"
isolation-level-for-create="SERIALIZABLE"
table-prefix="BATCH_"
max-varchar-length="1000"/>
ID 외에는 이전에 나열된 구성 옵션이 필요없다. 설정하지 않으면 이전에 표시된 기본값이 사용된다.
max-varchar-length default 는 2500 (sample scheme script 에서 Long VARCHAR column Length이다)
dataSource 및 transactionManager 외에는 이전에 나열된 구성 옵션도 필요없다.
설정하지 않으면 이전에 표시된 기본값이 사용된다.
max-varchar-length default 는 2500 (sample scheme script 에서 Long VARCHAR column Length이다)
Transaction Configuration for the JobRepository
네임스페이스 또는 제공된 FactoryBean을 사용하면 저장소 주변에 transaction advice가 자동으로 생성된다.
이는 실패 후 다시 시작하는 데 필요한 상태를 포함하여 batch metadata가 올바르게 유지되도록 하기 위해서이다.
repository 메서드가 트랜잭션이 아닌 경우 프레임워크의 동작이 제대로 정의되지 않는다.
create* 메서드 속성의 격리 수준은 작업이 시작될 때 두 프로세스가 동시에 동일한 작업을 시작하려고 시도하는 경우 하나만 성공하도록 별도로 지정된다.
해당 메서드의 default isolation level 은 SERIALIZABLE이며 매우 공격적이다.
READ_COMMITTED는 일반적으로 똑같이 작동한다.
READ_UNCOMMITTED는 두 프로세스가 이러한 방식으로 충돌할 가능성이 없으면 된다.
create* 메소드에 대한 호출이 매우 짧기 때문에 SERIALIZED가 데이터베이스 플랫폼에서 지원하는 한 문제를 일으킬 가능성은 없다.
이 설정은 재정의가 가능하다
재정의 예시
<job-repository id="jobRepository"
isolation-level-for-create="REPEATABLE_READ" />
@Configuration
@EnableBatchProcessing(isolationLevelForCreate = "ISOLATION_REPEATABLE_READ")
public class MyJobConfiguration {
// job definition
}
namespace를 사용하지 않으면 AOP를 사용하여 transaction 동작을 구성해야한다
how to configure the transactional behavior of the repository
해당 예시
<aop:config>
<aop:advisor
pointcut="execution(* org.springframework.batch.core..*Repository+.*(..))"/>
<advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
You can use the preceding fragment nearly as is, with almost no changes. Remember also to include the appropriate namespace declarations and to make sure spring-tx and spring-aop (or the whole of Spring) are on the classpath.
java예시
@Bean
public TransactionProxyFactoryBean baseProxy() {
TransactionProxyFactoryBean transactionProxyFactoryBean = new TransactionProxyFactoryBean();
Properties transactionAttributes = new Properties();
transactionAttributes.setProperty("*", "PROPAGATION_REQUIRED");
transactionProxyFactoryBean.setTransactionAttributes(transactionAttributes);
transactionProxyFactoryBean.setTarget(jobRepository());
transactionProxyFactoryBean.setTransactionManager(transactionManager());
return transactionProxyFactoryBean;
}
Changing the Table Prefix
JobRepository의 수정 가능한 또 다른 속성은 메타 데이터 테이블의 테이블 접두사이다.
기본적으로 모두 BATCH_로 시작한다. BATCH_JOB_EXECUTION 및 BATCH_STEP_EXECUTION이 두 가지 예이다.
그러나 이 접두사를 수정해야 하는 potential 경우가 있다. 스키마 이름을 테이블 이름 앞에 추가해야 하거나 동일한 스키마 내에서 둘 이상의 메타데이터 테이블 집합이 필요한 경우 테이블 접두사를 변경해야한다
xml예시
<job-repository id="jobRepository"
table-prefix="SYSTEM.TEST_" />
java예시
@Configuration
@EnableBatchProcessing(tablePrefix = "SYSTEM.TEST_")
public class MyJobConfiguration {
// job definition
}
Given the preceding changes, every query to the metadata tables is prefixed with SYSTEM.TEST_. BATCH_JOB_EXECUTION is referred to as SYSTEM.TEST_JOB_EXECUTION.
** 테이블접두사만 구성할 수 있고, table and column names는 바꿀 수 없다
Non-standard Database Types in a Repository
지원되는 플랫폼 목록에 없는 db 플랫폼을 사용하려면 SQL 변형이 유사할 때 지원되는 유형을 쓸 수 있다
if the SQL variant is close enough. To do this, you can use the raw JobRepositoryFactoryBean instead of the namespace shortcut and use it to set the database type to the closest match.
<bean id="jobRepository" class="org...JobRepositoryFactoryBean">
<property name="databaseType" value="db2"/>
<property name="dataSource" ref="dataSource"/>
</bean>
@Bean
public JobRepository jobRepository() throws Exception {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
factory.setDataSource(dataSource);
factory.setDatabaseType("db2");
factory.setTransactionManager(transactionManager);
return factory.getObject();
}
데이터베이스 유형이 지정되지 않은 경우 JobRepositoryFactoryBean은 DataSource에서 데이터베이스 유형을 자동 감지하려고 시도한다.
플랫폼 간의 주요 차이점은 주로 기본 키를 증가시키는 전략에 의해 설명되므로 incrementerFactory도 재정의해야 하는 경우가 많다(Spring 프레임워크의 표준 구현 중 하나를 사용하여).
그래도 작동하지 않거나 RDBMS를 사용하지 않는 경우 유일한 옵션은 SimpleJobRepository가 의존하는 다양한 Dao 인터페이스를 구현하고 일반적인 Spring 방식으로 수동으로 연결하는 것일 수 있다.