0

我对以下 quarkus/hibernate-reactive/mutiny 有一个非常令人困惑的问题。我将首先描述我在 Quarkus 中使用 hibernate-reactive 和 mutiny 实现的功能。

一个。任务是从数据库中检索记录,

  Uni<MyRecord> getAuthenticationRecord(String id);

湾。然后使用对象中的 refresh_token 字段并构建请求对象并将其传递给返回 CallableFuture 的第三方 API。

  CompletableFuture<TokenResponse> refreshToken(final TokenRequest tokenRequest);

最后检索值tokenRequest并更新在步骤 a 中检索到的记录。

我尝试了以下方法:

class MyApi {
  public Uni<AuthRecord> refreshToken(String owner) {

    MyRecord authRecord = getAuthenticationRecord(owner); //get the authentication record

    TokenResponse refreshToken = authRecord.onItem().transform(MyRecord::refreshToken)
    .chain(refreshToken -> {
        TokenRequest request = new TokenRequest(refreshToken); //create the request object
        return Uni.createFrom().completionStage(refreshToken(request)); //convert the CallableFuture to Uni 
    });


    //Join the unis and update the auth record
    return Uni.combine().all().unis(authRecord, refreshToken).asTuple().onItem().transform( 
      tuplle -> {
        var record = tuple.getItem1();
        var refresh = tuple.getItem2();

        record.setCode(refresh.getToken());
        return record.persistAndFlush();
      }
    );
  }
}

在测试用例中使用它:

@Inject
MyApi api;

@Test 
public void test1() {
  //This produces nothing
  api.refreshToken("owner").subscribe().with(
    item -> { 
      System.out.println(Json.encode(item));
    }
  )
}

@Test 
public void test2() {
  //This won't work because no transaction is active
  var record = api.refreshToken("owner").await().indefinitely();

}

@Test 
@ReactiveTransactional
public void test3() {
  //This won't work either because the thread is blocked @Blocking annotation didn't help either 
  var record = api.refreshToken("owner").await().indefinitely();

}

有什么建议么?

4

0 回答 0