簡體   English   中英

Spring Cloud Kinesis Binder 如何處理生產者的錯誤 - 根據文檔,它不起作用

[英]Spring Cloud Kinesis Binder How to Handle ERROR for Producer - As per Documentation it is Not working

我遵循了以下文檔,我有一個生產者和消費者與 Kinesis Stream 完美配合。 我想了解在發生任何異常時如何處理 Producer(Source)中的 ERROR。

我根據 Spring Stream 錯誤處理文檔嘗試了以下方法:

@StreamListener("errorChannel")
@ServiceActivator(inputChannel = "errorChannel")

兩者都不起作用。 我在生產者方法中顯式地拋出一個 RuntimeException 並期望它將在“errorChannel”中但無法接收到它。

如果有人成功做到了這一點,請幫助我弄清楚這一點或與我分享方法。

注意:幾個月前我遇到了這個確切的問題。 我們使用 Kafka 代替 Kinesis。 因為你已經標記了 spring-cloud-stream-binder-kafka,所以我在同一個地方給出了輸入。

你可以為你的生產者編寫一個自定義回調,這個回調可以告訴你消息是失敗還是成功發布。 失敗時,記錄消息的元數據。

下面是一個小代碼片段,可以更好地解釋我正在談論的回調。

請注意,這是針對 Kafka 的。 相應地更改 Kinesis 的代碼。

public class ProduceToKafka {

  private ProducerRecord<String, String> message = null;

 // TracerBulletProducer class has producer properties
  private KafkaProducer<String, String> myProducer = TracerBulletProducer
      .createProducer();

  public void publishMessage(String string) {

    ProducerRecord<String, String> message = new ProducerRecord<>(
        "topicName", string);

    myProducer.send(message, new MyCallback(message.key(), message.value()));
  }

  class MyCallback implements Callback {

    private final String key;
    private final String value;

    public MyCallback(String key, String value) {
      this.key = key;
      this.value = value;
    }


    @Override
    public void onCompletion(RecordMetadata metadata, Exception exception) {
      if (exception == null) {
        log.info("--------> All good !!");
      } else {
        log.info("--------> not so good  !!");
        log.info(metadata.toString());
        log.info("" + metadata.serializedValueSize());
        log.info(exception.getMessage());

      }
    }
  }

}

我寫了一篇關於在 Spring 抽象的不同級別處理生產者失敗的中等文章。 這可以幫助你。 檢查一下: https : //medium.com/@akhil.ghatiki/kafka-producer-failure-handling-at-multiple-levels-of-spring-abstractions-e530edb02a6c

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM