我有一个JobExecutionListener
,它将执行后的作业状态写入日志文件。我怎样才能获得处理的物品数量?
@Component
public class JobListener implements JobExecutionListener {
@Override
public void afterJob(JobExecution task) {
log.info(task.getStatus());
//number of entries??
}
}
答案 0 :(得分:2)
使用JobExplorer,您可以查看之前的StepExecutions以获取每个步骤中读取的项目数等。您可以在此处阅读有关JobExplorer的更多信息:http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/explore/JobExplorer.html
<强>更新强>
您实际上甚至不需要使用JobExplorer
。由于您拥有JobExecution
,因此您已经引用了所有StepExecutions
。每个StepExecution
包含读取,处理,写入,跳过等项目的数量。
答案 1 :(得分:1)
我看到了两种方法。
您可以实施ItemProcessListener。在处理项目之后/之前调用此接口。此界面还报告了任何错误。
public class ItemCountsListener implements ItemProcessListener<Object, Object> {
private static final AtomicLong count = new AtomicLong(1);
public void afterProcess(Object item, Object result) {
count.getAndIncrement();
}
public void beforeProcess(Object item) {}
public void onProcessError(Object item, Exception e) { }
}
或者您可以调用方法jobExecution.getStepExecutions()
。此方法返回Collection<StepExecution>
个对象。在这个类中,有一个方法getWriteCount
,它返回为此执行写入的当前项目数。
我会做类似这段代码的事情:
public void afterJob(JobExecution jobExecution) { int nbItemsProcessed; for (StepExecution stepExecution : jobExecution.getStepExecutions()) { nbItemsProcessed += stepExecution.getWriteCount(); } }
答案 2 :(得分:0)
这就是我做到的:
JobExecution job = jobLauncher.run(praepJob, getJobParameters());
job.getStepExecutions().stream().findFirst().get().getWriteCount()
答案 3 :(得分:0)
两种方式,
如果您正在使用元数据表,则可以配置jobExplorer来获取元数据数据。
使用xml,您可以将其配置为
<bean id="jobOperator"
class="org.springframework.batch.core.launch.support.SimpleJobOperator">
<property name="jobRegistry" ref="jobRegistry" />
<property name="jobExplorer" ref="jobExplorer" />
<property name="jobLauncher" ref="jobLauncher" />
<property name="jobRepository" ref="jobRepository" />
</bean>
<bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
<bean id="jobExplorer"
class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="tablePrefix" value="BATCH_" />
</bean>
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
<property name="taskExecutor" ref="batchTaskExecutor"></property>
</bean>
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="databaseType" value="oracle" />
<property name="transactionManager" ref="transactionManager" />
</bean>
<bean id="batchTaskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="10" />
<property name="maxPoolSize" value="25" />
<property name="queueCapacity" value="30" />
</bean>