Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Reactive MVC vs @EnableAsync

I'm a newbie to Spring Reactive Modules. What I got is basically, at its core it enable reactive programming and we can develop end to end reactive service.

But, suppose I just want to make my controller as Async, so that I can do work on multiple threads and send a reply like "Task Started" (not particularly this) and have my work going on and close the HTTP link.

I also got to know about @EnableAsync and @Async to make a method Async.

What if I just use @Async above my controller method that I want to make async. It worked but, is this a good practice? And can we use this in production codes?

like image 618
Nikhil Pareek Avatar asked Jan 05 '18 04:01

Nikhil Pareek


People also ask

Is WebFlux better than Spring MVC?

Moreover, Spring WebFlux supports reactive backpressure, so we have more control over how we should react to fast producers than both Spring MVC Async and Spring MVC. Spring Flux also has a tangible shift towards functional coding style and declarative API decomposition thanks to Reactor API behind it.

Is Spring MVC reactive?

A Basic Introduction To Spring WebFlux Introduction Spring WebFlux, like SpringMVC, provides reactive, async, non-blocking programming support for web applications in an annotated Controller style.

What is difference between Spring WebFlux and MVC?

The main difference between the two frameworks is that spring-mvc is based on thread pools, while spring-webflux is based on event-loop mechanism. Both the models support commonly used annotations such as @Controller . A developer can run a reactive client from a spring-mvc controller to make calls to remote services.

Can I use Spring MVC and WebFlux together?

both infrastructure will compete for the same job (for example, serving static resources, the mappings, etc) mixing both runtime models within the same container is not a good idea and is likely to perform badly or just not work at all.


1 Answers

I do not see any problem using @Asyncas this will release the request thread. But this is a simple approach and it has a lot of limitations. Note that if you want to deal with reactive streams, you do not have an API capable of that. For instance, if an @Async method calls another, the second will not be async.

The Webflux instead will bring the most complete API (in Java) to deal with things the reactive way. What you cannot do with only @Async. With Flux, for instance, you can process or access with multiple layers reactively and this you cannot reach the way you doing.

Nevertheless, it will bring a new universe for you, so if you just want to release the thread of the request, your approach is just fine, but if you need more you will have to deal with it in a more complex way.

Now, if you want to answer the HTTP request and then do the work asyncly, this is not what you want. I recommend that you have a JMS provider (like ActiveMQ), where your controller sends the message to be processed by a job and answers the request.

Hope it helps!

like image 167
Fabiano Avatar answered Sep 23 '22 03:09

Fabiano