users@grizzly.java.net

Re: Async or not async, this is the problem

From: rama <rama.rama_at_tiscali.it>
Date: Tue, 16 Dec 2008 23:56:18 +0100

>


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.

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.


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.

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



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