[英]Kafka Consumer diamond operator cannot infer arguments
我正在創建一個 Kafka 消費者。 提到的一段代碼在我的示例 POC 項目上運行良好。
不知何故,當我在我的項目應用程序中使用它時,它會給出錯誤,無法解析 Diamond Operator。
項目代碼
private Consumer<Long, String> createConsumer(String topicName) {
final Properties props = new Properties();
// todo: use @Value instead of hardcodes
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ConsumerConfig.GROUP_ID_CONFIG, "application");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, LongDeserializer.class.getName());
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");
final Consumer<Long, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Collections.singletonList(topicName));
return consumer;
}
錯誤快照
您的代碼中不需要菱形運算符。 將錯誤行更改為此
KafkaConsumer consumer = new KafkaConsumer(props);
返回類型應從Consumer<Long, String>
更改為KafkaConsumer
。 Rest 所有代碼都很好,您可以按原樣使用消費者。
只要您導入不是KafkaConsumer
實現的接口的java.util.function.Consumer
(具有一種通用類型),這是可以重現的。 您需要導入正確的:
org.apache.kafka.clients.consumer.Consumer
這個有兩種通用類型,它應該沒有編譯錯誤。
final Consumer<Long, String> consumer = new KafkaConsumer<>(props);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.