Reputation: 3494
Is it possible to tell how many records are read and/or processed once the job is executed completely? I've a job that reads data from the database and in the processor, I filter few records based on certain criteria and send them to the writer. I would like to know how many total records are read from the DB and how many are sent to the writer step.
Here is my batch config file.
<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>
Upvotes: 8
Views: 24086
Reputation: 11479
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;
}
}
inside of batch 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>
Upvotes: 7
Reputation: 1467
the simplest approach is - you can use a listener over your step and can get all the counts.
<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) {
}
}
Upvotes: 2
Reputation: 21483
Spring Batch stores the number of items read, processed, skipped, written, etc in the job repository. Assuming you're using a database job repository, you can view them there in the BATCH_STEP_EXECUTION
table.
You can read more about the information stored in the job repository in the documentation here: http://docs.spring.io/spring-batch/reference/html/metaDataSchema.html
Upvotes: 6