Daniel Arechiga
Daniel Arechiga

Reputation: 967

What are the benefits of using BaseSubscriber hooks vs Subscriber methods in spring Reactor?

projectreactor.io/docs/core/release/reference documentation shows an implementation of BaseSubscriber as an alternative to using lambdas in a subscription, however by reading the reference docs: BaseSubscriber I fail to see how overriding any of the hooks achieves anything that overriding Subscriber base methods would not. Why should we use BaseSubscriber at all?

Upvotes: 0

Views: 920

Answers (1)

Simon Baslé
Simon Baslé

Reputation: 28301

BaseSubscriber implements book-keeping and guards in the Subscriber methods so you don't have to write that boilerplate and worry the boilerplate is correct / covering all bases (eg. protection against exceptions in onNext/onSubscribe/etc... and translation of said exceptions into onError signals).

Note that when you use the lambda-based versions of Flux#subscribe, it creates a pretty similar LambdaSubscriber with the same kind of boilerplate.

Then in both case you as the developer are only left with implementing the business logic. In one case, it's through provided lambdas, and in the case of BaseSubscriber it's through implementing the hookXxx methods.

The main difference between the lambda approach and the BaseSubscriber one is in lambdas you cannot access state other than the lambda's input parameter. With BaseSubscriber, we specifically capture the Subscription and the hooks methods can cancel() or request(n) that subscription.

Upvotes: 1

Related Questions