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

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

问题描述

我想从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< String,SaleResponseFactory>

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

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