gpt4 book ai didi

java - 可以通过使用注入(inject)的 JobBuilderFactory 到该组件来创建批处理作业作为 @Component

转载 作者:行者123 更新时间:2023-12-02 03:32:04 25 4
gpt4 key购买 nike

我很好奇是否可以通过其他方式定义 Spring Batch Job,而不是通过方法创建 bean。有什么方法可以通过创建扩展 Job 的 @Component 并使用注入(inject)的 JobBuilderFactory 创建实例来定义批处理作业,以避免自己手动实现所有必需的方法?

@Component
public class ImportEmployeeJob extends FlowJob {

private final JobBuilderFactory jobBuilderFactory;
private final FlowStep printProcessedEmployeeFromCsv;

public ImportEmployeeJob(JobBuilderFactory jobBuilderFactory, FlowStep printProcessedEmployeeFromCsv, ApplicationContext context) {
this.jobBuilderFactory = jobBuilderFactory;
this.printProcessedEmployeeFromCsv = printProcessedEmployeeFromCsv;

jobBuilderFactory.get("importEmployeeJob")
.incrementer(new RunIdIncrementer())
.listener(new PersonCsvJobExecutionListener())
.flow(printProcessedEmployeeFromCsv)
.end()
.build();
}

}

这是一个有点愚蠢的例子,但我只是想告诉你我的意思

最佳答案

您可以使用工厂 bean 来实现这一点。这是一个简单的例子:

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
@EnableBatchProcessing
public class MyJob {

public static void main(String[] args) throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class);
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job job = context.getBean(Job.class);
jobLauncher.run(job, new JobParameters());
}

@Component
public static class MyJobFactoryBean implements FactoryBean<Job> {

private final JobBuilderFactory jobs;

private final StepBuilderFactory steps;

public MyJobFactoryBean(JobBuilderFactory jobs, StepBuilderFactory steps) {
this.jobs = jobs;
this.steps = steps;
}

@Override
public Job getObject() {
Step step = steps.get("step")
.tasklet((contribution, chunkContext) -> {
System.out.println("hello world");
return RepeatStatus.FINISHED;
})
.build();

return jobs.get("job")
.start(step)
.build();
}

@Override
public Class<?> getObjectType() {
return Job.class;
}

@Override
public boolean isSingleton() {
return true;
}
}

}

希望有帮助。

关于java - 可以通过使用注入(inject)的 JobBuilderFactory 到该组件来创建批处理作业作为 @Component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56878153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com