首页 > 解决方案 > 如何根据一些 Kafka 事件暂停和恢复 spring 批处理步骤

问题描述

我们正在尝试创建一个中央批处理服务,它将在远程(微)服务中调用批处理。在此期间,我们希望暂停步骤执行,直到远程服务没有响应批处理服务。这可以通过 Spring Batch 实现吗?

标签: javaspring-bootapache-kafkaspring-batch

解决方案


您可以尝试实现 StepListener,其中您有 beforeStep 和 afterStep 方法,您可以在 beforeStep 方法调用中进行控制以等待其他服务调用完成其执行

public class StepTwoListener implements StepExecutionListener {
@Override
public void beforeStep(StepExecution stepExecution) {
     long start = System.currentTimeMillis();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Sleep time in ms = "+(System.currentTimeMillis()-start));
    System.out.println("Before Step Execution");
}}

你可以在你的步骤中使用监听器

@Bean
public Step stepTwo() {
    return stepBuilderFactory.get("stepTwo").tasklet(new StepTwo()).listener(new StepTwoListener()).build();
}

推荐阅读