小编Dan*_*tor的帖子

如何与 Project Reactor 并行调用两个或多个 Web 服务或 REST 并加入答案

您好,我想知道如何在 pararelo 中调用两个或多个 Web 服务或 Rest 服务并编写对调用的响应。

我在网上找到了一些使用其他技术的例子,但我无法让它与反应器一起工作

// start task A asynchronously
CompletableFuture<ResponseA> futureA = asyncServiceA.someMethod(someParam);
// start task B asynchronously
CompletableFuture<ResponseB> futureB = asyncServiceB.someMethod(someParam);

CompletableFuture<String> combinedFuture = futureA
        .thenCombine(futureB, (a, b) -> a.toString() + b.toString());

// wait till both A and B complete
String finalValue = combinedFuture.join();
Run Code Online (Sandbox Code Playgroud)

///////////////////////////////////////////////// //////////////////////////////

static void Run()
{
    //Follow steps at this link for addding a reference to the necessary .NET library:
    //http://stackoverflow.com/questions/9611316/system-net-http-missing-from-    
    //namespace-using-net-4-5

    //Create an HTTP Client
    var client = new HttpClient(); …
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot project-reactor

7
推荐指数
2
解决办法
8813
查看次数

如何在条件下使用“zipWhen”?

您好 我想知道如何使用反应堆项目的“zipwhen”功能,因为文档中没有明确说明。

我的问题是以下我对 Rest/Web 服务进行了一系列调用,问题是,因为我现在拥有它,我总是调用所有服务,但我想参数化以便不总是调用所有服务,如果它没有取决于使用 zipwhen 的需要。

我真的不知道是否可以使用 zipwhen。

就像是 :

我有一个类服务,我在其中调用不同的 Web 服务(因为它是一个示例,所以它总是相同的,但实际上它将调用 n 个不同的 Web 服务)

@Service
public class FuenteRiesgoService {

    @Autowired
    SmartBearWsClient smartBearWsClient;

    public Mono<GetCurrentTimeResponse> fuenteWS1() {

        return Mono.fromCallable(() -> {
            Random rn = new Random();
            System.out.println("Llamando a fuente WS1 ..... on " + Thread.currentThread().getName());
            Thread.sleep(rn.nextInt(50 - 1 + 1) + 1 * 10000);
            GetCurrentTimeResponse getCurrentTimeResponse = smartBearWsClient.getCurrentTime(new GetCurrentTime());
            System.out.println("Respuesta fuente WS1 " + getCurrentTimeResponse.getGetCurrentTimeResult() + " on "
                    + Thread.currentThread().getName());
            return getCurrentTimeResponse;
        }).subscribeOn(Schedulers.parallel());

    }

    public Mono<GetCurrentTimeResponse> …
Run Code Online (Sandbox Code Playgroud)

java spring project-reactor spring-webflux

5
推荐指数
1
解决办法
6769
查看次数

使用 Project Reactor 并行调用其余 Web 服务?

我想知道如何对 REST 或 Web 服务进行多个并行调用,然后加入响应并在调用 @RestController 的响应中发送它。

类似于使用 ComparableFuture 构建的以下代码,但使用 Reactor(Flux, Mono)。

CompletableFuture<Company> companyCompletableFuture = CompletableFuture.supplyAsync(() -> {
     return  Company.find.where().eq("id", id).findUnique();  
});

CompletableFuture<List<Domain>> domainsCompletableFuture = CompletableFuture.supplyAsync(() -> {
     return Domain.find.where().eq("company_id", id).findList();
});

// wait for all the data
CompletableFuture allDoneFuture = CompletableFuture.allOf(companyCompletableFuture, domainsCompletableFuture);

allDoneFuture.get(); // wait for all done
company = companyCompletableFuture.get();
domain = domainsCompletableFuture.get()
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot project-reactor

2
推荐指数
1
解决办法
3933
查看次数