Implement Factory pattern for Kafka consumer

随声附和 提交于 2021-02-08 09:20:09

问题


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?


回答1:


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>

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



来源:https://stackoverflow.com/questions/64977248/implement-factory-pattern-for-kafka-consumer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!