0

Two Springboot microservices need to interact with each other to achieve mass emailing feature. Microservice 1: CollectorService collects information about clients and Microservice 2: NotificationService to send documents via SMTP service. NotificationService is unable to handle the load that CollectorService produces. Now I want to understand how NotificationService can signal CollectorService to slow down. My code looks like below currently.

CollectorSerivce calling NotificationService

final WebClient client = WebClient.builder().baseUrl(BASE_URL.get()).defaultHeaders(httpHeaders -> {
            httpHeaders.set("requestId", requestId);
        }).build();

            Mono<List> responseFlux = client.post().uri(EXTERNAL_API_PATH.get() + "/withattFluxBackPressure")
                    .contentType(MediaType.APPLICATION_JSON)
                    .body(BodyInserters.fromValue(message)).retrieve().bodyToMono(List.class);

NotificationService processing requests

 @PostMapping(value = "/externalNotification/withattFluxBackPressure")
    public List<NotificationResponse> sendMailMessageWAttFlux(@RequestBody List<EmailTemplate> mailMessages) throws Exception{
        log.info("Processing email sending request async...");

       
//        byte[] byteArrayResource = get file content from S3
        List<NotificationResponse> response = new ArrayList<>();

        Flux<NotificationResponse> responseFlux = Flux.fromIterable(mailMessages).flatMap(messages -> {
            try {
                return Flux.just(notificationService.sendMailWAtt(messages, byteArrayResource).map(NotificationResponse::error).orElse(NotificationResponse.success()));
            } catch (IOException e) {
                log.error("Exception", e);
                return Flux.just(new NotificationResponse().error(e.getMessage()));
            }
        });

        responseFlux.subscribe(new BaseSubscriber<NotificationResponse>() {
                    @Override
                    protected void hookOnSubscribe(Subscription subscription) {
                        log.info("called hookOnSubscribe......");
                        subscription.request(1);
                    }

                    @Override
                    protected void hookOnNext(NotificationResponse value) {
                        log.info("called hookOnNext.......{} ", value);
                        response.add(value);
                        request(1);

                    }

                    @Override
                    protected void hookOnComplete() {
                        log.info("called hookOnComplete.......");
                    }

                    @Override
                    protected void hookOnError(Throwable throwable) {
                        log.info("called hookOnError.......");
                        if(throwable instanceof ConnectException) {
                            log.error("called ConnectException.......");
                        }
                        if(throwable instanceof ResourceAccessException) {
                            log.error("called ResourceAccessException.......");
                        }
                        if(throwable instanceof ConnectTimeoutException) {
                            log.error("called ConnectTimeoutException.......");
                        }
                        if(throwable instanceof io.netty.channel.ConnectTimeoutException) {
                            log.error("called netty ConnectTimeoutException.......");
                        }
                    }
                });

        return response;
}```

 1. When NotificationService overloads, how can it signal(backpressure) CollectorService to slow down? (Ideal scenario)
 2. Alternatively, NotificationService processes 5 emails then signal/request CollectorService for the next 5.

Thanks for your time!

0 Answers0