users@grizzly.java.net

Re: Problem with Suspend/Resume

From: Oleksiy Stashok <oleksiy.stashok_at_oracle.com>
Date: Wed, 19 Jan 2011 18:17:13 +0100

Oh, btw, here are updated test sources for MyAdapter and MyExecutor I
can propose.

WBR,
Alexey.


package com.acme;

import com.sun.grizzly.tcp.Adapter;
import com.sun.grizzly.tcp.Request;
import com.sun.grizzly.tcp.Response;
import com.sun.grizzly.util.buf.ByteChunk;
import java.io.IOException;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
  *
  */
public class MyAdapter implements Adapter
{
     private ThreadPoolExecutor executor;
     private ByteChunk bc;
     private boolean suspendMode;

     public MyAdapter( boolean suspendMode )
         throws IOException
     {
         executor = new ThreadPoolExecutor( 0, 500, 5*60,
TimeUnit.SECONDS, new SynchronousQueue<Runnable>() );
         bc = new ByteChunk();
         bc.append( "pong".getBytes(), 0, 4 );
         this.suspendMode = suspendMode;
     }

     @Override
        public void service( Request request, Response response ) throws
Exception
     {
         if( suspendMode )
         {
             response.suspend();
             executor.execute( new MyExecutor(request,response,bc) );
         }
         else
         {
             response.setStatus( 200 );
             response.setMessage( "Sounds good" );
             response.doWrite( bc );
             response.flush();
         }
     }

     @Override
        public void afterService( Request request, Response response ) throws
Exception
     {
// request.recycle();
// response.recycle();
     }
}



package com.acme;

import com.sun.grizzly.tcp.Request;
import com.sun.grizzly.tcp.Response;
import com.sun.grizzly.util.buf.ByteChunk;

/**
  *
  * @author dGay
  */
public class MyExecutor implements Runnable
{
     Request req;
     Response res;
     ByteChunk bc;

     public MyExecutor( Request req, Response res, ByteChunk bc )
     {
         this.req = req;
         this.res = res;
         this.bc = bc;
     }

