0
votes

I run the following simple example for a Jersey application on a Grizzly server:

@Path("example")
public class example {
    @POST
    public Response someMethod(@Context Request r) {
        System.out.println("Received request at : " + LocalDateTime.now());
        Response response = Response.ok().build();
        return response;
    }
}


public class GrizzlyServerMain {

    public static HttpServer startServer(String BASE_URI) throws IOException {

        // Scans for JAX-RS resources and providers in the specified code-package
        final ResourceConfig resourceConfig = new PackagesResourceConfig("Application");

        // create and start a new instance of grizzly http server exposing the Jersey application at BASE_URI
        return GrizzlyServerFactory.createHttpServer(BASE_URI, resourceConfig);
    }

    public static void main(String[] args) throws IOException {
        String BASE_URI = "http://0.0.0.0:8000";

        // Base URI the Grizzly HTTP server will listen on
        startServer(BASE_URI);

        //noinspection ResultOfMethodCallIgnored
        System.in.read();
    }
}

When I run it from IntelliJ, everything works as expected.

Connected to the target VM, address: '127.0.0.1:61163', transport: 'socket' Dec 30, 2016 4:00:07 PM com.sun.jersey.api.core.PackagesResourceConfig init INFO: Scanning for root resource and provider classes in the packages: Application Dec 30, 2016 4:00:08 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses INFO: Root resource classes found: class Application.Greetings class Application.example Dec 30, 2016 4:00:08 PM com.sun.jersey.api.core.ScanningResourceConfig init INFO: No provider classes found. Dec 30, 2016 4:00:08 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate INFO: Initiating Jersey application, version 'Jersey: 1.19.3 10/24/2016 03:43 PM' Dec 30, 2016 4:00:08 PM org.glassfish.grizzly.http.server.NetworkListener start INFO: Started listener bound to [0.0.0.0:8000] Dec 30, 2016 4:00:08 PM org.glassfish.grizzly.http.server.HttpServer start INFO: [HttpServer] Started Received request at : 2016-12-30T16:00:13.097 Received request at : 2016-12-30T16:00:14.319 Received request at : 2016-12-30T16:00:15.583 Disconnected from the target VM, address: '127.0.0.1:61163', transport: 'socket'

But when I create a JAR with all the dependencies, and as soon as I send the request from the client, I get:

WARNING: Exception during FilterChain execution java.lang.IllegalStateException at org.glassfish.grizzly.http.server.io.OutputBuffer.reset(OutputBuffer.java:217) at org.glassfish.grizzly.http.server.Response.reset(Response.java:732) at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:168) at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:265) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200) at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:134) at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112) at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:78) at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:815) at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:567) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:547) at java.lang.Thread.run(Unknown Source)

Can you please tell me what could I be doing wrong?

1
Your app appears dependent on a Glassfish server. Your jar doesn't include the server. I'm not sure how, or even if you'd want, to do this.Christopher Schneider
@ChristopherSchneider I wouldn't - thought when choosing "Build Artifacts" in IntelliJ, it puts everything the code needs in the JAR file. How did you come to check specifically this issue? I don't seem to see a clue to it in the exception message. PS, I think I see the Grizzly in the JAR file (study_jersey.jar\org\glassfish\grizzly - ZIP archive, unpacked size 9,992,959 bytes)Boris Milner
java.lang.IllegalStateException at org.glassfish.grizzly. You said you're trying to create a stand-alone jar, and I saw calls to Glassfish, a server.Christopher Schneider
@ChristopherSchneider I've opened the JAR archive and there is an org\glassfish\grizzly path inside it with Grizzly.Boris Milner
Possible duplicate of Grizzly and Jersey standalone jarDev-iL

1 Answers

1
votes

The exception seems to have changed from IllegalStateException to IllegalArgumentException.

Have a look at Java Doc for ContainerProvider.  ContainerProvider is an SPI class and its implementation will be provided by the server you are using, in your case the Grizzly server. As per Java Doc:
An implementation (a service-provider) identifies itself by placing a provider-configuration file (if not already present), "com.sun.jersey.spi.container.ContainerProvider" in the resource directory META-INF/services, and including the fully qualified service-provider-class of the implementation in the file..

So, I think you need to update "META-INF/services" to your jar and add the provider as "GrizzlyContainerProvider" (package qualified).

You can have a look at a similar post: Grizzly and Jersey standalone jar