I am using Asynchronous Servlets to process requests,
According to Docs:(complete(),dispatch())
╔══════════════════╦═══════════════════════════════════════════════════════════╗
║ void complete() ║ Completes the asynchronous operation and closes the ║
║ ║ response associated with this asynchronous context. ║
║ ║ You call this method after writing to the response object ║
║ ║ inside the asynchronous context. ║
╠══════════════════╬═══════════════════════════════════════════════════════════╣
║ void dispatch() ║ Dispatches the request and response objects ║
║ ║ of this AsyncContext to the servlet container. ║
╚══════════════════╩═══════════════════════════════════════════════════════════╝
I failed to understand async.dispatch(), async.complete(), and how they work.
I have few doubts on this:
- What exactly the difference between
async.dispatch()
andasync.complete()
? - When I called dispatch()(inside run()) the response reaches the client, that mean we can push the response asynchronously?
- if I call
asyncContext.dispatch()
first andasyncContext.complete()
next what is the behaviour of the thread? - If I call any method after
asyncContext.complete()
, what will happen to that method call, as shown below code(in the samerun()
)?
[when I tested this it's working fine and showing same thread id] - If I am calling Asynchronous methods inside
run()
should I need to completeasyncContext.complete()
inside callback()? (onSuccess() or onFailure()) Whether any help(example source/books/online help) available regarding this? (Async Servlets and Futures Combination)
final FutureCallback<Void> calculateTime= new CustomFuture<>(calculate); // start Async context. final AsyncContext asyncContext = request.startAsync(); asyncContext.start(new Runnable() { @Override public void run() { MyObject object= null; try { object= myFactory.create(); //dispatch async context asyncContext.dispatch("Object Successfully Created"); } catch (final IOException e1) { logger.error("logging error"); } asyncContext.complete(); //complete async context // call asynchronous method final ListenableFuture<Void> future = myService.doSomething(); Futures.addCallback(future, calculateTime); // calling asyncContext.complete() here will work? } });
Thanks in Advance.