gpt4 book ai didi

java - 测量 Spring 批步骤执行时间

转载 作者:行者123 更新时间:2023-11-30 06:30:04 24 4
gpt4 key购买 nike

测量 Spring Batch 中步骤执行时间的正确方法是什么?

这是步骤示例:

@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<POJO, POJO> chunk(10)
.reader(pojoReader())
.processor(pojoProcessor())
.build();
}

我在 ItemWriter 和 IemReader 上的步骤之前使用了一些示例

public class xxxxxItemReader implements xxxxItemReader <XXXX>{

private Logger log = LoggerFactory.getLogger(this.getClass());

@Override
public void beforeJob(JobExecution jobExecution) {
//
// Can Log || do some business code
//
log.info("Intercepting Job Excution - Before Job!");
}

@Override
public void afterJob(JobExecution jobExecution) {
//
// Can Log || do some Business code
//
log.info("Intercepting Job Excution - After Job!");
}

}

在这种情况下,我应该使用 ItemWriter 和 itemReader 进行前一步和后一步,最后将它们放在一起(添加)以获得霍尔步骤执行时间?这是最好的方法吗?

最佳答案

您可以在作业上使用JobExecutionListener:

@Bean
public Job helloJob() {
return jobBuilderFactory.get("HelloJob")
...
.listener(new JobExecutionListener()
{

@Override
public void beforeJob(JobExecution jobExecution)
{
// do nothing
}

@Override
public void afterJob(JobExecution jobExecution)
{
// get job's start time
Date start = jobExecution.getCreateTime();

// get job's end time
Date end = jobExecution.getEndTime();

// get diff between end time and start time
long diff = end.getTime() - start.getTime();

// log diff time
log.info(TimeUnit.SECONDS.convert(diff, TimeUnit.MILLISECONDS));
}
})
...
.build();
}

关于java - 测量 Spring 批步骤执行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46346291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com