Spring StatxeventitemReader-跳过以前作业执行中处理的XML片段

Spring StatxeventitemReader-跳过以前作业执行中处理的XML片段,spring,spring-batch,Spring,Spring Batch,使用CSV文件并重新启动失败的作业时,可以使用StepExecutionListner和关联的BeforStep(..)方法来定位 文件中的读取器。因此,代码可能看起来像: public void beforeStep(StepExecution stepExecution) { ExecutionContext executionContext = stepExecution.getExecutionContext(); if (executionContext.conta

使用CSV文件并重新启动失败的作业时,可以使用StepExecutionListner和关联的BeforStep(..)方法来定位 文件中的读取器。因此,代码可能看起来像:

public void beforeStep(StepExecution stepExecution) {

    ExecutionContext executionContext = stepExecution.getExecutionContext();

    if (executionContext.containsKey(getKey(LINES_READ_COUNT))) {

        long lineCount = executionContext.getLong(getKey(LINES_READ_COUNT));

        LineReader reader = getReader();
        Object record = "";
        while (reader.getPosition() < lineCount && record != null) {
            record = readLine();
        }
    }
} // Or something similar
环境-spring-batch-core-2.2.0;弹簧芯-3.2.2

测试输入文件

将XML文件转换为CSV文件

<company>
    <record refId="1001">
        <name>mkyong</name>
        <age>31</age>
        <dob>31/8/1982</dob>
        <income>200,000</income>
    </record>
    <record refId="1002">
        <name>kkwong</name>
        <age>30</age>
        <dob>26/7/1983</dob>
        <income>100,999</income>
    </record>
    <record refId="1003">
        <name>joel</name>
        <age>29</age>
        <dob>21/8/1984</dob>
        <income>1,000,000</income>
    </record>
    <record refId="1004">
        <name>leeyy</name>
        <age>29</age>
        <dob>21/3/1984</dob>
        <income>80,000.89</income>
    </record>
    <record refId="1005">
        <name>Grant</name>
        <age>29</age>
        <dob>21/3/1984</dob>
        <income>80,000.89</income>
    </record>
</company>
测试运行2

处理所有“剩余数据”,因此期望。。。。refId=“1003”,refId=“1004”,refId=“1005”

测试结果


不幸的是,看起来StateVentitemReader是从文件的开头读取,而不是根据StateVentitemReader.read.count的值重新定位自己,该值在第一次测试后设置为2。

您不需要配置任何东西,这已经是
StateVentitemReader
的默认行为。当它打开时,它会从步骤执行上下文中的读取计数中重新定位自己。

我已经用一些测试运行数据更新了我的帖子。不幸的是,我没有看到您描述的StatxeventitemReader的默认行为。我没有看到问题。您没有看到任何重复的输出记录,那么问题是什么?就像读卡器在输入文件中重新定位自己一样,写卡器也在输出文件中重新定位自己。因此,剩下的3条记录将附加到输出文件中。我的编写器具有
。如果我将
添加到我的writer配置中,我会看到您所引用的重复记录。这很奇怪。您能确保您实际上正在重新启动作业,而不是启动新的作业实例吗?在我的database.xml文件中,我有``一个,每次运行作业时,它都会初始化数据结构。
statxeventitemreader
现在正按照您的原始注释工作。谢谢你抽出时间。
<company>
    <record refId="1001">
        <name>mkyong</name>
        <age>31</age>
        <dob>31/8/1982</dob>
        <income>200,000</income>
    </record>
    <record refId="1002">
        <name>kkwong</name>
        <age>30</age>
        <dob>26/7/1983</dob>
        <income>100,999</income>
    </record>
    <record refId="1003">
        <name>joel</name>
        <age>29</age>
        <dob>21/8/1984</dob>
        <income>1,000,000</income>
    </record>
    <record refId="1004">
        <name>leeyy</name>
        <age>29</age>
        <dob>21/3/1984</dob>
        <income>80,000.89</income>
    </record>
    <record refId="1005">
        <name>Grant</name>
        <age>29</age>
        <dob>21/3/1984</dob>
        <income>80,000.89</income>
    </record>
</company>
batch_job_execution --->>  "FAILED";"FAILED";"java.lang.RuntimeException: Get me out of here!

batch_step_execution_context --->> {"string":"StaxEventItemReader.read.count","int":2}

Output CSV file --->> 1001,mkyong,31,31/08/1982,200000
                      1002,kkwong,30,26/07/1983,100999
batch_job_execution --->>  "COMPLETED";"COMPLETED";"''";"2015-01-25 16:18:08.587"

batch_step_execution_context --->>  {"string":"StaxEventItemReader.read.count","int":6}


Output CSV file --->> 1001,mkyong,31,31/08/1982,200000
                      1002,kkwong,30,26/07/1983,100999
                      1003,joel,29,21/08/1984,1000000
                      1004,leeyy,29,21/03/1984,80000.89
                      1005,Grant,29,21/03/1984,80000.89