简体   繁体   English

Kafka Consumer 钻石算子无法推断 arguments

[英]Kafka Consumer diamond operator cannot infer arguments

I am creating a Kafka consumer.我正在创建一个 Kafka 消费者。 Mentioned piece of code is running fine on my sample POC project.提到的一段代码在我的示例 POC 项目上运行良好。

Somehow when I use this in my project application, it gives error, of not able to resolve Diamond Operator.不知何故,当我在我的项目应用程序中使用它时,它会给出错误,无法解析 Diamond Operator。

Project Code项目代码

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;
}

Error Snapshot错误快照

在此处输入图像描述

You do not need a diamond operator in your code.您的代码中不需要菱形运算符。 Change the error line to this将错误行更改为此

KafkaConsumer consumer = new KafkaConsumer(props);

The return type should change from Consumer<Long, String> to KafkaConsumer .返回类型应从Consumer<Long, String>更改为KafkaConsumer Rest all code is fine and you can use the consumer as it is. Rest 所有代码都很好,您可以按原样使用消费者。

This is reproducible as long as you import java.util.function.Consumer (having one generic type) which is not an interface that KafkaConsumer implements.只要您导入不是KafkaConsumer实现的接口的java.util.function.Consumer (具有一种通用类型),这是可以重现的。 You need to import the correct one:您需要导入正确的:

org.apache.kafka.clients.consumer.Consumer

This one has two generic types and it should work with no compilation errors.这个有两种通用类型,它应该没有编译错误。

final Consumer<Long, String> consumer = new KafkaConsumer<>(props);

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

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