- Spring 5.0 Projects
- Nilang Patel
- 324字
- 2021-07-02 12:35:06
Reactor lifecycle methods
Reactor provides lifecycle methods to capture various events happening in publisher-subscriber communication. Those lifecycle methods are aligned with Reactive Streams specification. Reactor lifecycle methods can be used to hook custom implementation for a given event. Let's understand how that works with the following code:
public class ReactorLifecycleMethods {
public static void main(String[] args) {
List<String> designationList = Arrays.asList(
"Jr Consultant","Associate Consultant","Consultant",
"Sr Consultant","Principal Consultant");
Flux<String> designationFlux = Flux.fromIterable(designationList);
designationFlux.doOnComplete(
() -> System.out.println("Operation Completed ..!!"))
.doOnNext(
value -> System.out.println("value in onNext() ->"+value))
.doOnSubscribe(subscription -> {
System.out.println("Fetching the values ...!!");
for(int index=0; index<6;index++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
subscription.request(1);
}
})
.doOnError(
throwable-> {
System.out.println("Opps, Something went wrong ..!! "
+throwable.getMessage());
})
.doFinally(
(signalType->
System.out.println("Shutting down the operation, Bye ..!! "
+signalType.name())))
.subscribe();
}
We are creating the Flux object with data from a list and then calling various lifecycle methods, like doOnComplete(), doOnNext(), doOnSubscribe(), doOnError(), and doOnTerminate() in a chain. Finally, we call the subscribe() method, which does not consume the events, but all lifecycle methods will be executed as appropriate events are triggered.
This is similar to the custom subscriber implementation in the Custom subscribers section. You will see a similar output. The details of these lifecycle methods are as follows:
- doOnComplete(): Once all the data is received by the Subscriber, this method will be called.
- doOnNext(): This method will listen to the value event coming from the producer.
- doOnSubscribe(): Used to plug Subscription. It can control the backpressure by defining how many more elements are required with a subscription.request() call.
- doOnError(): If any error occurs, this method will be executed.
- doOnTerminate(): Once the operation is completed, either successfully or with error, this method will be called. It will not be considered on a manual cancellation event.
- doOnEach(): As the name suggests, it will be called for all Publisher events raised during stream processing.
- doFinally(): This will be called on stream closures due to error, cancellation, or successful completion of events.
- EJB 3.1從入門到精通
- 數(shù)字烏托邦
- 中小型局域網(wǎng)組建、管理與維護(hù)實(shí)戰(zhàn)
- 網(wǎng)絡(luò)安全應(yīng)急響應(yīng)技術(shù)實(shí)戰(zhàn)
- 網(wǎng)絡(luò)基礎(chǔ)與網(wǎng)絡(luò)管理項(xiàng)目化教程
- IPv6網(wǎng)絡(luò)切片:使能千行百業(yè)新體驗(yàn)
- Echo Quick Start Guide
- 網(wǎng)管第一課:網(wǎng)絡(luò)操作系統(tǒng)與配置管理
- 轉(zhuǎn)化:提升網(wǎng)站流量和轉(zhuǎn)化率的技巧
- 5G時(shí)代的大數(shù)據(jù)技術(shù)架構(gòu)和關(guān)鍵技術(shù)詳解
- Getting Started with tmux
- Building RESTful Web Services with .NET Core
- 從物聯(lián)到萬聯(lián):Node.js與樹莓派萬維物聯(lián)網(wǎng)構(gòu)建實(shí)戰(zhàn)
- 企業(yè)網(wǎng)絡(luò)組建與維護(hù)項(xiàng)目式教程
- 目標(biāo)跟蹤中的群智能優(yōu)化方法