gpt4 book ai didi

java - 在 Spring Batch 中调用 ItemReader、ItemProcessor、ItemWriter 的构造函数时?

转载 作者:行者123 更新时间:2023-11-29 05:27:16 25 4
gpt4 key购买 nike

我想知道这样的流程:

ItemReader -> ItemProcessor -> ItemWriter 

其中每一个都是实现等效接口(interface)的自定义类,并且在面向 block 的步骤中,何时调用每个构造函数?

据我所知(如果我错了请纠正我)ItemWriter 构造函数将在步骤开始时被调用一次,并且对于每个 block ,只调用 write()。此规则适用于其他 2 个吗?

最佳答案

Vinay 的回答是正确的,但需要一些详细说明。

对于 reader-processor->writer Spring 调用默认构造函数(让我们忽略 @PostConstruct 等)Scope(“step”),顾名思义就是为每一步创建一个新的bean。Step 不必与线程一对一,例如假设我有以下 reader-processor->writer

  @Component
@Scope("step")
public class DoNothingItemReader implements ItemReader<String>{
public DoNothingItemReader() {
LOGGER.info(String.format("New %s created"
,ClassUtils.getShortName(this.getClass())));
}

@Override
public String read() throws Exception{
LOGGER.info("Nothing to read...");
..
}

}

@Component
@Scope("step")
public class DoNothingItemProcessor implements ItemProcessor<String, String> {
public DoNothingItemProcessor() {
LOGGER.info(String.format("New %s created"
,ClassUtils.getShortName(this.getClass())));
}

@Override
public String process(String i) throws Exception {
LOGGER.info("Nothing to process...");
return i;
}

}

@Component
@Scope("step")
public class DoNothingItemWritter implements ItemWriter<String[]> {
public DoNothingItemWritter() {
LOGGER.info(String.format("New %s created"
,ClassUtils.getShortName(this.getClass())));
}

@Override
public void write(List<? extends String[]> items) throws Exception {
LOGGER.info("Nothing to write...");

}

现在我们分两步重用上面的内容,例如

    <batch:job id="testScopStep">
<batch:step id="step1" next="step2">
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="doNothingItemReader"
processor="doNothingItemProcessor"
writer="doNothingItemWritter" commit-interval="3">
</batch:chunk>
</batch:tasklet>
</batch:step>
<batch:step id="step2">
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="doNothingItemReader"
processor="doNothingItemProcessor"
writer="doNothingItemWritter" commit-interval="3">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>

每次读取器->处理器->写入器
构造函数将被调用两次日志将是

 [SimpleJobLauncher] - <Job: [FlowJob: [name=testScopStep]] launched>
[SimpleStepHandler] - <Executing step: [step1]>
[DoNothingItemReader] - <New DoNothingItemReader created>
[DoNothingItemReader] - <Nothing to read...>
[DoNothingItemReader] - <Nothing to read...>
[DoNothingItemReader] - <Nothing to read...>
[DoNothingItemProcessor] - <New DoNothingItemProcessor created>
[DoNothingItemProcessor] - <Nothing to process...>
[DoNothingItemProcessor] - <Nothing to process...>
[DoNothingItemWritter] - <New DoNothingItemWritter created>
[DoNothingItemWritter] - <Nothing to write...>
[SimpleStepHandler] - <Executing step: [step2]>
[DoNothingItemReader] - <New DoNothingItemReader created>
[DoNothingItemReader] - <Nothing to read...>
[DoNothingItemReader] - <Nothing to read...>
[DoNothingItemReader] - <Nothing to read...>
[DoNothingItemProcessor] - <New DoNothingItemProcessor created>
[DoNothingItemProcessor] - <Nothing to process...>
[DoNothingItemProcessor] - <Nothing to process...>
[DoNothingItemWritter] - <New DoNothingItemWritter created>
[DoNothingItemWritter] - <Nothing to write...>
[SimpleJobLauncher] - <Job: [FlowJob: [name=testScopStep]] completed

现在考虑以下使用复合编写器的场景

 <batch:job id="testScopStep">
<batch:step id="step1">
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="doNothingItemReader"
processor="doNothingItemProcessor"
writer="compositeWriter" commit-interval="3">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>

这里的构造函数只会被调用一次。
日志会显示

[SimpleJobLauncher] - <Job: [FlowJob: [name=testScopStep]] launched>
[SimpleStepHandler] - <Executing step: [step1]>
[DoNothingItemReader] - <New DoNothingItemReader created>
[DoNothingItemReader] - <Nothing to read...>
[DoNothingItemReader] - <Nothing to read...>
[DoNothingItemReader] - <Nothing to read...>
[DoNothingItemProcessor] - <New DoNothingItemProcessor created>
[DoNothingItemProcessor] - <Nothing to process...>
[DoNothingItemProcessor] - <Nothing to process...>
[DoNothingItemWritter] - <New DoNothingItemWritter created>
[DoNothingItemWritter] - <Nothing to write...>
[DoNothingItemWritter] - <Nothing to write...>
[SimpleJobLauncher] - <Job: [FlowJob: [name=testScopStep]] completed

所以在这种情况下,我们应该小心考虑到我们共享同一个作者。

关于java - 在 Spring Batch 中调用 ItemReader、ItemProcessor、ItemWriter 的构造函数时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22254789/

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