我有一个批处理作业,它使用 HibernateCursorItemReader
从数据库读取,使用自定义处理器处理结果,然后提交到另一个数据库。
我想展示的是,根据要处理的总项目数,已经处理了多少项目。
我尝试使用 JobExecutionListener
实现自定义 @beforeJob
以从第一个表中获取行数,然后可以进行比较
针对 Job Execution
定期提交。
有没有比使用 Job Execution
监听器更好的方法。是否可以在第一次读取时获得表的总行数,在初始化期间在 HibernateCursorItemReader
上设置一个值或类似的东西?
工作
<batch:job id="SomeLongJob" job-repository="jobRepository" restartable="true">
<batch:listeners>
<batch:listener ref="batchJobExecutionListener" />
</batch:listeners>
<batch:step id="first1">
<tasklet transaction-manager="hibernateTransactionManager">
<chunk reader="hibernateItemReader"
processor="batchCustomProcessor"
writer="hibernateItemWriter"
skip-limit="0"
commit-interval="10">
</chunk>
</tasklet>
</batch:step>
</batch:job>
读者
<bean id="hibernateItemReader"
class="org.springframework.batch.item.database.HibernateCursorItemReader">
<property name="queryString" value="from MyTable" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>
最佳答案
在作业开始之前获取要处理的总项目,即行
BatchJobExecutionListener.class
public void beforeJob(JobExecution jobExecution) {
int total = dao.getTotalRows();
jobExecution.getExecutionContext().put("JobComplete", total);
}
在作业执行期间检索总项目并与读取计数进行比较
JobWrapper.class
...
private JobExecution jobE;
...
public Long getProgress() {
double jobComplete = (Double) jobE.
getExecutionContext().
get("jobComplete");
double reads = 0;
for (StepExecution step : jobE.getStepExecutions()) {
reads = reads + step.getReadCount();
}
return Math.round(reads / jobComplete * 100);
}