【发布时间】:2018-01-04 23:25:19
【问题描述】:
目前,我正在尝试使用文档 https://docs.spring.io/spring-batch/reference/html/configureJob.html 中用于 spring-batch-boot 的基本 Web 容器指南,让我的脚本在 tomcat 服务器上运行
在修改主类之前,该脚本作为 jar 文件正常工作,但是当我尝试将其转换为 servlet 时,我的 @PostConstruct 仅在服务器启动时出现问题。此代码将 application.properties 设置为 spring.batch.job.enabled=false 并有一个控制器
@Controller
public class JobLauncherController {
@Autowired
JobLauncher jobLauncher;
@Autowired
Job job;
@RequestMapping("/jobLauncher.html")
public void handle() throws Exception{
jobLauncher.run(job, new JobParameters());
}
以主Application为tomcat启动servlet为
@SpringBootApplication
@EnableBatchProcessing
public class BatchApplication extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(BatchApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(BatchApplication.class, args);
}
问题是我的工作使用自定义项目读取器和编写器在使用@PostConstruct 运行它之前对其进行初始化。它在服务器启动时运行 @PostConstruct,这有助于初始化 bean 以进行写入。
我的项目阅读器/编写器看起来像这样
public class CustomReader extends ItemStreamSupport implements ItemReader<Acct>, ResourceAwareItemReaderItemStream<Acct> {
//basic autowiring
private int nextAcctIndex;
private List<Acct> acctsList = new ArrayList();
@PostConstruct
private void initialize() throws IOException {
//logic to parse files
acctsList = Collections.unmodifiableList(acctsList);
nextAcctIndex = 0;
}
@Override
public Acct read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
// System.out.println("Start Read");
Acct nextAcct = null;
if (nextAcctIndex < acctsList.size()) {
nextAcct = acctsList.get(nextAcctIndex);
nextAcctIndex++;
//System.out.println(nextAcct);
}
与大多数示例一样,BatchConfiguration 将所有内容调用为@Bean public
IteamReader<Acct> CustomReader(){ return new CustomReader();}
我的问题是我是否以错误的方式处理这个问题,或者有没有办法让它只有在控制器请求时才能调用 @PostConstruct?
【问题讨论】:
-
你的读者/作者应该是步骤或工作范围。显然,您将它们创建为单例。但是你为什么要在
@PostConstruct方法中这样做呢?没有这种需要…… -
你想用
@PostConstruct做什么?为什么要使用它而不是常规的 spring 注释? -
就是这样。我是 spring 新手,不确定我到底想问什么,但 StepExecutionListener 与 BeforeStep 和 AfterStep 完美搭配
标签: spring-mvc spring-boot spring-batch