为Kafka消费者实现工厂模式 [英] Implement Factory pattern for Kafka consumer

查看:35
本文介绍了为Kafka消费者实现工厂模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Kafka 消费者那里获取响应对象.完整的代码就我而言,如您所见,我将输入转换为工厂对象,而不是使用工厂对象将输入转换为自定义对象.

I want to get response object from Kafka consumer. 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?

推荐答案

这应该与 Kafka 消费者 API 和适当的解串器一起开箱即用;不需要转换,因为您使用的是泛型 ConsumerRecord

This should just work out of the box with the Kafka consumer API and an appropriate deserializer; no casting required since you're using generics ConsumerRecord<String, SaleResponseFactory>

工厂模式用于构建对象.您在这里所做的只是访问已经反序列化和构建的 Kafka 事件的方法

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消费者实现工厂模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