簡體   English   中英

Kafka Spring Cloud Stream Batch Consumption - 無法將類轉換為消息

[英]Kafka Spring Cloud Stream Batch Consumption - Unable to cast class to Message

在 Spring Cloud Stream 批處理模式下使用 KafkAvroSerialized 消息時遇到以下錯誤消息。 這在非批處理模式下早期工作正常。

以下是將現有應用程序轉換為批處理模式的僅有的兩個更改 -

  1. 在 application.yml 中啟用屬性 batch-mode:true 2) 修改了 List<Message<SpecificRecord>> 的參數,該參數以前是 Message<SpecificRecord>

    Error Message : org.springframework.messaging.MessageException: Exception thrown while invoking MyConsumer#consume[1 args]; nested exception is java.lang.ClassCastException: SomeClass cannot be cast to org.springframework.messaging.Message

請幫助解決問題。

我設法以批處理模式獲取標題,但它們被包裝在列表中。

如果您需要訪問元數據和其他標頭,則不能使用@StreamListener,您需要使用函數式編程方法編寫您的消費者函數,例如:

@Bean
Consumer<Message<List<String>>> input() {
    return list -> {
        System.out.println(list);
    };
}

並添加 spring.cloud.function.definition=input(your function name),還將綁定設置為 input(your function name) -in-0 如

spring:
  profiles: dev
  cloud:
    stream:
      function:
        definition: input
      bindings:
        input-in-0:
          destination: yourDestination
          group: yourGroup

參考: https : //docs.spring.io/spring-cloud-stream/docs/current/reference/html/spring-cloud-stream.html#_functional_binding_names

https://cloud.spring.io/spring-cloud-stream/multi/multi__programming_model.html

我也能夠達到可以讓以下塊工作的地步,

@Bean
Consumer<Message<List<Foo>>> consumer() {
    return  message -> {
        log.info("Message={}", message);
        message.getPayload().forEach(event -> {
            log.info("event = {}", event);
        });
        message.getHeaders().entrySet().forEach(header -> {
            log.info("header = {}", header);
        });
    };
}

但是,我最終擁有的是兩個列表,一個列表和另一個列表,有沒有辦法可靠地配對 Foo - Headers?

暫無
暫無

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

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