简体   繁体   English

如何在使用@Async 的方法中实现回调(Springboot 应用程序)

[英]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.

相关问题 如何测试在 Springboot 中使用 Mapstruct 映射器的服务 class 方法? - How to test Service class methods which uses Mapstruct mappers in Springboot? 如何使用Square的Retrofit网络库实现异步回调 - How to implement an async Callback using Square's Retrofit networking library Java单元测试 - 如何使用mockito答案对异步方法(使用回调)进行单元测试 - Java unit testing - how to unit test an async method (uses callback) with mockito answer SpringBoot-2.1.3:使用 @Async 和 CompletableFuture 的并行方法调用 - SpringBoot-2.1.3: Parallel Methods Invocation with @Async with CompletableFuture 如何在springboot中使用@Async和@Scheduled注释? - How to use @Async with @Scheduled annotation in springboot? 如何使用 Springboot 实现 Impinj Octane SDK? - How to implement Impinj Octane SDK with Springboot? 如何在 java springboot 中实现集群设置的缓存? - How to implement caching for cluster setup in java springboot? 如何使用 springboot 2.3.0 实现 liveness/readines - How to implement liveness/readines using springboot 2.3.0 通过回调主请求实现外部服务的异步过程-Spring - Implement Async process for external service with callback to main Request - Spring 如何工作实现JNA回调? - how to work implement JNA callback?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM