如何在没有 itemwriter 的情况下编写 spring 批处理步骤

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26202241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:07:00  来源:igfitidea点击:

How to write a spring batch step without an itemwriter

springspring-batch

提问by user3247376

I am trying to configure a spring batch step without an item writer using below configuraion. However i get error saying that writer element has neither a 'writer' attribute nor a element.

我正在尝试使用以下配置在没有项目编写器的情况下配置 spring 批处理步骤。但是我收到错误说 writer 元素既没有“writer”属性也没有元素。

I went through the link spring batch : Tasklet without ItemWriter. But could not resolve issue. Could any one tell me the specific changes to be made in the code snippet I mentioned

我浏览了链接spring 批处理:没有 ItemWriter 的 Tasklet。但无法解决问题。谁能告诉我在我提到的代码片段中要进行的具体更改

<batch:job id="helloWorldJob">
        <batch:step id="step1">
            <batch:tasklet>
                <batch:chunk reader="cvsFileItemReader"
                    commit-interval="10">
                </batch:chunk>
            </batch:tasklet>
        </batch:step>
    </batch:job>

    <bean id="cvsFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">

        <property name="resource" value="classpath:cvs/input/report.csv" />

        <property name="lineMapper">
            <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                <property name="lineTokenizer">
                    <bean
                        class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                        <property name="names" value="id,sales,qty,staffName,date" />
                    </bean>
                </property>
                <property name="fieldSetMapper">
                    <bean class="com.mkyong.ReportFieldSetMapper" />

                    <!-- if no data type conversion, use BeanWrapperFieldSetMapper to map by name
                    <bean
                        class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                        <property name="prototypeBeanName" value="report" />
                    </bean>
                     -->
                </property>
            </bean>
        </property>

    </bean>

回答by Luca Basso Ricci

For chunk-based step reader and writer are mandatory.
If you don't want a writer use a No-operation ItemWriter that does nothing.

对于基于块的步进读取器和写入器是强制性的。
如果您不希望编写器使用不执行任何操作的无操作 ItemWriter。

EDIT:
A no-op implementation is an empty implementation of interface tha does...nothing!
Just let your class implements desiderable inteface(s) with empty methods.

编辑:
无操作实现是接口的空实现……什么都没有!
只需让您的类使用空方法实现所需的接口即可。

No-op ItemWriter:

无操作 ItemWriter:

public class NoOpItemWriter implements ItemWriter {
  void write(java.util.List<? extends T> items) throws java.lang.Exception {
    // no-op
  }
}

回答by Ashok Singh

I hope you got answer but I want to explain it for other readers, When we use chunk then usually we declare reader, processor and writer. In chunk reader and writer are mandatory and processor is optional. In your case if you don't need writer then u need to make a class which implements ItemWriter. Override write method and keep it blank. Now create a bean of writer class and pass it as reference of writer.

我希望你得到答案,但我想向其他读者解释,当我们使用块时,通常我们声明读者、处理器和作者。在块中,读取器和写入器是必需的,而处理器是可选的。在您的情况下,如果您不需要编写器,那么您需要创建一个实现 ItemWriter 的类。覆盖 write 方法并将其保留为空白。现在创建一个 writer 类的 bean 并将其作为 writer 的引用传递。

<batch:step id="recordProcessingStep" >
        <batch:tasklet>
            <batch:chunk reader="fileReader" processor="recordProcessor"
                writer="rocordWriter" commit-interval="1" />
        </batch:tasklet>
    </batch:step>

Your writer class will look like .

你的作家班看起来像 .

public class RecordWriter<T> implements ItemWriter<T> {
@Override
public void write(List<? extends T> items) throws Exception {
    // TODO Auto-generated method stub

}

}

}

回答by user2065095

In maven repoyou can find the framework "spring-batch-samples".
In this framework you will find this Writer :

maven repo 中,您可以找到框架“spring-batch-samples”。
在这个框架中,你会发现这个 Writer :

org.springframework.batch.sample.support.DummyItemWriter