|
| 1 | +package com.baeldung.debugging.consumer.chronjobs; |
| 2 | + |
| 3 | +import java.time.Duration; |
| 4 | +import java.util.concurrent.ThreadLocalRandom; |
| 5 | + |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.http.MediaType; |
| 10 | +import org.springframework.scheduling.annotation.Scheduled; |
| 11 | +import org.springframework.stereotype.Component; |
| 12 | +import org.springframework.web.reactive.function.client.WebClient; |
| 13 | + |
| 14 | +import com.baeldung.debugging.consumer.model.Foo; |
| 15 | +import com.baeldung.debugging.consumer.model.FooDto; |
| 16 | +import com.baeldung.debugging.consumer.service.FooService; |
| 17 | + |
| 18 | +import reactor.core.publisher.Flux; |
| 19 | + |
| 20 | +@Component |
| 21 | +public class ChronJobs { |
| 22 | + |
| 23 | + private static Logger logger = LoggerFactory.getLogger(ChronJobs.class); |
| 24 | + private WebClient client = WebClient.create("http://localhost:8081"); |
| 25 | + |
| 26 | + @Autowired |
| 27 | + private FooService service; |
| 28 | + |
| 29 | + @Scheduled(fixedRate = 10000) |
| 30 | + public void consumeInfiniteFlux() { |
| 31 | + Flux<Foo> fluxFoo = client.get() |
| 32 | + .uri("/functional-reactive/periodic-foo") |
| 33 | + .accept(MediaType.TEXT_EVENT_STREAM) |
| 34 | + .retrieve() |
| 35 | + .bodyToFlux(FooDto.class) |
| 36 | + .delayElements(Duration.ofMillis(100)) |
| 37 | + .map(dto -> { |
| 38 | + logger.debug("process 1 with dto id {} name{}", dto.getId(), dto.getName()); |
| 39 | + return new Foo(dto); |
| 40 | + }); |
| 41 | + Integer random = ThreadLocalRandom.current() |
| 42 | + .nextInt(0, 3); |
| 43 | + switch (random) { |
| 44 | + case 0: |
| 45 | + logger.info("process 1 with approach 1"); |
| 46 | + service.processFoo(fluxFoo); |
| 47 | + break; |
| 48 | + case 1: |
| 49 | + logger.info("process 1 with approach 1 EH"); |
| 50 | + service.processUsingApproachOneWithErrorHandling(fluxFoo); |
| 51 | + break; |
| 52 | + default: |
| 53 | + logger.info("process 1 with approach 2"); |
| 54 | + service.processFooInAnotherScenario(fluxFoo); |
| 55 | + break; |
| 56 | + |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Scheduled(fixedRate = 20000) |
| 61 | + public void consumeFiniteFlux2() { |
| 62 | + Flux<Foo> fluxFoo = client.get() |
| 63 | + .uri("/functional-reactive/periodic-foo-2") |
| 64 | + .accept(MediaType.TEXT_EVENT_STREAM) |
| 65 | + .retrieve() |
| 66 | + .bodyToFlux(FooDto.class) |
| 67 | + .delayElements(Duration.ofMillis(100)) |
| 68 | + .map(dto -> { |
| 69 | + logger.debug("process 2 with dto id {} name{}", dto.getId(), dto.getName()); |
| 70 | + return new Foo(dto); |
| 71 | + }); |
| 72 | + Integer random = ThreadLocalRandom.current() |
| 73 | + .nextInt(0, 3); |
| 74 | + switch (random) { |
| 75 | + case 0: |
| 76 | + logger.info("process 2 with approach 1"); |
| 77 | + service.processFoo(fluxFoo); |
| 78 | + break; |
| 79 | + case 1: |
| 80 | + logger.info("process 2 with approach 1 EH"); |
| 81 | + service.processUsingApproachOneWithErrorHandling(fluxFoo); |
| 82 | + break; |
| 83 | + default: |
| 84 | + logger.info("process 2 with approach 2"); |
| 85 | + service.processFooInAnotherScenario(fluxFoo); |
| 86 | + break; |
| 87 | + |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Scheduled(fixedRate = 20000) |
| 92 | + public void consumeFiniteFlux3() { |
| 93 | + Flux<Foo> fluxFoo = client.get() |
| 94 | + .uri("/functional-reactive/periodic-foo-2") |
| 95 | + .accept(MediaType.TEXT_EVENT_STREAM) |
| 96 | + .retrieve() |
| 97 | + .bodyToFlux(FooDto.class) |
| 98 | + .delayElements(Duration.ofMillis(100)) |
| 99 | + .map(dto -> { |
| 100 | + logger.debug("process 3 with dto id {} name{}", dto.getId(), dto.getName()); |
| 101 | + return new Foo(dto); |
| 102 | + }); |
| 103 | + logger.info("process 3 with approach 3"); |
| 104 | + service.processUsingApproachThree(fluxFoo); |
| 105 | + } |
| 106 | + |
| 107 | + @Scheduled(fixedRate = 20000) |
| 108 | + public void consumeFiniteFluxWithCheckpoint4() { |
| 109 | + Flux<Foo> fluxFoo = client.get() |
| 110 | + .uri("/functional-reactive/periodic-foo-2") |
| 111 | + .accept(MediaType.TEXT_EVENT_STREAM) |
| 112 | + .retrieve() |
| 113 | + .bodyToFlux(FooDto.class) |
| 114 | + .delayElements(Duration.ofMillis(100)) |
| 115 | + .map(dto -> { |
| 116 | + logger.debug("process 4 with dto id {} name{}", dto.getId(), dto.getName()); |
| 117 | + return new Foo(dto); |
| 118 | + }); |
| 119 | + logger.info("process 4 with approach 4"); |
| 120 | + service.processUsingApproachFourWithCheckpoint(fluxFoo); |
| 121 | + } |
| 122 | +} |
0 commit comments