Saket
Saket

Reputation: 46157

Async calls from Spring MVC Controller

I am trying to make an asynchronous call from within my (Spring MVC based) Controller.

I am using the following snippet:

FutureTask<Object> runnableTask = new FutureTask<Object>(  
                new Runnable() {  
                    public void run() {  
                        // do something
                    }  

                }, null);  
runnableTask.run(); 

However, no matter what, this is not executed asynchronously, i.e., my Controller does not return back the response (to the view) until the above task completes. Am I missing something or is there an alternate way to do so?

Upvotes: 2

Views: 5348

Answers (1)

Bozho
Bozho

Reputation: 597362

You need an executor to do that:

Executor executor = Executors.newXx(..); //any executor, likely single-threaded
executor.submit(yourRunnable);
executor.shutdown();

But spring already has that (docs) - just make a method and annotate it with @Async (and have <task:annotation-driven /> in the xml)

Upvotes: 5

Related Questions