简体   繁体   English

处理集成流中可能发生的异常

[英]Handling exception that might occur in an integration flow

I have a REST controller which calls a gateway annotated with @MessagingGateway(errorChannel = ERROR_CHANNEL)我有一个 REST controller 调用一个用@MessagingGateway(errorChannel = ERROR_CHANNEL)注释的网关

This way, whatever error occurs downstream the integration flow initiated by the gateway will flow into an error channel which will be handled by another integration flow, this is working as expected.这样,网关启动的集成流下游发生的任何错误都将流入错误通道,该通道将由另一个集成流处理,这按预期工作。

Now, there is another scenario where an integration flow reads messages from Kafka, routes those messages to another channel, one more integration flow processes those messages and another flow sends an HTTP request to a remote service.现在,还有另一种场景,集成流从 Kafka 读取消息,将这些消息路由到另一个通道,另一个集成流处理这些消息,另一个流向远程服务发送 HTTP 请求。

public IntegrationFlowBuilder attachmentEventTenantRouter(String tenantId) {
    return attachmentEventBaseFlow(".*")
            .filter(Message.class, m -> m.getHeaders().get(KafkaConstants.HEADER_PREFIX + MessageHeader.TENANT_ID_KEY) != null && m.getHeaders().get(KafkaConstants.HEADER_PREFIX + MessageHeader.TENANT_ID_KEY, String.class).equalsIgnoreCase(tenantId));
}

private IntegrationFlowBuilder attachmentEventBaseFlow(String eventRegex) {
    return IntegrationFlows
            .from(Kafka.messageDrivenChannelAdapter(kafkaListenerContainerFactory.createContainer(topic)).errorChannel(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME))
            .log(LoggingHandler.Level.DEBUG, "Inside Kafka Consumer")
            .filter(Message.class, m -> filter(m, eventRegex))
            .transform(messageToEventTransformer);
}

@Bean 
public IntegrationFlow kafkaConsumerFlow() {
    return fromKafkaFlowHelper.attachmentEventTenantRouter(TENANT_ID)
            .route(Message.class, m -> m.getHeaders().get(KafkaConstants.HEADER_PREFIX + MessageHeader.EVENT_TYPE_KEY, String.class), p -> p
                    .resolutionRequired(false)
                    .channelMapping("eventType", "transformMessagesFromKafkaAndPublishAnotherEvent")
                    .defaultOutputChannel("nullChannel"))
            .get();
}

@Bean
public IntegrationFlow transformMessagesFromKafkaAndPublishAnotherEvent() {
    return flow -> flow
            .transform(transformer)
            .handle( getKafkaHandler() );
}

@Bean
public IntegrationFlow sendHttpRequestToRemoteServiceFromKafkaEvent() {
    return flow -> flow
            .transform(transformer)
            .handle(gatewayCall, e -> e.advice(expressionAdvice()));
}

How can I do to handle the exceptions that might occur in the flows above?如何处理上述流程中可能发生的异常?

As you can see, I am using a ExpressionEvaluatingRequestHandlerAdvice which does the work for the handle method, but not sure how to handle exceptions that might occur in the transformers?如您所见,我使用的是 ExpressionEvaluatingRequestHandlerAdvice,它为 handle 方法工作,但不确定如何处理转换器中可能发生的异常?

The massage gateway with an error channel configured does the trick when the gateway is called by a rest controller, but when the flow is initiated by the Kafka consumer, I'm lost how to achieve this.当网关被 rest controller 调用时,配置了错误通道的按摩网关可以解决问题,但是当流程由 Kafka 消费者启动时,我不知道如何实现这一点。

Thanks.谢谢。

EDITED AFTER Artem's RESPONSE TO ADD CLARIFICATION :在 Artem 回应添加澄清后编辑

This is the configuration of the integration flow that posts a request to a remote service and whose exceptions does not seem to be caught and routed to the errorChannel without a ExpressionEvaluatingRequestHandlerAdvice:这是将请求发布到远程服务的集成流的配置,并且在没有 ExpressionEvaluatingRequestHandlerAdvice 的情况下,它的异常似乎不会被捕获并路由到 errorChannel:

@Bean
public IntegrationFlow sendHttpRequestToRemoteServiceFromKafkaEvent() {
    return flow -> flow
            .transform(transformer)
            .handle(getOAuth2Handler(HttpMethod.PUT, "remote url"), e -> e.advice(expressionEvaluatingRequestHandlerAdvice));
}

private OAuth2RequestHandler getOAuth2Handler(HttpMethod httpMethod, String url) {
    return new OAuth2RequestHandler(oAuth2RestTemplate, httpMethod, url);
}

And class OAuth2RequestHandler which implements a MessageHandler以及实现 MessageHandler 的 class OAuth2RequestHandler

@Override
public void handleMessage(org.springframework.messaging.Message<?> message) throws MessagingException {
    String requestBody = (String) message.getPayload();
    ResponseEntity<String> response = oAuth2RestTemplate.exchange(url, httpMethod, new HttpEntity<>(requestBody), String.class);
}

I see you use already an errorChannel() on the Kafka message-driven channel adapter.我看到您已经在 Kafka 消息驱动的通道适配器上使用了errorChannel() So, what is the question?那么,问题是什么?

This part of your flow is exact equivalent to mentioned @MesaagingGateway with its errorChannel configuration.这部分流程完全等同于提到的@MesaagingGateway及其errorChannel配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM