spring mvc - Difference between springmvc @Async, DeferredResult, Callable -
i've long-running task defined in spring service, started springmvc controller. want start service , return httpresponse caller before service ends. service saves file on file system @ end. in javascript i've created polling job check service status.
in spring 3.2 i've found @async annontation, don't understand how different deferredresult , callable. when have use @async? , when use deferredresult?
async annotates method going called asynchronously.
@org.springframework.stereotype.service public class myservice { @org.springframework.scheduling.annotation.async void dosomework(string url) { [...] } }
so spring need define how going executed. example:
<task:annotation-driven /> <task:executor id="executor" pool-size="5-10" queue-capacity="100"/>
this way when call service.dosomework("parameter") call put queue of executor called asynchronously. useful tasks executed concurrently.
you use async execute kind of asynchronous task. if want calling task periodically use @scheduled (and use task:scheduler instead of task:executor). simplified ways of calling java runnables.
deferredresult<> used answer petition without blocking tomcat http thread used answer. going return value responsebody annotated method.
@org.springframework.stereotype.controller { private final java.util.concurrent.linkedblockingqueue<deferredresult<string>> suspendedrequests = new java.util.concurrent.linkedblockingqueue<>(); @requestmapping(value = "/getvalue") @responsebody deferredresult<string> getvalue() { final deferredresult<string> result = new deferredresult<>(null, null); this.suspendedrequests.add(result); result.oncompletion(new runnable() { @override public void run() { suspendedrequests.remove(result); } }); service.setvalue(result); // sets value! return result; } }
the previous example lacks 1 important thing , it's doesn't show how deferred result going set. in other method (probably setvalue method) there going result.setresult(value). after call setresult spring going call oncompletion procedure , return answer http request (see https://en.wikipedia.org/wiki/push_technology#long_polling).
but if executing setvalue synchronously there no advantage in using deferred result.here async comes in hand. use async method set return value in point in future using thread.
@org.springframework.scheduling.annotation.async void setvalue(deferredresult<string> result) { string value; // time consuming actions [...] result.setresult(value); }
async not needed use deferred result, 1 way of doing it.
in example there queue of deferred results that, example, scheduled task monitoring process it's pending requests. use non blocking mechanism (see http://en.wikipedia.org/wiki/new_i/o) set returning value.
to complete picture search information java standard futures (http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/future.html) , callables (http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/callable.html) equivalent spring deferredresult , async.
Comments
Post a Comment