[英]How to Implement a Callback in Methods that uses @Async (Springboot App)
I have a need to implement a callback-like solution to be notified of the success/failure of method calls.我需要实现一个类似回调的解决方案来通知方法调用的成功/失败。 These methods I am calling are in a Springboot app and they are all asynchronous.我正在调用的这些方法在 Springboot 应用程序中,它们都是异步的。 More specifically, they all use the @Async
annotation.更具体地说,它们都使用@Async
注释。
I have not been successful in finding a solution to this and was wondering whether others may have some ideas.我没有成功找到解决方案,想知道其他人是否有一些想法。 I cannot use another async method (that is, I cannot change the existing code to remove the @Async
and use something in its place).我不能使用其他异步方法(也就是说,我不能更改现有代码以删除@Async
并在其位置使用某些东西)。 However, I can modify the method bodies themselves to add any callback capability.但是,我可以修改方法体本身以添加任何回调功能。
Thank you.谢谢你。
I think you can just create a new Interface with two methods, and then pass a implementation parameter to the async method.我认为您可以使用两个方法创建一个新接口,然后将实现参数传递给异步方法。
public interface TaskCallBack {
void success(int code);
void fail(int code);
}
Based on the suggestion of M. Derium (above) I am using CompletableFuture for my solution.根据 M. Derium(上图)的建议,我使用 CompletableFuture 作为我的解决方案。 I am still in the process of implementing and testing, but it seems to be a good solutions.我还在实施和测试过程中,但似乎是一个很好的解决方案。 Here is a good reference: Guide to CompletableFuture这是一个很好的参考: CompletableFuture 指南
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.