- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道这样的流程:
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/
有没有办法改变 ItemProcessor 的跳过行为,使其表现得像 ItemWriter?在 ItemProcessor 中抛出可跳过的异常会导致重新处理所有已接受的项目,从而导致其复杂性呈二次方的
我在我的批处理 Spring 项目中定义了一个作业流,并定义了 ItemReader、ItemProcessor、ItemWriter 等 我的 ItemReader 如下代码: @Component
以下是阅读器bean的代码示例 有 .我有一个类似的作家 bean 。我想要一个处理器的动态切换器。界面ItemProcessor需求I & O值得一提的是,要么我必
如果项目符合特定逻辑,我希望能够在处理它时删除它。例如,如果该项目不包含我要查找的值,我不希望将该项目写出到文件中。 我目前正在使用一个实现 ItemProcessor 的类. 我只是返回null吗?
我正在使用 Spring Batch 来解析我的文件。在 ItemProcessor 中,我验证传入字段是否正确。如果不是,我想抛出 ValidationException 并将具有不正确字段的相应行
我需要将一些数据从一个表迁移到另一个表,并在两者之间进行一些处理。处理是专有的,并作为 REST 服务公开。因此,我需要调用 REST 服务从表中读取记录,然后将处理后的记录写入另一个表中。如何将其实
我有一个场景,我需要解析平面文件并将这些记录处理到 mysql 数据库插入(模式已经存在)。 我正在使用 FlatFileItemReader 解析文件,并使用 JdbcCursorItemWrite
我正在编写一个 spring 批处理作业,在我的一个步骤中,我有以下处理器代码: @Component public class SubscriberProcessor implements Item
我有一组非常大的电子表格,需要使用 Spring Batch 生成。 我有一个 ItemReader 发出“行”,我希望我的 ItemProcessor 每收到 3000 行发出 1 个“表”。 有什
所以我在 Spring Batch 3.0.7.RELEASE 和 Spring 4.3.2.RELEASE 中遇到问题,监听器没有在我的 ItemProcessor ItemStreamReader
我需要在项目处理器之间传递与处理项目相关的数据,我不需要保留数据,最好的方法是什么(注意我目前正在使用 StepSynchronizationManager 访问 stepExecution 并将数据
问题很简单:如何将仅在 ItemReader 中知道的值(例如当前文件名)传递给 ItemProcessor? @Bean @JobScope public ItemReader reader(@Va
我有一个 Spring Batch 应用程序来获取 samba 服务器中的文件并在同一服务器上的不同文件夹中生成新文件。然而,流程中仅调用 ItemReader。问题是什么?谢谢。 批量配置: @Co
我想知道这样的流程: ItemReader -> ItemProcessor -> ItemWriter 其中每一个都是实现等效接口(interface)的自定义类,并且在面向 block 的步骤中,
我在 Spring Boot 和 Spring Batch 应用程序中遇到问题,我分享我的情况,我的 Layer Dao 是一个抛出 NPE (NullPointerException) 的接口(in
所以我正在创建我的第一个 Spring 批处理作业。我正在尝试读入 xml,操作一些值,然后将其写出。如果我不实现 org.springframework.batch.item.ItemProcess
我是一名优秀的程序员,十分优秀!