英文:
Value not return to calling method when using ConsumerRecord<> in the listner
问题
# KafkaListener
@KafkaListener(id = ProductTopicConstants.UPDATE_PRODUCT, topics = ProductTopicConstants.UPDATE_PRODUCT,
containerFactory = "addUpdateProductContainerFactory")
@SendTo
public Object UpdateProduct(ConsumerRecord<String, ProductViewModel> productViewModel) {
String id = productViewModel.key();
Product product = productRepository.findByid(id);
if (product != null) {
product.setName(productViewModel.name());
product.setPrice(productViewModel.price());
product.setDescription(productViewModel.description());
return productRepository.save(product);
}
return KafkaNull.INSTANCE;
}
# Producer
public GenericResponse<ProductViewModel> Update(ProductViewModel product, String id) throws InterruptedException, ExecutionException, TimeoutException {
RequestReplyFuture<String, Object, Object> future =
this._replyTemplate.sendAndReceive(new ProducerRecord<>(ProductTopicConstants.UPDATE_PRODUCT, 0, id, product));
LOG.info(future.getSendFuture().get(kafkaConstants.kafkaTimeout, TimeUnit.SECONDS).getRecordMetadata().toString());
Object productDb = future.get(kafkaConstants.kafkaTimeout, TimeUnit.SECONDS).value();
if (productDb == null)
return null;
if (productDb == HttpStatus.CONFLICT)
return new GenericResponse<ProductViewModel>(null, HttpStatus.CONFLICT);
Product mappedProducts = mapper.convertValue(productDb, new TypeReference<Product>() {});
return new GenericResponse<ProductViewModel>(new ProductViewModel(mappedProducts.getId(), mappedProducts.getName(), mappedProducts.getPrice(), mappedProducts.getDescription(), mappedProducts.getVersion()), null);
}
# Container configuration
@Bean
public ConsumerFactory<String, ProductViewModel> consumerFactoryAddUpdateProduct() {
return new DefaultKafkaConsumerFactory<>(consumerConfigs(),
new StringDeserializer(),
new JsonDeserializer<>(ProductViewModel.class));
}
@Bean
public KafkaListenerContainerFactory<?> addUpdateProductContainerFactory(ProducerFactory<String, Object> pf) {
ConcurrentKafkaListenerContainerFactory<String, ProductViewModel> factory =
new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactoryAddUpdateProduct());
factory.setReplyTemplate(kafkaTemplate(pf));
return factory;
}
# Error
org.springframework.kafka.requestreply.KafkaReplyTimeoutException: Reply timed out
at org.springframework.kafka.requestreply.ReplyingKafkaTemplate.lambda$scheduleTimeout$3(ReplyingKafkaTemplate.java:339) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[na:na]
at java.base/java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:264) ~[na:na]
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java) ~[na:na]
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]
英文:
KafkaListener
@KafkaListener(id = ProductTopicConstants.UPDATE_PRODUCT, topics = ProductTopicConstants.UPDATE_PRODUCT,
containerFactory = "addUpdateProductContainerFactory")
@SendTo
public Object UpdateProduct(ConsumerRecord<String, ProductViewModel> productViewModel) {
String id = productViewModel.key();
Product product = productRepository.findByid(id);
if (product != null) {
product.setName(productViewModel.name());
product.setPrice(productViewModel.price());
product.setDescription(productViewModel.description());
return productRepository.save(product);
}
return KafkaNull.INSTANCE;
}
Producer
public GenericResponse<ProductViewModel> Update(ProductViewModel product, String id) throws InterruptedException, ExecutionException, TimeoutException {
RequestReplyFuture<String, Object, Object> future =
this._replyTemplate.sendAndReceive(new ProducerRecord<>(ProductTopicConstants.UPDATE_PRODUCT,0, id,product));
LOG.info(future.getSendFuture().get(kafkaConstants.kafkaTimeout, TimeUnit.SECONDS).getRecordMetadata().toString());
Object productDb = future.get(kafkaConstants.kafkaTimeout, TimeUnit.SECONDS).value();
if (productDb == null)
return null;
if (productDb == HttpStatus.CONFLICT)
return new GenericResponse<ProductViewModel>(null, HttpStatus.CONFLICT);
Product mappedProducts = mapper.convertValue(productDb, new TypeReference<Product>() {});
return new GenericResponse<ProductViewModel>(new ProductViewModel(mappedProducts.getId(), mappedProducts.getName(), mappedProducts.getPrice(), mappedProducts.getDescription(), mappedProducts.getVersion()), null);
}
Container configuration
@Bean
public ConsumerFactory<String, ProductViewModel> consumerFactoryAddUpdateProduct() {
return new DefaultKafkaConsumerFactory<>(consumerConfigs(),
new StringDeserializer(),
new JsonDeserializer<>(ProductViewModel.class));
}
@Bean
public KafkaListenerContainerFactory<?> addUpdateProductContainerFactory(ProducerFactory<String, Object> pf) {
ConcurrentKafkaListenerContainerFactory<String, ProductViewModel> factory =
new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactoryAddUpdateProduct());
factory.setReplyTemplate(kafkaTemplate(pf));
return factory;
}
Error
org.springframework.kafka.requestreply.KafkaReplyTimeoutException: Reply timed out
at org.springframework.kafka.requestreply.ReplyingKafkaTemplate.lambda$scheduleTimeout$3(ReplyingKafkaTemplate.java:339) ~[spring-kafka-2.5.2.RELEASE.jar:2.5.2.RELEASE]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[na:na]
at java.base/java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:264) ~[na:na]
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java) ~[na:na]
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]
答案1
得分: 1
这是一个已知的问题;我正在解决方案;请求/回复处理基于spring-messaging;当您消耗原始的ConsumerRecord
时,回复处理不起作用,因为消息逻辑被绕过。
解决方法是将方法更改为public Object UpdateProduct(Message<ProductViewModel> message)
。
头部包含在message.getHeaders()
中,键位于头部KafkaHeaders.MESSAGE_KEY
中。
ProductViewModel
就是message.getPayload()
。
英文:
This is a known problem; I am working on solution; the request/reply processing is based on spring-messaging; when you consume the raw ConsumerRecord
, the reply handing doesn't work because the messaging logic is bypassed.
The work around is to change the method to public Object UpdateProduct(Message<ProductViewModel> message)
instead.
The headers are contained in message.getHeaders()
, the key is in header KafkaHeaders.MESSAGE_KEY
.
The ProductViewModel
is message.getPayload()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论