users@grizzly.java.net

Re: Async or not async, this is the problem

From: Jeanfrancois Arcand <Jeanfrancois.Arcand_at_Sun.COM>
Date: Tue, 16 Dec 2008 18:24:43 -0500

Salut,

rama wrote:
>>
>
>
> Hello!
>
> tnx for you help, but i still need some assistance to figure why this
> piece of stupid code doesn't work as i suppose :D
>
>
> I can see the output ("RESUME" and "START") but i don't get any reply
> (ie rama stupid)
>
> basically, this is more or less what i have think that i will need to
> add to my application.

Since you don't invoke Response.resume(), then you need to add your
logic inside the cancelled() method. Something like:

> public void cancelled(GrizzlyAdapter attachment) {
> try {
> if (res.isSuspended()) {
> res.getWriter().write("ram not stupid");
> }
> } catch (Exception ex) {
> ex.printStackTrace();
> } finally {
> }
> }


>
> when a request is a push over pull req, i add the res to session, and
> wait until someone add to msg queue something.
> if someone add something to msg queue, res is resumed, and queue flushed
> out.
>

So you will eventually call resume, right?

>
> What i don't probably understand is why the request isn't executed.
> Notice that if i put a thread.sleep at the end of service function,
> request is executed.

Call resume() and it will be executed.

>
> But, in such a way, i will not block other request to be executed?

Hope that help.

A+

-- Jeanfrancois


>
>
>
> public class Webserver {
>
> public static void main(String[] args) {
> try {
> GrizzlyWebServer ws = new GrizzlyWebServer();
> ws.addGrizzlyAdapter(new GrizzlyAdapter() {
> public void service(GrizzlyRequest req, final GrizzlyResponse res) {
> FakeSession s = FakeSession.fake;
> s.res = res;
> s.start();
> res.suspend(1000, this, new CompletionHandler<GrizzlyAdapter>() {
> public void cancelled(GrizzlyAdapter arg0) {
>
>
> }
> public void resumed(GrizzlyAdapter arg0) {
> System.out.println("RESUME");
> try {
> res.getWriter().println("rama stupid");
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
> });
> }
> }, new String[]{});
> ws.start();
> } catch (IOException ex){
> // Something when wrong.
> }
> }
>
>
> static class FakeSession extends Thread {
> protected GrizzlyResponse res;
> private static FakeSession fake = new FakeSession();
>
>
> public void run() {
> try {
> System.out.println("START");
> Thread.sleep(1000);
> } catch (InterruptedException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> res.resume();
> }
> }
>
>
> }
>
>> Take a look at:
>>
>> http://www.nabble.com/-ANN--New-HTTP-API-for-Resuming-Suspending-Request-Response-tc20573149.html#a20573149