6

I Have some method inside my Controller which executes @Async task

@Async
public Future<String> getResultFromServer(){
    String result = ......
    return new AsyncResult<String>(result);
} 

The method execution time is up to 1o minutes. All I need to do is just to return result to client side which will be connected using AJAX/JQuery.

I don't want the client to request my server every second whether @Async method executed or not. I just want to keep my connection open and then just "push" result to Server.

@RequestMapping(value="/async.do", method=RequestMethod.POST)
public void getResult(HttpServletResponse res){
    String result = null;
    PrintWriter out = res.getWriter();
    Future<String> future = getResultFromServer();
    try {
        if (future.isDone())
            result = future.get();
        out.println(result);
        out.close();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}

I understand that this very close to Comit model, but I'm not familiar with comet in general.

My question is how can I keep connection open on client side using JavaScript/JQuery?

and will my @RequestMapping(value="/async.do", method=RequestMethod.POST) method push result to the client?

1
  • 1
    How did you solve this/part 2 eventually ? Commented Jul 13, 2015 at 4:12

1 Answer 1

3

The easiest way would be not to invoke the method in a asynchronous way, instead invoke it directly from the controller in a synchronous way.

Then the request will need to "wait" until the method result is calculated, and it can be returned as soon as it is created.

Of course this means, that the connection will be open for how long it takes do the job (1 minute).

Sign up to request clarification or add additional context in comments.

6 Comments

thank you for the answer and for the idea to execute method directly, I have another concern: As far as I understand, Tomcat proceeds my request in separate thread which opens for my HTTP connection. Does it mean that if my request proceeds for 15 minutes then Tomcat thread will run all that time? Does it mean that this operation requires 2 thread one for request and second for Async task? is there any way to suspend the first Thread while second thread (async task) executed?
If you use @async then you have two threads, one that invokes the method annotated by @async and returns instantly, and an other one that is really doing the stuff in the @async method. To suspend the first thread, simply invoke future.get(), this blocks the thread until the result is callculated. But if you do so: invoke the @async and call future.get() directly after wards, then the complete @ascync stuff is useless, because you simulating (almost) the normal behaviour of an method invocation with lots technical stuff.
@danny.lesnik: But if your calculation will take 15 minutes, then you really need an other solution than holding up a connection for this time span!
Yes I understand that, do You have any idea? I'm not sure If Spring MVC can work with Comet and if yes, how to implement it.
@danny.lesnik: post this as an new question
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.