在errorHandler中处理异常后如何在Controller或Route中获取错误消息?

问题描述 投票:2回答:1

我必须自定义Sftp入站默认处理程序LoggingHandler,并使用我自己的扩展了CustomizedErrorHandlerErrorHandler。但是处理异常后,我无法将任何消息返回给控制器。

我正在研究几天,但没有发现任何东西可以使用Controller向我的UI显示自定义消息。以下是我的CustomizedErrorHandlerSftpInboundConfiguration中的一些代码段。

SftpInboundConfiguration

public IntegrationFlow fileFlow() {
    SftpInboundChannelAdapterSpec spec = Sftp
            .inboundAdapter(getSftpSessionFactory())
            .preserveTimestamp(true)
            .remoteDirectory(getSourceLocation())
            .autoCreateLocalDirectory(true)
            .deleteRemoteFiles(false)
            .localDirectory(new File(getDestinationLocation()));

    return IntegrationFlows
            .from(spec, e -> e.id(BEAN_ID)
                    .autoStartup(false)
                    .poller(sftpPoller())
            )
            .channel(sftpReceiverChannel())
            .handle(sftpInboundMessageHandler())
            .get();
}

... ... ...

public PollerMetadata sftpPoller() {
        PollerMetadata pollerMetadata = new PollerMetadata();
        List<Advice> adviceChain = new ArrayList<>();
        pollerMetadata.setErrorHandler(customErrorMessageHandler());
        pollerMetadata.setTrigger(new PeriodicTrigger(5000));
        return pollerMetadata;
}

... ... ...

private CustomErrorMessageHandler customErrorMessageHandler() {
        return new CustomErrorMessageHandler(
                controlChannel(),
                BEAN_ID
        );
}

CustomErrorMessageHandler

public class CustomErrorMessageHandler implements ErrorHandler {
    private final MessageChannel CONTROL_CHANNEL;
    private final String BEAN_ID;

    public CustomErrorMessageHandler(
                  MessageChannel controlChannel, 
                  String beanID
    ) {
        this.CONTROL_CHANNEL = controlChannel;
        this.BEAN_ID = beanID;
    }

    public void handleError(@NotNull Throwable throwable) {
        final Throwable rootCause = ExceptionUtils.getRootCause(throwable);
        if (rootCause instanceof MessagingException) {
            log.error("MessagingException : {} ", rootCause.getMessage());
        } else if (rootCause instanceof SftpException) {
            log.error("SftpException : {}", rootCause.getMessage());
        }   ... ... ... 
        else {
            log.error("Unknown : Cause : {} , Error : {}", 
                                 rootCause, rootCause.getMessage());
        }


        log.info("Stopping SFTP Inbound");
        boolean is_stopped = CONTROL_CHANNEL.send(
                 new GenericMessage<>("@" + BEAN_ID + ".stop()"));
        if (is_stopped) {
            log.info("SFTP Inbound Stopped.");
        } else {
            log.info("SFTP Inbound Stop Failed.");
        }
    }
}

现在,我想从if-else语句中保存一些自定义消息,并需要在UI中显示它。有什么方法可以保存消息并使用Route或Controller显示它吗?

java spring spring-integration dsl spring-integration-sftp
1个回答
2
投票

不要自定义错误处理程序,请改用poller.errorChannel("myErrorChannel")

然后添加错误通道流

@Bean
IntegrationFlow errors() {
    return IntegrationFLows.from("myErrorChannel")
        .handle(...)
        ...
        .get();

发送给处理程序的消息是带有ErrorMessage有效负载的MessagingException,其中causefailedMessage是故障点的消息,而originalMessage是由故障点发出的原始消息。适配器。

处理完异常后,您可以简单地在控制器上调用方法以告知其状态。

© www.soinside.com 2019 - 2024. All rights reserved.