简体   繁体   中英

Implement Factory pattern for Kafka consumer

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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