-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathGreetingReactiveServiceImpl.java
48 lines (41 loc) · 1.2 KB
/
GreetingReactiveServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package meltdown.demo.greeting;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
@Service
@RequiredArgsConstructor
class GreetingReactiveServiceImpl implements GreetingReactiveService {
private final GreetingService greetingService;
/*
* Correct implementation
*/
public Mono<String> getGreeting() {
return Mono.fromCallable(greetingService::getGreeting)
// properly schedule above blocking call on
// scheduler meant for blocking tasks
.subscribeOn(Schedulers.boundedElastic());
}
/**
* Incorrect implementation.
* - Imposter Method (doing work before subscribing)
* - Blocking in event loop
*/
private Mono<String> getGreeting_imposter() {
try {
// blocking work performed during assembly, before subscribing
String greeting = greetingService.getGreeting();
return Mono.just(greeting);
} catch (Exception e) {
return Mono.error(e);
}
}
/**
* Incorrect implementation.
* - Blocking in event loop
*/
private Mono<String> getGreeting_blocking() {
// blocking call on event loop
return Mono.fromCallable(greetingService::getGreeting);
}
}