Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在errorHandler中处理异常后,如何在控制器或路由中获取错误消息?_Java_Spring_Spring Integration_Dsl_Spring Integration Sftp - Fatal编程技术网

Java 在errorHandler中处理异常后,如何在控制器或路由中获取错误消息?

Java 在errorHandler中处理异常后,如何在控制器或路由中获取错误消息?,java,spring,spring-integration,dsl,spring-integration-sftp,Java,Spring,Spring Integration,Dsl,Spring Integration Sftp,我必须定制Sftp入站默认处理程序LoggingHandler,并使用我自己的CustomizedErrorHandler扩展ErrorHandler。但在处理异常后,我无法向控制器返回任何消息 我研究了几天,发现没有任何东西可以使用Controller将我定制的消息显示到我的UI上。下面是我的自定义ErrorHandler,SftpInboundConfiguration中的一些代码片段 SftpInboundConfiguration public IntegrationFlow fileF

我必须定制Sftp入站默认处理程序
LoggingHandler
,并使用我自己的
CustomizedErrorHandler
扩展
ErrorHandler
。但在处理异常后,我无法向控制器返回任何消息

我研究了几天,发现没有任何东西可以使用
Controller
将我定制的消息显示到我的UI上。下面是我的
自定义ErrorHandler
SftpInboundConfiguration
中的一些代码片段

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 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.");
        }
    }
}

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 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 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.");
        }
    }
}
public类CustomErrorMessageHandler实现ErrorHandler{
专用最终消息通道控制\ U通道;
私有最终字符串BEAN_ID;
公共CustomErrorMessageHandler(
消息通道控制通道,
串珠
) {
this.CONTROL_CHANNEL=controlChannel;
this.BEAN_ID=beanID;
}
公共无效句柄错误(@NotNull Throwable Throwable){
最终可丢弃的根本原因=ExceptionUtils.getRootCause(可丢弃);
if(MessaginException的根本原因实例){
错误(“MessaginException:{}”,rootCause.getMessage());
}else if(SftpException的根本原因实例){
错误(“SftpException:{}”,rootCause.getMessage());
}   ... ... ... 
否则{
log.error(“未知:原因:{},错误:{}”,
rootCause,rootCause.getMessage());
}
log.info(“停止SFTP入站”);
boolean is_stopped=CONTROL_CHANNEL.send(
新的GenericMessage(“@”+BEAN\u ID+”.stop()”);
如果(是否已停止){
log.info(“SFTP入站停止”);
}否则{
log.info(“SFTP入站停止失败”);
}
}
}

现在我想保存一些来自
if-else
语句的定制消息,并需要在UI中显示它。是否有任何方法可以保存消息并使用路由或
控制器显示消息?

不要自定义错误处理程序,而是使用
轮询器.errorChannel(“myErrorChannel”)

然后添加一个错误通道流

@Bean
集成流错误(){
返回IntegrationFLows.from(“myErrorChannel”)
.句柄(…)
...
.get();
发送给处理程序的消息是一条
错误消息
,带有
消息异常
有效载荷,带有
原因
故障消息
,这是故障点的消息,而
原始消息
是适配器发出的原始消息


处理完异常后,您只需调用控制器上的一个方法来告诉它状态。

我无法理解
poller.errorChannel(…)
。您能举个例子吗?我在
PollerMetadata
中只得到了
poller.setErrorHandler(…)
,而不是
poller(sftpoller())
使用
.poller(Pollers.fixedDelay(5000).errorChannel(“myErrors”)
)。
PollerSpec
具有
建议(建议…建议)
。很抱歉问上一个问题。这是我在代码中输入的错误。非常感谢您的回答和评论。如果我遇到任何其他问题,我将再次发表评论。您可以使用
.handle(controller,“setState”)
向控制器发送包含状态的消息。