java - Spring Batch 集成——远程分块从属异常

标签 java spring spring-integration spring-batch

确保从站上的异常映射到主站上的响应(指示远程步骤实际上失败)的标准配置是什么?

目前,从站上的服务激活器中发生的任何异常都会完全停止流程,并且在整个过程超时之前我在主站上没有得到任何响应。我可以在从站上添加几次重试(使用手册中的 this 信息),但如果它完全失败或者是一个异常(exception),我不想重试,我需要立即将失败响应发送到主站。

不幸的是,有关远程分块的文档非常稀疏,我找不到任何显示如何处理此问题的内容。

最佳答案

通过分区,无论成功/失败,您都应该返回 StepExecution:

@ServiceActivator
public StepExecution handle(StepExecutionRequest request) {

    Long jobExecutionId = request.getJobExecutionId();
    Long stepExecutionId = request.getStepExecutionId();
    StepExecution stepExecution = jobExplorer.getStepExecution(jobExecutionId, stepExecutionId);
    if (stepExecution == null) {
        throw new NoSuchStepException("No StepExecution could be located for this request: " + request);
    }

    String stepName = request.getStepName();
    Step step = stepLocator.getStep(stepName);
    if (step == null) {
        throw new NoSuchStepException(String.format("No Step with name [%s] could be located.", stepName));
    }

    try {
        step.execute(stepExecution);
    }
    catch (JobInterruptedException e) {
        stepExecution.setStatus(BatchStatus.STOPPED);
        // The receiver should update the stepExecution in repository
    }
    catch (Throwable e) {
        stepExecution.addFailureException(e);
        stepExecution.setStatus(BatchStatus.FAILED);
        // The receiver should update the stepExecution in repository
    }

    return stepExecution;

}

(StepExecutionRequestHandler)。

通过分块,您应该以任何方式获得 ChunkResponse...

@ServiceActivator
public ChunkResponse handleChunk(ChunkRequest<S> chunkRequest) throws Exception {

    if (logger.isDebugEnabled()) {
        logger.debug("Handling chunk: " + chunkRequest);
    }

    StepContribution stepContribution = chunkRequest.getStepContribution();

    Throwable failure = process(chunkRequest, stepContribution);
    if (failure != null) {
        logger.debug("Failed chunk", failure);
        return new ChunkResponse(false, chunkRequest.getSequence(), chunkRequest.getJobId(), stepContribution, failure.getClass().getName()
                + ": " + failure.getMessage());
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Completed chunk handling with " + stepContribution);
    }
    return new ChunkResponse(true, chunkRequest.getSequence(), chunkRequest.getJobId(), stepContribution);

}

(ChunkProcessorChunkHandler)

关于java - Spring Batch 集成——远程分块从属异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44933331/

相关文章:

database - Spring JPA : Testing DAO layer with multiple databases in a CI environment

spring-integration - 'UniqueExpiryCallback' 中只能注册一个 'MessageGroupStore' 实例

java - 获取第二个最后列表项 Jsoup 的值

java - 在Google应用程序引擎中如何删除最初使用FileService存储的文件

java - 为什么在HashMap.keySet()中声明局部变量ks?

Spring集成路由表

error-handling - 应用程序运行时异常未发送到 errorChannel 或 ServiceActivator 无法监听 errorChannel

Java 根据值顺序对对象列表进行排序(例如 d、s、a、q、c)

java - ResponseEntity 可以在 Spring 的 Controller 之外使用吗?

java - Spring bean 在 Spring Web 应用程序中初始化两次