how to safely shutdown executorService on webflux

555 views Asked by At
  1. I have created threadPool using executor service on webflux, but after completion of my flux task i want to shutdown the executor service.

  2. can you please let me know the safest way i can shutdown the executor service.

Code:

ExecutorService myPool = Executors.newFixedThreadPool(10); 
try {
        ParallelFlux<ParelleResponceBody> name = Flux.fromIterable(str).parallel(3)
                .runOn(Schedulers.fromExecutorService(myPool)).flatMap(s -> {
                    ParelleResponceBody bodyHolder = new ParelleResponceBody();
                    bodyHolder.setStr(s);
                    return Flux.fromStream(Stream.of(bodyHolder));
                });
            name.subscribe(s -> {
            System.out.println("parallel output : " + s.getStr());
        });
        return name;
    } finally {
        myPool.shutdown();
        System.out.println("checking thread : "+myPool.isShutdown());

    }

Exception:

checking thread : true
2020-09-28 12:02:10.614 ERROR 8684 --- [ctor-http-nio-2] a.w.r.e.AbstractErrorWebExceptionHandler : [da6f83ed-1]  500 Server Error for HTTP GET "/flux/parallelFluxexample/"

reactor.core.Exceptions$ReactorRejectedExecutionException: Scheduler unavailable
    at reactor.core.Exceptions.failWithRejected(Exceptions.java:268) ~[reactor-core-3.3.9.RELEASE.jar:3.3.9.RELEASE]
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    |_ checkpoint ⇢ Handler com.learnflux.controller.RequestHandler#convertListToMap() [DispatcherHandler]
    |_ checkpoint ⇢ HTTP GET "/flux/parallelFluxexample/" [ExceptionHandlingWebHandler]
Stack trace:
        at reactor.core.Exceptions.failWithRejected(Exceptions.java:268) ~[reactor-core-3.3.9.RELEASE.jar:3.3.9.RELEASE]
        at reactor.core.publisher.Operators.onRejectedExecution(Operators.java:985) ~[reactor-core-3.3.9.RELEASE.jar:3.3.9.RELEASE]
        at reactor.core.publisher.FluxPublishOn$PublishOnSubscriber.trySchedule(FluxPublishOn.java:326) ~[reactor-core-3.3.9.RELEASE.jar:3.3.9.RELEASE]
0

There are 0 answers