问题
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