Fire and forget with reactor

后端 未结 2 1532
面向向阳花
面向向阳花 2020-12-06 08:54

I have a method like below in my Spring boot app.

public Flux search(SearchRequest request) {
  Flux<         


        
2条回答
  •  被撕碎了的回忆
    2020-12-06 09:30

    Have you considered running the processing in separate threads using publishOn like in the example below? This may not be exactly what you are asking for but allows you to continue with other matters while the processing of the results in the flux is done by one or more threads, four in my example, from a dedicated scheduler (theFourThreadScheduler).

        @Test
        public void processingInSeparateThreadTest() {
            final Scheduler theFourThreadScheduler = Schedulers.newParallel("FourThreads", 4);
            final Flux theResultFlux = Flux.just("one", "two", "three", "four", "five", "six", "seven", "eight");
    
            theResultFlux.log()
                .collectList()
                .publishOn(theFourThreadScheduler)
                .subscribe(theStringList -> {
                    doThisAsync(theStringList);
                });
    
            System.out.println("Subscribed to the result flux");
    
            for (int i = 0; i < 20; i++) {
                System.out.println("Waiting for completion: " + i);
                try {
                    Thread.sleep(300);
                } catch (final InterruptedException theException) {
                }
            }
        }
    
        private void doThisAsync(final List inStringList) {
            for (final String theString : inStringList) {
                System.out.println("Processing in doThisAsync: " + theString);
                try {
                    Thread.sleep(500);
                } catch (final InterruptedException theException) {
                }
            }
        }
    

    Running the example produce the following output, showing that the processing performed in doThisAsync() is performed in the background.

    Subscribed to the result flux
    Waiting for completion: 0
    Processing in doThisAsync: one
    Waiting for completion: 1
    Processing in doThisAsync: two
    Waiting for completion: 2
    Waiting for completion: 3
    Processing in doThisAsync: three
    Waiting for completion: 4
    Waiting for completion: 5
    Processing in doThisAsync: four
    Waiting for completion: 6
    Processing in doThisAsync: five
    Waiting for completion: 7
    Waiting for completion: 8
    Processing in doThisAsync: six
    Waiting for completion: 9
    Processing in doThisAsync: seven
    Waiting for completion: 10
    Waiting for completion: 11
    Processing in doThisAsync: eight
    Waiting for completion: 12
    Waiting for completion: 13
    Waiting for completion: 14
    Waiting for completion: 15
    Waiting for completion: 16
    Waiting for completion: 17
    Waiting for completion: 18
    Waiting for completion: 19
    

    References: Reactor 3 Reference: Schedulers

提交回复
热议问题