gpt4 book ai didi

KAFKA ListenableFuture spring- how to return client a response with object based on onsuccess method future.addCallback as onsuccess is void(Kafka ListenableFuture Spring-如何向客户端返回基于onSuccess方法future.addCallback的对象响应,因为onSuccess为空)

转载 作者:bug小助手 更新时间:2023-10-26 20:05:32 29 4
gpt4 key购买 nike



public class Test {

public StudentDto publishStudentDto

{
ListenableFuture<SendResult<String, Student>> future = this.studentKafkaTemplate.send(topicName, student);
future.addCallback(new ListenableFutureCallback<SendResult<String, Student>>() {
@Override
public void onSuccess(SendResult<String, Student> result) {
logger.info("Student created & message published to topic: {} with offset: {} to partition {}", student, result.getRecordMetadata().offset(), result.getRecordMetadata().partition());
}

@Override
public void onFailure(Throwable ex) {
logger.error("student not created. Error in publishing student to topic : " + student, ex);
}
});
}
}

I want to return StudentDto to controller if Student is published successfully on topic. But as onSuccess is called after the response is returned to controller. Is there is any way i can return StudentDto if it published successfully.

如果在主题上成功发布了学生,我想将StudentDto返回给控制器。但因为onSuccess是在响应返回给控制器之后调用的。如果StudentDto成功发布,我有什么方法可以将其返回到。


I want to return StudentDto to controller if Student is published successfully on topic. But as onSuccess is called after the response is returned to controller. Is there is any way i can return StudentDto if it published successfully.

如果在主题上成功发布了学生,我想将StudentDto返回给控制器。但因为onSuccess是在响应返回给控制器之后调用的。如果StudentDto成功发布,我有什么方法可以将其返回到。


更多回答
优秀答案推荐

Don't use an async listener, simply call get(...) on the future (with some timeout) and the thread will block until success or failure.

不要使用异步监听程序,只需调用Get(...)在将来(有一些超时),线程将被阻塞,直到成功或失败。



future.completable().join();

By using the above line of code and a small flag for updating the status, the main thread will wait until the callback is completed before exiting and you can compare the flag to return or leave it.

通过使用上面的代码行和一个用于更新状态的小标志,主线程将等待,直到回调完成后才退出,您可以比较标志以返回或离开它。


join() returns the result after the future is completed.

Join()在完成未来运算后返回结果。


更多回答

Thanks for the reply. Ideally get(...) will block the other request at a time(for the timeout set). Could you please suggest which is the better approach to send record to kafka async with callback listener or the get on the future.

谢谢你的回复。理想情况下获得(...)将一次阻止另一个请求(针对超时设置)。你能建议一下,用回拨监听器向卡夫卡发送记录更好的方法是哪一种,还是Get on the Future?

If you want to block the caller thread until the result is received, get is simplest. Async would mean you'd have to write code to block the caller and wake him when the result is received.

如果希望在收到结果之前阻止调用方线程,则最简单的方法是GET。异步将意味着您必须编写代码来阻止调用者,并在收到结果时将其唤醒。

Can you help me with this one as well. stackoverflow.com/questions/75780715/…

你能帮我把这件也做一下吗?STACKOVERFLOW.com/Questions/75780715/…

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com