gpt4 book ai didi

java - 从存储库返回实体时如何使用 CompletableFuture.thenCompose() ?

转载 作者:行者123 更新时间:2023-12-02 02:10:37 28 4
gpt4 key购买 nike

我开始使用CompletableFuture在 Spring Boot 中,我在某些地方看到常用的存储库方法返回 CompletableFuture <Entity>而不是Entity .

我不知道发生了什么,但是当我返回 CompletableFuture 的实例时在存储库中,代码运行完美。但是,当我返回实体时,代码不会异步工作,并且始终返回 null .

这是一个例子:

@Service
public class AsyncServiceImpl{
/** .. Init repository instances .. **/

@Async(AsyncConfiguration.TASK_EXECUTOR_SERVICE)
public CompletableFuture<Token> getTokenByUser(Credential credential) {
return userRepository.getUser(credential)
.thenCompose(s -> TokenRepository.getToken(s));
}
}

@Repository
public class UserRepository {

@Async(AsyncConfiguration.TASK_EXECUTOR_REPOSITORY)
public CompletableFuture<User> getUser(Credential credentials) {
return CompletableFuture.supplyAsync(() ->
new User(credentials.getUsername())
);
}
}

@Repository
public class TokenRepository {

@Async(AsyncConfiguration.TASK_EXECUTOR_REPOSITORY)
public CompletableFuture<Token> getToken(User user) {
return CompletableFuture.supplyAsync(() ->
new Token(user.getUserId())
);
}
}

前面的代码运行完美,但是后面的代码没有异步运行,结果始终是 null .

@Service
public class AsyncServiceImpl {
/** .. Init repository instances .. **/

@Async(AsyncConfiguration.TASK_EXECUTOR_SERVICE)
public CompletableFuture<Token> requestToken(Credential credential) {
return CompletableFuture.supplyAsync(() -> userRepository.getUser(credential))
.thenCompose(s ->
CompletableFuture.supplyAsync(() -> TokenRepository.getToken(s)));
}
}

@Repository
public class UserRepository {
@Async(AsyncConfiguration.TASK_EXECUTOR_REPOSITORY)
public User getUser(Credential credentials) {
return new User(credentials.getUsername());
}
}

@Repository
public class TokenRepository {
@Async(AsyncConfiguration.TASK_EXECUTOR_SERVICE)
public Token getToken(User user) {
return new Token(user.getUserId());
}
}

为什么第二个代码不起作用?

最佳答案

根据 the Spring @Async Javadoc :

the return type is constrained to either void or Future

它也是further detailed in the reference documentation :

In the simplest case, the annotation may be applied to a void-returning method.

[…]

Even methods that return a value can be invoked asynchronously. However, such methods are required to have a Future typed return value. This still provides the benefit of asynchronous execution so that the caller can perform other tasks prior to calling get() on that Future.

在第二个示例中,您的 @Async 注释方法不会返回 Future (或 ListenableFutureCompletableFuture code> 也受支持)。但是,Spring 必须异步运行您的方法。因此,它只能表现为您的方法具有 void 返回类型,因此它返回 null

顺便说一句,当您使用 @Async 时,您的方法已经异步运行,因此您不应在方法内使用 CompletableFuture.supplyAsync() 。您应该简单地计算结果并将其返回,包装在 CompletableFuture.completedFuture() 中如果需要的话。如果您的方法仅组合 future(例如您的服务仅组合异步存储库结果),那么您可能不需要 @Async 注释。另请参阅the example from the Getting Started guide .

关于java - 从存储库返回实体时如何使用 CompletableFuture.thenCompose() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50102090/

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