简体   繁体   English

为Kafka消费者实现工厂模式

[英]Implement Factory pattern for Kafka consumer

I want to get response object from Kafka consumer.我想从 Kafka 消费者那里得到响应对象。 Full code In my case as you can see I'm casting input to a factory object instead of using the factory object to transform the input into custom object.完整代码在我的例子中,如您所见,我将输入强制转换为工厂对象,而不是使用工厂对象将输入转换为自定义对象。

    ConsumerRecord<String, SaleResponseFactory> consumerRecord = replyFuture.get(10, TimeUnit.SECONDS);
    SaleResponseFactory value = (SaleResponseFactory) consumerRecord.value();
    System.out.println("!!! " + value.getUnique_id());

It's not very clear for me how I can use the factory pattern in this case.我不太清楚在这种情况下如何使用工厂模式。 Is there some better solution in general?一般有更好的解决方案吗?

This should just work out of the box with the Kafka consumer API and an appropriate deserializer;这应该与 Kafka 消费者 API 和适当的反序列化器一起开箱即用; no casting required since you're using generics ConsumerRecord<String, SaleResponseFactory>由于您使用的是泛型ConsumerRecord<String, SaleResponseFactory>

Factory pattern is for building objects.工厂模式用于构建对象。 All you're doing here is accessing a method of a Kafka event that's already been deserialized and built您在这里所做的就是访问已经反序列化和构建的 Kafka 事件的方法

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

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