Search code examples
spring-webfluxproject-reactorreactorspring-webclient

Avoid multiple calls to API using spring webclient with flux


Both those calls are client http api:

Flux<SavingsViewFilter> views = savingsApi.getViewFilterSavings(viewId);
            Flux<Group> groups = groupsApi.getAllGroups();

and return a Flux

requestBodySpec.retrieve().bodyToFlux(returnType);

I need to filter element inside the Flux<Group>, based on values from eacg view.getGroupId()

        return views.flatMap(view ->
                groups
                        .filter(group -> Objects.equals(group.getGroupId(), view.getGroupId()))
                        .flatMap(group -> Flux.just(DepositAccount.builder()
                                .agencyName(group.getGroupName())
                                .settlementAccount(view.getName())
                                .build())));

It is working, but the problem is that it's doing for each view object, one more HTTP request to getAllGroups.

How can I avoid multiple requests to getAllGroups?


Solution

  • The issue is you are subscribing to groups for every element of views.

    You can use join to only subscribe to each once, and then join element by element.

    views
        //Join fluxes and create tuple for each pair
        .join(groups, s-> Flux.never(),s-> Flux.never(),Tuples::of)
        //Filter out any that don't have matching groupIds
        .filter(t -> t.getT1().getGroupId().equals(t.getT2().getGroupId()))
        //Use map, not flatMap since builder is not blocking
        .map(t -> DepositAccount.builder()
            .agencyName(t.getT2().getGroupName())
            .settlementAccount(t.getT1().getName())
            .build()
         );