实现回调在Springboot Java微服务中

huangapple go评论68阅读模式
英文:

Implement callback in Springboot Java microservices

问题

已存在情况: 非UI Java 客户端程序调用基于 REST 的 Web 服务 API。请求包括回调 URL 和唯一标识符。除了请求架构外,对客户端没有更多了解。

要求: 服务器应作为 Java Spring Boot 微服务开发,提供 CRUD 操作的 REST API。
服务器端:创建请求需要很长时间才能完成,因此不能是同步的。在接收到请求后,服务器执行基本的架构验证,启动一些异步任务(例如调用外部服务或可能需要时间的数据库操作),然后将响应返回给客户端。
每当异步任务可用结果时,服务器会使用结果数据和操作状态调用回调 URL。

服务器实现为使用 Spring-Web MVC 框架的 Spring Boot Web 服务。

问题: 在 Spring 中生成异步任务可以使用线程执行器和 CompletableFuture,或者使用 @Async 注解。哪一种方法更推荐?
并且当异步任务完成时,如何通过回调 URL 调用客户端 API 返回结果数据?

英文:

Pre-existing : Non-UI Java client program invokes REST-based web service API. The request has a callback_url and a unique identifier. Nothing more is known about the client except for the request schema.

Requirement : Server is to be developed as Java Springboot microservices exposing CRUD operations in REST APIs.
Server-side: the create request takes a long time to complete. Hence it cannot be synchronous. Upon receiving the request, the server does basic schema validation, starts some asynchronous tasks (like calls to external services or database operations which can take time) and returns response to client.
Whenever the result is available from the async tasks, the server invokes callbackUrl with the result data and status of the operation.

The server is implemented as Springboot webservice using Spring-Web MVC framework.

Question : Spawning async tasks in Spring can be done by using Thread Executors and CompletableFuture or by @Async annotation. Which one is recommended?
And when async task is completed, how do I invoke the client API via callback url to return the result data?

答案1

得分: 1

我认为CompletableFuture和@Async都可以很好地工作。我更喜欢@Async,因为它更符合Spring在大多数功能中的工作方式。

您可以配置@Async的线程执行器,类似于以下方式:

@Configuration
@EnableAsync
public class SpringAsyncConfig implements AsyncConfigurer {
   @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.initialize();
        return threadPoolTaskExecutor;
    }
}

关于第二个问题,我认为您可以使用Spring的RestTemplate或WebClient在异步方法完成时调用callback_url。

在这两种情况下,无论是@Async还是CompletableFuture,您都可以使用Future类来验证处理是否完成。

Future<Object> future = asyncService.asyncMethod();
英文:

I think both CompletableFuture and @Async work well. I prefer @Async because it is more the way Spring works in most of its features.

You can configure the Thread Executor for the @Async with something like that:

@Configuration
@EnableAsync
public class SpringAsyncConfig implements AsyncConfigurer {
   @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.initialize();
        return threadPoolTaskExecutor;
    }
}

About the second question, I think you can use the spring RestTemplate or WebClient to make the call to the callback_url when the async method finishes.

In both cases, with @Async or Completable Future, you can use the Future class to verify when the processing is done.

Future&lt;Object&gt; future = asyncService.asyncMethod();

huangapple
  • 本文由 发表于 2023年6月5日 01:36:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定