     @Override
     public void run()
     {
         try
         {
             // Thread.sleep( 5 );
             res.setStatus( 200 );
             res.setMessage( "Sounds good" );
             res.doWrite( bc );
             res.flush();

         }
         catch( Exception ex )
         {
             System.out.println( "\n\tException while writing
response !" );
             ex.printStackTrace( System.out );
             System.out.println();
         }
         finally
         {
             try
             {
                     res.resume();
             } catch (Exception ex)
             {
                 System.out.println( "\n\tException while resuming !" );
                 ex.printStackTrace( System.out );
                 System.out.println();
             }
         }
     }
}

On Jan 19, 2011, at 17:55 , Gay David (Annecy) wrote:

> Hi Alexey,
>
> Thanks you so much for the –very- quick response.
> I’ve open the issue : http://java.net/jira/browse/GRIZZLY-958
>
>
> Regards
> David
>
>
> De : Oleksiy Stashok [mailto:oleksiy.stashok_at_oracle.com]
> Envoyé : mercredi 19 janvier 2011 17:32
> À : Gay David (Annecy)
> Cc : users_at_grizzly.java.net
> Objet : Re: Problem with Suspend/Resume
>
> Hi David,
>
> I found the issue in Grizzly.
> Can you pls. submit a bug and I'll provide the fix and a bit updated
> test code asap
>
> Thanks.
>
> WBR,
> Alexey.
>
> On Jan 19, 2011, at 16:32 , Gay David (Annecy) wrote:
>
>
> Hi all,
>
> I’ve got a problem with Grizzly with the Suspend/Resume mode.
> I’ve write a small test trying to reproduce what’s happen sometimes
> in our server.
> (A whole Maven project is attached, just unzip and run ‘mvn clean
> test’ to reproduce it)
>
> Basically this test suspend the response and delegate the
> computation of the request to a Thread pool executor and then resume
> the request.
>
> While running the test, there are some error, most often there is a
> NPE here :
>
> java.lang.NullPointerException
> at com.sun.grizzly.tcp.Response.resume(Response.java:784)
> at com.acme.MyExecutor.run(MyExecutor.java:55)
> at java.util.concurrent.ThreadPoolExecutor
> $Worker.runTask(ThreadPoolExecutor.java:886)
> at java.util.concurrent.ThreadPoolExecutor
> $Worker.run(ThreadPoolExecutor.java:908)
> at java.lang.Thread.run(Thread.java:619)
>
> (I made the test on windows XP with JDK 1.6.0_21)
>
> I would like to know if someone has already see this problem ?
> Is what I am doing just crazy ? ;-)
> Do I misunderstand how to play with suspend/resume ?
> Is there a problem in Grizzly ?
>
> PS1 : I have better result if I add a small sleep here in the
> MyExecutor class :
>
> …
> @Override
> public void run()
> {
> try
> {
> Thread.sleep( 5 );
> res.setStatus( 200 );
> …
>
> It sounds like a synchronize problem ?
>
> PS2 : I also made the test on an Ubuntu, and I’ve other problems :
>
> GRAVE: GRIZZLY0038: HTTP Processing error.
> java.lang.NullPointerException
> at
> com
> .sun
> .grizzly.tcp.SuspendResponseUtils.attach(SuspendResponseUtils.java:61)
> at
> com.sun.grizzly.tcp.Response.suspend(Response.java:945)
> at
> com.sun.grizzly.tcp.Response.suspend(Response.java:884)
> at
> com.sun.grizzly.tcp.Response.suspend(Response.java:858)
> at
> com.sun.grizzly.tcp.Response.suspend(Response.java:841)
> at com.acme.MyAdapter.service(MyAdapter.java:34)
> at
> com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:
> 818)
> at
> com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
> at
> com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1008)
> at
> com
> .sun
> .grizzly
> .http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
> at
> com
> .sun
> .grizzly
> .DefaultProtocolChain
> .executeProtocolFilter(DefaultProtocolChain.java:137)
> at
> com
> .sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:
> 104)
> at
> com
> .sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:
> 90)
> at
> com
> .sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
> at
> com
> .sun
> .grizzly
> .ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
> at
> com
> .sun
> .grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
> at com.sun.grizzly.ContextTask.run(ContextTask.java:
> 71)
> at com.sun.grizzly.util.AbstractThreadPool
> $Worker.doWork(AbstractThreadPool.java:532)
> at com.sun.grizzly.util.AbstractThreadPool
> $Worker.run(AbstractThreadPool.java:513)
> at java.lang.Thread.run(Thread.java:662)
> 19 janv. 2011 16:25:40 com.sun.grizzly.http.ProcessorTask parseRequest
> GRAVE: GRIZZLY0041: Unblocking keep-alive exception.
> java.lang.NullPointerException
> at
> com
> .sun
> .grizzly
> .tcp.http11.InternalInputBuffer.fill(InternalInputBuffer.java:768)
> at
> com
> .sun
> .grizzly
> .tcp
> .http11
> .InternalInputBuffer.parseRequestLine(InternalInputBuffer.java:402)
> at
> com.sun.grizzly.http.ProcessorTask.parseRequest(ProcessorTask.java:
> 851)
> at
> com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:692)
> at
> com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1008)
> at
> com
> .sun
> .grizzly
> .http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
> at
> com
> .sun
> .grizzly
> .DefaultProtocolChain
> .executeProtocolFilter(DefaultProtocolChain.java:137)
> at
> com
> .sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:
> 104)
> at
> com
> .sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:
> 90)
> at
> com
> .sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
> at
> com
> .sun
> .grizzly
> .ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
> at
> com
> .sun
> .grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
> at com.sun.grizzly.ContextTask.run(ContextTask.java:
> 71)
> at com.sun.grizzly.util.AbstractThreadPool
> $Worker.doWork(AbstractThreadPool.java:532)
> at com.sun.grizzly.util.AbstractThreadPool
> $Worker.run(AbstractThreadPool.java:513)
> at java.lang.Thread.run(Thread.java:662)
>
>
> Thanks for any help or tips.
> Regards
> David
>
>
>
> <GrizzlyTest.zip>
>