commit
This commit is contained in:
@@ -11,6 +11,7 @@ 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.beans.BeansException;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
@@ -22,6 +23,8 @@ import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import com.spring.infra.batch.BatchJobInfoBeanPostProcessor.BatchJobInfoData;
|
||||
|
||||
/**
|
||||
* 배치 작업을 정의하는 추상 클래스입니다.
|
||||
* <p>
|
||||
@@ -32,9 +35,11 @@ import org.springframework.transaction.PlatformTransactionManager;
|
||||
* @version 1.0
|
||||
*/
|
||||
@Configuration
|
||||
public abstract class AbstractBatchTask implements ApplicationContextAware {
|
||||
public abstract class AbstractBatchTask implements ApplicationContextAware, InitializingBean {
|
||||
|
||||
private final BatchJobInfo batchJobInfo;
|
||||
private BatchJobInfoBeanPostProcessor batchJobInfoProcessor;
|
||||
private BatchJobInfoData batchJobInfoData;
|
||||
private ApplicationContext applicationContext;
|
||||
private JobRepository jobRepository;
|
||||
private PlatformTransactionManager transactionManager;
|
||||
@@ -52,18 +57,23 @@ public abstract class AbstractBatchTask implements ApplicationContextAware {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ApplicationContext를 설정합니다.
|
||||
*
|
||||
* @param applicationContext 설정할 ApplicationContext
|
||||
* @throws BeansException Beans 예외
|
||||
*/
|
||||
@Override
|
||||
public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
this.batchJobInfoProcessor = applicationContext.getBean(BatchJobInfoBeanPostProcessor.class);
|
||||
initializeBatchJobInfo();
|
||||
registerJobBean();
|
||||
}
|
||||
|
||||
private void initializeBatchJobInfo() {
|
||||
String beanName = applicationContext.getBeanNamesForType(this.getClass())[0];
|
||||
this.batchJobInfoData = batchJobInfoProcessor.getBatchJobInfo(beanName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 배치 작업을 Spring의 Bean으로 등록합니다.
|
||||
*/
|
||||
@@ -71,7 +81,7 @@ public abstract class AbstractBatchTask implements ApplicationContextAware {
|
||||
var beanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
|
||||
var registry = (BeanDefinitionRegistry) beanFactory;
|
||||
var beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(Job.class, this::createJob);
|
||||
registry.registerBeanDefinition(jobName(), beanDefinitionBuilder.getBeanDefinition());
|
||||
registry.registerBeanDefinition(batchJobInfoData.getJobName(), beanDefinitionBuilder.getBeanDefinition());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,9 +113,9 @@ public abstract class AbstractBatchTask implements ApplicationContextAware {
|
||||
private Job createJob() {
|
||||
List<Step> steps = createSteps();
|
||||
if (steps.isEmpty()) {
|
||||
throw new IllegalStateException("No steps defined for job: " + jobName());
|
||||
throw new IllegalStateException("No steps defined for job: " + batchJobInfoData.getJobName());
|
||||
}
|
||||
var jobBuilder = new JobBuilder(jobName())
|
||||
var jobBuilder = new JobBuilder(batchJobInfoData.getJobName())
|
||||
.incrementer(new RunIdIncrementer())
|
||||
.repository(jobRepository)
|
||||
.start(steps.get(0));
|
||||
@@ -122,7 +132,7 @@ public abstract class AbstractBatchTask implements ApplicationContextAware {
|
||||
*/
|
||||
protected List<Step> createSteps() {
|
||||
List<Step> steps = new ArrayList<>();
|
||||
steps.add(addStep(jobName() + "Step", createTasklet()));
|
||||
steps.add(addStep(batchJobInfoData.getJobName() + "Step", createTasklet()));
|
||||
return steps;
|
||||
}
|
||||
|
||||
@@ -148,40 +158,4 @@ public abstract class AbstractBatchTask implements ApplicationContextAware {
|
||||
*/
|
||||
protected abstract Tasklet createTasklet();
|
||||
|
||||
/**
|
||||
* 배치 작업 그룹을 반환합니다.
|
||||
*
|
||||
* @return 배치 작업 그룹 이름
|
||||
*/
|
||||
public String group() {
|
||||
return batchJobInfo.group();
|
||||
}
|
||||
|
||||
/**
|
||||
* 배치 작업 이름을 반환합니다.
|
||||
*
|
||||
* @return 배치 작업 이름
|
||||
*/
|
||||
public String jobName() {
|
||||
return batchJobInfo.jobName();
|
||||
}
|
||||
|
||||
/**
|
||||
* cron 표현식을 반환합니다.
|
||||
*
|
||||
* @return cron 표현식
|
||||
*/
|
||||
public String cronExpression() {
|
||||
return batchJobInfo.cronExpression();
|
||||
}
|
||||
|
||||
/**
|
||||
* 배치 작업의 설명을 반환합니다.
|
||||
*
|
||||
* @return 배치 작업의 설명
|
||||
*/
|
||||
public String description() {
|
||||
return batchJobInfo.description();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user