Reputation: 53
I am new to Spring Reactive programming and Spring Async methods. I have a doubt. By using spring webflux we can have reactive programming so that we can execute a particular piece of code and our current thread need not to wait for it to finish the execution. Similarly by using @async method we can make that particular piece of code run in a different thread so that our current thread wont wait.
So in these cases how webflux and async method are different and when to use which?
Upvotes: 3
Views: 2191
Reputation: 2707
Your question alomost mentions the difference, where you say in a different thread
Reason of Confusion: Webfux and @Async , both run Asynchronously from the thread they are being called.
Now Lets Drill Down Further..
Weblux, it runs asynchronously, but NOT because a new Thread is spawned.
It is build around publisher-subscriber pattern (Observer Pattern), which provides it the Asynchronous nature.
Upvotes: 1
Reputation: 72399
They're chalk and cheese - they're different enough that they're not really directly comparable.
The @Async
annotation simply executes the annotated method in a separate thread, and doesn't block on that thread before returning (doesn't wait for it to finish.) The normal use case for async is for fire-and-forget (or fire-and-read-later) scenarios, for long running tasks, in a traditional blocking environment.
Webflux on the other hand chucks out the traditional blocking threading model entirely, and instead serves every request through an event loop of just a handful of threads that must never block. To do so requires a fundamentally different style of writing code than a traditional blocking application. It's not just used for fire-and-forget scenarios, it becomes the default way you handle every request that comes into your application.
Upvotes: 2