Skip to content

Commit 3d2f6ef

Browse files
rozagerardomaibin
authored andcommitted
[BAEL-2278] spring-5-reactive | debugging reactive streams (eugenp#5789)
* First approach for the scenario, not stable yet. * Added 2 services for teh article Consumer is the main example project, with all the debugging functionality * * cleaned unused spring-5-data functionality * cleaning unused spring-5-data pom * *fixed indentation error * addressed Jira comments: * Created live test, and renamed unit test as integrtion test * Tried to fix issue in CI, couldnt reproduce locally
1 parent 71bca76 commit 3d2f6ef

18 files changed

Lines changed: 679 additions & 2 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.debugging.consumer;
2+
3+
import java.util.Collections;
4+
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.scheduling.annotation.EnableScheduling;
10+
import org.springframework.security.config.web.server.ServerHttpSecurity;
11+
import org.springframework.security.web.server.SecurityWebFilterChain;
12+
13+
import reactor.core.publisher.Hooks;
14+
15+
@SpringBootApplication(exclude = MongoReactiveAutoConfiguration.class)
16+
@EnableScheduling
17+
public class ConsumerSSEApplication {
18+
19+
public static void main(String[] args) {
20+
Hooks.onOperatorDebug();
21+
SpringApplication app = new SpringApplication(ConsumerSSEApplication.class);
22+
app.setDefaultProperties(Collections.singletonMap("server.port", "8082"));
23+
app.run(args);
24+
}
25+
26+
@Bean
27+
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
28+
http.authorizeExchange()
29+
.anyExchange()
30+
.permitAll();
31+
return http.build();
32+
}
33+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.debugging.consumer.controllers;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
import reactor.core.publisher.Hooks;
7+
8+
@RestController
9+
public class ReactiveConfigsToggleRestController {
10+
11+
@GetMapping("/debug-hook-on")
12+
public String setReactiveDebugOn() {
13+
Hooks.onOperatorDebug();
14+
return "DEBUG HOOK ON";
15+
}
16+
17+
@GetMapping("/debug-hook-off")
18+
public String setReactiveDebugOff() {
19+
Hooks.resetOnOperatorDebug();
20+
return "DEBUG HOOK OFF";
21+
}
22+
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.debugging.consumer.model;
2+
3+
import java.util.concurrent.ThreadLocalRandom;
4+
5+
import org.springframework.data.annotation.Id;
6+
7+
import lombok.AllArgsConstructor;
8+
import lombok.Data;
9+
10+
@Data
11+
@AllArgsConstructor
12+
public class Foo {
13+
14+
@Id
15+
private Integer id;
16+
private String formattedName;
17+
private Integer quantity;
18+
19+
public Foo(FooDto dto) {
20+
this.id = (ThreadLocalRandom.current()
21+
.nextInt(0, 100) == 0) ? null : dto.getId();
22+
this.formattedName = dto.getName();
23+
this.quantity = ThreadLocalRandom.current()
24+
.nextInt(0, 10);
25+
}
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.debugging.consumer.model;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
6+
@Data
7+
@AllArgsConstructor
8+
public class FooDto {
9+
10+
private Integer id;
11+
private String name;
12+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.debugging.consumer.service;
2+
3+
import java.util.concurrent.ThreadLocalRandom;
4+
5+
import com.baeldung.debugging.consumer.model.Foo;
6+
7+
import reactor.core.publisher.Flux;
8+
9+
public class FooNameHelper {
10+
11+
public static Flux<Foo> concatAndSubstringFooName(Flux<Foo> flux) {
12+
flux = concatFooName(flux);
13+
flux = substringFooName(flux);
14+
return flux;
15+
}
16+
17+
public static Flux<Foo> concatFooName(Flux<Foo> flux) {
18+
flux = flux.map(foo -> {
19+
String processedName = null;
20+
Integer random = ThreadLocalRandom.current()
21+
.nextInt(0, 80);
22+
processedName = (random != 0) ? foo.getFormattedName() : foo.getFormattedName() + "-bael";
23+
foo.setFormattedName(processedName);
24+
return foo;
25+
});
26+
return flux;
27+
}
28+
29+
public static Flux<Foo> substringFooName(Flux<Foo> flux) {
30+
return flux.map(foo -> {
31+
String processedName;
32+
Integer random = ThreadLocalRandom.current()
33+
.nextInt(0, 100);
34+
35+
processedName = (random == 0) ? foo.getFormattedName()
36+
.substring(10, 15)
37+
: foo.getFormattedName()
38+
.substring(0, 5);
39+
40+
foo.setFormattedName(processedName);
41+
return foo;
42+
});
43+
}
44+
45+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.debugging.consumer.service;
2+
3+
import java.util.concurrent.ThreadLocalRandom;
4+
5+
import com.baeldung.debugging.consumer.model.Foo;
6+
7+
import reactor.core.publisher.Flux;
8+
9+
public class FooQuantityHelper {
10+
11+
public static Flux<Foo> processFooReducingQuantity(Flux<Foo> flux) {
12+
flux = flux.map(foo -> {
13+
Integer result;
14+
Integer random = ThreadLocalRandom.current()
15+
.nextInt(0, 90);
16+
result = (random == 0) ? result = 0 : foo.getQuantity() + 2;
17+
foo.setQuantity(result);
18+
return foo;
19+
});
20+
return divideFooQuantity(flux);
21+
}
22+
23+
public static Flux<Foo> divideFooQuantity(Flux<Foo> flux) {
24+
return flux.map(foo -> {
25+
Integer result = Math.round(5 / foo.getQuantity());
26+
foo.setQuantity(result);
27+
return foo;
28+
});
29+
}
30+
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.debugging.consumer.service;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import com.baeldung.debugging.consumer.model.Foo;
7+
8+
import reactor.core.publisher.Flux;
9+
10+
public class FooReporter {
11+
12+
private static Logger logger = LoggerFactory.getLogger(FooReporter.class);
13+
14+
public static Flux<Foo> reportResult(Flux<Foo> input, String approach) {
15+
return input.map(foo -> {
16+
if (foo.getId() == null)
17+
throw new IllegalArgumentException("Null id is not valid!");
18+
logger.info("Reporting for approach {}: Foo with id '{}' name '{}' and quantity '{}'", approach, foo.getId(), foo.getFormattedName(), foo.getQuantity());
19+
return foo;
20+
});
21+
}
22+
23+
public static Flux<Foo> reportResult(Flux<Foo> input) {
24+
return reportResult(input, "default");
25+
}
26+
}

0 commit comments

Comments
 (0)