dev@grizzly.java.net

Writing an Async Http Server (was Re: Grizzly and Axis2)

From: Jeanfrancois Arcand <Jeanfrancois.Arcand_at_Sun.COM>
Date: Thu, 22 May 2008 10:58:45 -0400

Salut,

changing the title so if people search our list, they find this email.

sumitsureka wrote:
> Hi Alexey,
>
> You are right on this:
>
> [As I understand you have scenario, where request processing takes a
> long time (some complex computation, big database requests etc) and
> you don't want processing thread block until the request will be
> processed... Is this correct?]
>
> Currently, my program get the control in the method service(Request request,
> Response response), say this is Thread-1, from where I pass the request to
> the next thread say Thread-2, which does the required processing. Meanwhile,
> the Thread-1 (service thread) is waiting for the request to get processed
> which in general may take long time.
>
> Due to high latency, I am unable to start the processing of the next
> request, which I do not want. I want something like that the Thread-1 should
> not wait for the response and should start accepting other requests. When
> Thread-2 completes processing the requests, then in that case Thread-2
> itseld should be able to provide the response through the same connection to
> the client.
>
> I hope, I am able to explain the scenario. Please let me know if there is
> anything that I have missed out.

Crystal clear :-)

What you need to use in this case if Grizzly ARP (Asynchronous Request
Processing). Mainly, you need to implement the following interface:

https://grizzly.dev.java.net/nonav/apidocs/com/sun/grizzly/http/AsyncFilter.html

A simple tutorial (written for Grizzly 1.0, but really easy to port) cam
be read here:

http://kasparov.skife.org/blog/src/java/grizzly-arp-basic.html

The idea here is inside your AsyncFilter, you decide when to suspend the
request. In your description above, your AsyncFilter will suspend the
request when Thread-2. Thread-2 will eventually call back your
AsyncFilter once the processing is completed, and the AsyncFilter will
resume the request.

As an exampled, here is how Comet is implemented in Grizzly via a
Asynchronous Request Processing's AsyncFilter:

https://grizzly.dev.java.net/nonav/xref/com/sun/grizzly/comet/CometAsyncFilter.html

or how Grizzly JRuby suspends request when no Rails runtime are unavailable:

https://grizzly.dev.java.net/nonav/xref/com/sun/grizzly/jruby/RubyRuntimeAsyncFilter.html

To enable Grizzly ARP, you just need to tell the SelectorThread which
AsyncFilter to use:

68 SelectorThread st = super.createSelectorThread(args);
69 st.setEnableAsyncExecution(true);
70 AsyncHandler asyncHandler = new DefaultAsyncHandler();
71 asyncHandler.addAsyncFilter(<<your AsyncFilter>>);
72 st.setAsyncHandler(asyncHandler);

Take a look and let us know how it goes.

Thanks

-- Jeanfrancois



>
> Regards,
> Sumit