users@grizzly.java.net

Re: Async or not async, this is the problem

From: rama <rama.rama_at_tiscali.it>
Date: Wed, 17 Dec 2008 00:48:51 +0100

|you have find a bug :-) Just add response.flushBuffer() after your
write and that will works. I will fix that ASAP.

>
> I've missed that one. Can you increate the suspend timeout (put 10
> 000) as I suspect you have a race here.
>
>


sure, here is a new "version" of the test
As you can see, the sleep in the thread is 10.000, i have "removed"
the static from fakeSession, and create a new one (this is just a test)
i got the "start" when fakesession thread startup, and i got the
"resume" when fakesession thread resume the request...but i don't get
nothing
on output....


and, after adding the flushbuffer, i got...

Exception in thread "Thread-2" java.lang.NullPointerException
        at com.sun.grizzly.http.DefaultProcessorTask.action
(DefaultProcessorTask.java:1049)
        at com.sun.grizzly.tcp.Response.action(Response.java:253)
        at com.sun.grizzly.tcp.Response.doWrite(Response.java:653)
        at com.sun.grizzly.tcp.http11.GrizzlyOutputBuffer.realWriteBytes
(GrizzlyOutputBuffer.java:468)
        at com.sun.grizzly.util.buf.ByteChunk.flushBuffer(ByteChunk.java:433)
        at com.sun.grizzly.util.buf.C2BConverter.flushBuffer
(C2BConverter.java:197)
        at com.sun.grizzly.tcp.http11.GrizzlyOutputBuffer.realWriteChars
(GrizzlyOutputBuffer.java:667)
        at com.sun.grizzly.util.buf.CharChunk.flushBuffer(CharChunk.java:467)
        at com.sun.grizzly.tcp.http11.GrizzlyOutputBuffer.doFlush
(GrizzlyOutputBuffer.java:418)
        at com.sun.grizzly.tcp.http11.GrizzlyOutputBuffer.flush
(GrizzlyOutputBuffer.java:401)
        at com.sun.grizzly.tcp.http11.GrizzlyWriter.flush(GrizzlyWriter.java:
133)
        at Webserver$1.service(Webserver.java:27)
        at Webserver$1$1.resumed(Webserver.java:43)
        at Webserver$1$1.resumed(Webserver.java:1)
        at com.sun.grizzly.tcp.Response$ResponseAttachment.resume
(Response.java:901)
        at com.sun.grizzly.tcp.Response.resume(Response.java:738)
        at com.sun.grizzly.tcp.http11.GrizzlyResponse.resume
(GrizzlyResponse.java:1555)
        at Webserver$FakeSession.run(Webserver.java:72)


public class Webserver {

        public static void main(String[] args) {
                try {
                        
                                GrizzlyWebServer ws = new GrizzlyWebServer();
                                ws.addGrizzlyAdapter(new GrizzlyAdapter() {
                                        public void service(final GrizzlyRequest req, final
GrizzlyResponse res) {
                                                                if (res.isSuspended()) {
                                                                        try {
                                                                                res.getWriter().println("rama stupid");
                                                                                res.flushBuffer();
                                                                        } catch (IOException e) {
                                                                                e.printStackTrace();
                                                                        }
                                                                        return;
                                                                }
                                                                System.out.println("REQ "+req.getRequestURI()+"
"+Thread.currentThread().toString());
                                                                FakeSession s = new FakeSession();
                                                                s.res = res;
                                                                s.start();
                                                                res.suspend(50000, this, new CompletionHandler<GrizzlyAdapter>
() {
                                                                        public void cancelled(GrizzlyAdapter arg0) {
                                                                                
                                                                        }
                                                                        public void resumed(GrizzlyAdapter arg0) {
                                                                                System.out.println("RESUME");
                                                                                arg0.service(req, res);
                                                                                try {
                                                                                        arg0.afterService(req, res);
                                                                                } catch (Exception e) {
                                                                                        // TODO Auto-generated catch block
                                                                                        e.printStackTrace();
                                                                                }
                                                                        }
                                                        });

                                        }
                                }, new String[]{});
                                ws.start();
         } catch (IOException ex){
                             // Something when wrong.
         }
        }
        
        static class FakeSession extends Thread {
                protected GrizzlyResponse res;
                
                public void run() {
                        try {
                                System.out.println("START");
                                Thread.sleep(10000);
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                        res.resume();
                }
        }
                
}