在Spring Cloud Aws Kinesis Stream中实现自定义错误消息处理程序

时间:2018-02-18 09:58:41

标签: spring-integration spring-cloud spring-cloud-stream spring-integration-aws

我写过关于Kinesis流的春天云流应用程序

跨渠道的消息流

<expr>?.also { a ->
    // then-block
} ?: run {
    // else-block
}

我打算在这些频道上编写错误处理。 目前我明确捕捉错误并发送到不同的渠道。我想实现generi异常处理程序

,而不是捕获和修改有效负载

粘合剂

Kinesis Input --> Transformer --> Router (route to error or actaul processor)

配置

    public interface Binder {

      @Input(ITEM_KINESIS_INPUT)
      SubscribableChannel itemMessage();

    }

变形金刚

    @Configuration
    @EnableIntegration
    public class IntegrationConfiguration {

      @Bean(name = DEFAULT_ITEM_LISTENER)
      public MessageChannel defaultItemChannel() {
        return new DirectChannel();
      }

      @Bean(name = DEFAULT_ERROR)
      public MessageChannel DEFAULT_ERROR() {
        return new DirectChannel();
      }

      @Bean
      @Transformer(inputChannel = ITEM_KINESIS_INPUT, outputChannel = ITEM_TRANSFORMER)
      public ItemTransformer itemTransformer() {
        return new ItemTransformer();
      }


      @ServiceActivator(inputChannel = ITEM_TRANSFORMER)
      @Bean
      public ItemMessageTypeRouter router() {
        return new ItemMessageTypeRouter();
      }

    // I tried this as well Not working
      @ServiceActivator(inputChannel = "errorChannel")
      @Bean
      public AbstractMessageHandler process() {
        return new AbstractMessageHandler() {
          @Override
          protected void handleMessageInternal(Message<?> message1) throws Exception {
            System.out.println("MessageHandler SA");
          }
        };

      }

    }

在路由器路由到不同频道时出错

    public class ItemTransformer extends AbstractTransformer {


      @Override
      public String getComponentType() {
        return "transformer";
      }



      @Override
      protected Object doTransform(Message<?> message) throws Exception {
        Object transformedPayload = null;
        try {
          //transform
        } catch (Exception e) {
          transformedPayload =
              new MessageTransformationException(message, "failed to transform message", e);
        }
        return MessageBuilder.withPayload(transformedPayload).copyHeaders(message.getHeaders()).build();
      }
    }

错误处理

public class ItemMessageTypeRouter extends AbstractMappingMessageRouter {


  @Override
  protected List<Object> getChannelKeys(Message<?> message) {
    return (message.getPayload() instanceof Throwable) ? Arrays.asList(DEFAULT_ERROR)
        : Arrays.asList(DEFAULT_ITEM_LISTENER)
  } 
}

我们可以实现所有这些通道的通用异常处理程序。

目前我只处理Transformer中的错误,但如果路由器或后来的频道发生任何异常,那么这也需要手动处理

有关更好的错误处理的任何见解

0 个答案:

没有答案