Spring Batch - 读取的记录数和数量处理

时间:2015-05-21 03:29:12

标签: spring spring-batch

完成作业后,是否可以判断读取和/或处理了多少记录?我有一个从数据库和处理器读取数据的工作,我根据某些标准过滤几个记录并将它们发送给编写器。我想知道从DB中读取了多少总记录以及向写入步骤发送了多少记录。

这是我的批量配置文件。

<bean id="dbItemReader" class="....JdbcCursorItemReader">
    <property name="datasource" ref="datasource"/>
    <property name="sql" ref="select * from"/>
    <property name="rowMapper">
      <bean class="com.my.MyRowMapper"/>
     </property> 
 </bean>

<bean id="itemProcessor" class="com.my.MyItemProcessor"/> 
<bean id="itemWriter" class="com.my.MyItemWriter"/>

<batch:job id="myJob">
    <batch:step id="step1">
       <batch:tasklet transaction-manager="jobTransactionManager">
             <batch:chunk reader="dbItemReader" processor="itemProcessor" writer="itemWriter" commit-interval="100"/>
       </batch:tasklet> 

3 个答案:

答案 0 :(得分:5)

Spring Batch在作业存储库中存储读取,处理,跳过,写入等项目的数量。假设您正在使用数据库作业存储库,则可以在BATCH_STEP_EXECUTION表中查看它们。

您可以在此处的文档中详细了解存储在作业存储库中的信息:http://docs.spring.io/spring-batch/reference/html/metaDataSchema.html

答案 1 :(得分:2)

最简单的方法是 - 你可以在你的步骤中使用一个监听器,并且可以获得所有的计数。

<batch:job id="myJob">
<batch:step id="step1">
   <batch:tasklet transaction-manager="jobTransactionManager">
         <batch:chunk reader="dbItemReader"
          processor="itemProcessor" 
          writer="itemWriter" 
          commit-interval="100">

          <bean id="customStepListner" class="com.company.listner.StepListner" />

          </batch:chunk>
   </batch:tasklet> 




public class StepListner implements StepExecutionListener {
    @Override
    public ExitStatus afterStep(StepExecution arg0) {
        int readCount=arg0.getReadCount();
        int writeCount=arg0.getWriteCount();
        int skipCount=arg0.getSkipCount();
        int commitCount=arg0.getCommitCount();

        arg0.getStartTime();
        arg0.getEndTime();

    }

    @Override
    public void beforeStep(StepExecution arg0) {

    }
}

答案 2 :(得分:2)

import org.apache.log4j.Logger;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
/**
* Vaquar khan
*/
public class StepExecuListner implements StepExecutionListener {
    static Logger log = Logger.getLogger("badRecordLogger");

    @Override
    public void beforeStep(StepExecution stepExecution) {
        System.out.println("StepExecutionListener - beforeStep");
        log.error("StepExecutionListener - beforeStep " );

    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        System.out.println("StepExecutionListener - afterStep");
        log.error("------------------------------------------------------------------------------------");
        log.error("StepExecutionListener - afterStep:getCommitCount=" +  stepExecution.getCommitCount());
        log.error("StepExecutionListener - afterStep:getFilterCount=" +  stepExecution.getFilterCount());
        log.error("StepExecutionListener - afterStep:getProcessSkipCount=" +  stepExecution.getProcessSkipCount());
        log.error("StepExecutionListener - afterStep:getReadCount=" +  stepExecution.getReadCount());
        log.error("StepExecutionListener - afterStep:getReadSkipCount=" +  stepExecution.getReadSkipCount());
        log.error("StepExecutionListener - afterStep:getRollbackCount=" +  stepExecution.getRollbackCount());
        log.error("StepExecutionListener - afterStep:getWriteCount=" +  stepExecution.getWriteCount());
        log.error("StepExecutionListener - afterStep:getWriteSkipCount=" +  stepExecution.getWriteSkipCount());
        log.error("StepExecutionListener - afterStep:getStepName=" +  stepExecution.getStepName());
        log.error("StepExecutionListener - afterStep:getSummary=" +  stepExecution.getSummary());
        log.error("StepExecutionListener - afterStep:getStartTime=" +  stepExecution.getStartTime());
        log.error("StepExecutionListener - afterStep:getStartTime=" +  stepExecution.getEndTime());
        log.error("StepExecutionListener - afterStep:getLastUpdated=" +  stepExecution.getLastUpdated());
        log.error("StepExecutionListener - afterStep:getExitStatus=" +  stepExecution.getExitStatus());
        log.error("StepExecutionListener - afterStep:getFailureExceptions=" +  stepExecution.getFailureExceptions());
        log.error("------------------------------------------------------------------------------------");

        return null;
    }
}

批量xml内部

<bean id="stepListener" class="com.test.listener.StepListner" />



<batch:job id="TestDataLoader">
        <batch:split id="split1" task-executor="taskExecutor">
        <batch:flow>
        <batch:step id="step1">
         <batch:tasklet task-executor="taskExecutor" throttle-limit="5">
                <batch:chunk reader="itemReader" writer="itemWriter" commit-interval="${commitInterval}" skip-limit="3">

                    <batch:skippable-exception-classes>
                        <batch:include class="java.lang.NumberFormatException" />
                    </batch:skippable-exception-classes>
                    <batch:listeners>
                        <batch:listener ref="skipListener" />
                            <batch:listener ref="stepListener" />
                    </batch:listeners>
                </batch:chunk>
            </batch:tasklet>
        </batch:step>
        </batch:flow>

        <batch:flow>
        <batch:step id="step2">
             <batch:tasklet task-executor="taskExecutor" throttle-limit="15">
                <batch:chunk reader="itemReaderToDelete"
                    writer="itemWriterToDelete" commit-interval="${commitInterval}" skip-limit="3">
                    <batch:skippable-exception-classes>
                        <batch:include class="org.springframework.dao.DataAccessException" />
                        <batch:include class="java.lang.NumberFormatException" />
                    </batch:skippable-exception-classes>
                    <batch:listeners>
                        <batch:listener ref="skipListener" />
                        <batch:listener ref="stepListener" />
                    </batch:listeners>
                </batch:chunk>
            </batch:tasklet>
        </batch:step>
        </batch:flow>

        </batch:split>
    </batch:job>