injecting grizzly response in jersey application resource

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

injecting grizzly response in jersey application resource

Sören Altona
Hi,

I'm using a grizzly HTTP server to run a Jersey application where I try to write to the NIOOutputStream.
I'm injecting org.glassfish.grizzly.http.server.Request and org.glassfish.grizzly.http.server.Response instances into the Jersey application resource like this:

@Path("test")
public class TestResource
{
       @Inject
       private Provider<org.glassfish.grizzly.http.server.Request> requestProvider;

      @Inject
      private org.glassfish.grizzly.http.server.Response response;

      @GET
      @Produces("text/html")
      public void test()
      {
            final NIOOutputStream out = response.getNIOOutputStream();
            response.suspend();
            out.notifyCanWrite(new WriteHandler()
            {
                long now = System.currentTimeMillis();
                long waitTime = new Random().nextInt(500);

                @Override
                public void onWritePossible() throws Exception
                {
                       if (now + waitTime < System.currentTimeMillis())
                       {
                            out.write(new String("done").getBytes());
                            response.resume();
                       }
                       else
                       {
                            out.notifyCanWrite(this);
                       }
                }
           }
      }
}

When executing a request against /test I'm getting following exception from grizzly framework:

Dec 08, 2015 2:02:10 PM org.glassfish.grizzly.http.server.HttpHandler doHandle

WARNING: GRIZZLY0200: Service exception
java.lang.IllegalStateException: Internal org.glassfish.grizzly.http.server.Response has not been set
at org.glassfish.grizzly.http.server.Response.checkResponse(Response.java:1951)
at org.glassfish.grizzly.http.server.Response.setStatus(Response.java:1587)
at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer$ResponseWriter.writeResponseStatusAndHeaders(GrizzlyHttpContainer.java:266)
at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:683)
at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:444)
at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:434)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:329)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)


WARNING: GRIZZLY0013: Exception during FilterChain execution
java.lang.NullPointerException
at org.glassfish.grizzly.http.server.HttpServerFilter.afterService(HttpServerFilter.java:380)

at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:260)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)

I'm also getting following warning from jersey when starting the HTTP server:

WARNING: The following warnings have been detected: HINT: A HTTP GET method, public void TestResource.test(), returns a void type. It can be intentional and perfectly fine, but it is a little uncommon that GET method returns always "204 No Content".

The same code is working as expected when running in a org.glassfish.grizzly.http.server.HttpHandler implementation.

public class TestHandler extends HttpHandler
{
        @Override
        public void service(Request request, Response response) throws Exception
        {
            ...
        }
}

Is there anything I can do to get this working using a Jersey resource?
It seems to be related to https://java.net/jira/browse/JERSEY-2462 but the problem should be resolved.
I'm using jersey 2.22.1 (jersey-container-grizzly2-http) and grizzly 2.3.23.
Any help would be appreciated.
Thanks,

Soeren

Reply | Threaded
Open this post in threaded view
|

Re: injecting grizzly response in jersey application resource

oleksiys
Administrator
Hi Soeren,

I believe you see the issue because you try to use Grizzly asynchronous request processing within the synchronous Jersey resource.
Please take a look how to create async Jersey resource [1], you'd probably need to use it to combine it with Grizzly async.

Please let me know if you have further questions.

WBR,
Alexey.

[1] https://jersey.java.net/documentation/latest/async.html

On 12/8/15 5:43 AM, Sören Altona wrote:
Hi,

I'm using a grizzly HTTP server to run a Jersey application where I try to write to the NIOOutputStream.
I'm injecting org.glassfish.grizzly.http.server.Request and org.glassfish.grizzly.http.server.Response instances into the Jersey application resource like this:

@Path("test")
public class TestResource
{
       @Inject
       private Provider<org.glassfish.grizzly.http.server.Request> requestProvider;

      @Inject
      private org.glassfish.grizzly.http.server.Response response;

      @GET
      @Produces("text/html")
      public void test()
      {
            final NIOOutputStream out = response.getNIOOutputStream();
            response.suspend();
            out.notifyCanWrite(new WriteHandler()
            {
                long now = System.currentTimeMillis();
                long waitTime = new Random().nextInt(500);

                @Override
                public void onWritePossible() throws Exception
                {
                       if (now + waitTime < System.currentTimeMillis())
                       {
                            out.write(new String("done").getBytes());
                            response.resume();
                       }
                       else
                       {
                            out.notifyCanWrite(this);
                       }
                }
           }
      }
}

When executing a request against /test I'm getting following exception from grizzly framework:

Dec 08, 2015 2:02:10 PM org.glassfish.grizzly.http.server.HttpHandler doHandle

WARNING: GRIZZLY0200: Service exception
java.lang.IllegalStateException: Internal org.glassfish.grizzly.http.server.Response has not been set
at org.glassfish.grizzly.http.server.Response.checkResponse(Response.java:1951)
at org.glassfish.grizzly.http.server.Response.setStatus(Response.java:1587)
at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer$ResponseWriter.writeResponseStatusAndHeaders(GrizzlyHttpContainer.java:266)
at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:683)
at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:444)
at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:434)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:329)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)


WARNING: GRIZZLY0013: Exception during FilterChain execution
java.lang.NullPointerException
at org.glassfish.grizzly.http.server.HttpServerFilter.afterService(HttpServerFilter.java:380)

at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:260)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)

I'm also getting following warning from jersey when starting the HTTP server:

WARNING: The following warnings have been detected: HINT: A HTTP GET method, public void TestResource.test(), returns a void type. It can be intentional and perfectly fine, but it is a little uncommon that GET method returns always "204 No Content".

The same code is working as expected when running in a org.glassfish.grizzly.http.server.HttpHandler implementation.

public class TestHandler extends HttpHandler
{
        @Override
        public void service(Request request, Response response) throws Exception
        {
            ...
        }
}

Is there anything I can do to get this working using a Jersey resource?
It seems to be related to https://java.net/jira/browse/JERSEY-2462 but the problem should be resolved.
I'm using jersey 2.22.1 (jersey-container-grizzly2-http) and grizzly 2.3.23.
Any help would be appreciated.
Thanks,

Soeren