Reputation: 2238
I am a complete newbie into the reactive world, so while going through some of the examples on Reactive Repositories I found below:
reactiverepository.save(employee).subscribe() // subscribing to make the publisher emit the data
reactiverepository.findAll() // no subscribe() ?
I know in order for a publisher(Flux in the above case) to emit the data, we need to subscribe()
to it. But why don't we have the same subscribe()
calls in other CRUD methods like in the above example? What am I missing here?
Upvotes: 3
Views: 2569
Reputation: 14820
reactor works with the concept producer
and consumer
. The consumer
subscribes
to the producer
and nothing will happen until you subscribe
.
When using webflux
, which is basically spring-web
but implemented with reactor
the concept goes that the final consumer is usually the one subscribing
.
You can think of it as the database is the producer
and the calling client
(webpage, react app, mobile app) is the consumer
and when the client initiates a call to the backend, the client subscribes
to your server
and the server
in turn subscribes
to the database
(remember? producer, the database produces data).
So your server is basically just forwarding the data to the calling client, which means that usually you just take the data and return it out to the client, and the framework will handle the "subscription", automatically.
Which usually means, you should NOT subscribe in your application, instead, you should chain on the calls, and always keep the chain intact so that your server can produce items to the client.
So when should you subscribe
in your server?
Well one reason could be that you have a running job in your server that maybe fetches data from one server and inserts it into it's database. Then your service is the initiating client, subscribing to another server, the other server produces items to you, you receive them and insert them into your database.
but for instance in your example:
// first we fetch all, we filter, then we save, and we return back to the calling client keeping the chain intact, and no subscriptions
return reactiverepository.findAll()
.filter(employes -> //some criteria)
.flatMap(employee -> reactiverepository.save(employee));
Upvotes: 2
Reputation: 1
At the first, you should know about the hot and cold publishers. I'd suggest seeing this article
The code you write in the reactive programming world is a description of what you want to do (In the reactor world they called it assembly, I'd suggest seeing this issue to better understand the difference between Assembly and Execution) after the data is ready and will not run exactly after your code is executed.
The second thing to keep in mind is that you are always in a chain and you only need one subscription on this chain.
For example, suppose you want to get all the data from the repository and filter them with one field and then return it to the outside of the method. So you should do something like this:
return reactiveRepository.findAll().filter { //filter with one field } //or doing something different with your data when they fetched from DB
You should notice that until you don't subscribe on the chain, data doesn't go to be fetched from DB and your chain doesn't go to do anything.
Upvotes: 0