I have one java program which process some business data and have to return response. But as part of backing up those data, I want to implement another method where it delivers that data to that method and asynchonously back up so that client does not have to wait for that backup operation to finish. Can someone suggest the better way to implement this? We are hitting more then 100 req/s and all the data processed needs to be backed up too.
-
If you are using Spring 3, you can use the @Async annotation on your asynchronous operation. See http://stackoverflow.com/questions/17167020/when-to-use-spring-async-vs-callable-controller-async-controller-servlet-3 for some ideas. – lreeder Sep 28 '13 at 15:42
-
Please provide more details like version of software (java, spring etc). This will help to answer your query, precisely. – CuriousMind Sep 28 '13 at 15:52
4 Answers
I guess you can make use of java.util.concurrent.ExecutorService

- 4,965
- 8
- 42
- 81
-
Problem I have with executorService is to shutdown as my main thread which has processed the request has returned the response. And executor is still doing background job, once it is done I want to shutdown that so that no threads are active, as i think this might cause some issue in prod app. Am I correct in this scenario? – user509755 Sep 28 '13 at 15:54
You should make use of java.util.concurrent.ExecutorService
to implement such functionality. It will give you much finer control over thread pool size and other aspects like timeout and wait.

- 3,143
- 3
- 29
- 54
Spring 3.0 @Async
allows you to run a task asynchronously. Follow this link to learn more about Spring Asynch task usage:
http://java-success.blogspot.in/2013/05/asynchronous-processiong-with-spring.html

- 67,789
- 12
- 98
- 136
It sounds like you need to seriously consider using some message queue technology like JMS (Hornet) or AMQP (RabbitMQ).
@Async
is nice for small chores but it does not scale and neither does a plain ExecutorService
. Not to mention your problem even sounds more like a message problem and not a simple task execution (backup and client notification). MQ theory/practice/implementation requires some reading so I recommend you look at Spring AMQP/JMS and general message queue documentation.
In terms of notifying the client take a look at the new spring mvc DeferredResult.

- 47,843
- 23
- 153
- 203