users@jersey.java.net

Cant get Jersey running in embedded Grizzly

From: Alex Sherwin <alex.sherwin_at_gmail.com>
Date: Sat, 16 May 2009 08:21:34 -0400

I've got:

jersey bundle 1.0.3
grizzly-servlet-webserver 1.9.15a

(also, activation, commons lang, commons logging, jaxb api, jaxb impl,
jsr311 api)

If I follow the straight forward approach from the HelloWorld example,
everything works fine when running inside Eclipse (JDK 1.5.0_16).
However, once i export a jar and run it from a command line (either on
OS X JDK 1.5.0_16 or OpenSolaris JDK 1.6.0_06) the Grizzly web server
starts up, but once I make a web request and Jersey attempts to
initialize, it is failing to locate my HelloWorldResource class
(again, this works when run from inside Eclipse).

Everything (meaning both of my classes) are in jar, classpath is all
setup, etc etc...

There is no special run configuration setup in eclipse, just a basic
Run As > Java Application invocation, which is simply running my main
method with the classpath setup with my libs.

What could possibly be different between running from within Eclipse
and the command line that would only change the way that Jersey is
attempting/failing to locate my resource classes? Here are my two
test classes...

Note that I've tried configuring the grizzly web server two different
ways labeled with "Method 1" and "Method 2", both work from within
Eclipse, both fail the same way when run from the command line
(Grizzly starts up, but Jersey fails to find Resource classes when a
web request is made)

After generating my jar (jersey-grizzly.jar), it is run like this:

java -cp .:activation-1.1.1.jar:asm-3.1.jar:commons-lang-2.4.jar:commons-logging-1.1.1.jar:grizzly-servlet-webserver-1.9.15a.jar:jaxb-api-2.1.jar:jaxb-impl-2.1.11.jar:jersey-bundle-1.0.3.jar:jersey-grizzly.jar:jsr311-api-1.0.jar:junit-4.6.jar:log4j-1.2.15.jar
com.asherwin.server.GrizzlyServer

com.asherwin.server.GrizzlyServer.java
==============================

package com.asherwin.server;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

import com.sun.grizzly.http.StatsThreadPool;
import com.sun.grizzly.http.embed.GrizzlyWebServer;
import com.sun.grizzly.http.servlet.ServletAdapter;
import com.sun.jersey.api.core.PackagesResourceConfig;

public class GrizzlyServer {

  private static final Logger LOG = Logger.getLogger(GrizzlyServer.class);

  private static final String HOST = "localhost";
  private static final int PORT = 8080;

  private static final MyStatistics threadPool = new MyStatistics(10,
100, Integer.MAX_VALUE, 60, TimeUnit.SECONDS);

  public GrizzlyServer() {

  }

  public static void main(final String[] args) {

    final String baseUri = new
StringBuilder().append("http://").append(HOST).append(":").append(PORT).append("/").toString();

    LOG.info("Starting Grizzly [" + baseUri + "]");

    try {

      // Method 1
      // final Map<String, String> initParams = new HashMap<String, String>();
      // initParams.put(PackagesResourceConfig.PROPERTY_PACKAGES,
"com.asherwin.resource");
      // final SelectorThread threadSelector =
GrizzlyWebContainerFactory.create(baseUri, initParams);
      // threadSelector.setSelectorReadThreadsCount(5);
      // threadSelector.setThreadPool(threadPool);
      // threadSelector.enableMonitoring();

      // Method 2
      GrizzlyWebServer ws = new GrizzlyWebServer(PORT);
      ServletAdapter jerseyAdapter = new ServletAdapter();
      jerseyAdapter.setServletInstance(new
com.sun.jersey.spi.container.servlet.ServletContainer());
      jerseyAdapter.addInitParameter(PackagesResourceConfig.PROPERTY_PACKAGES,
"com.asherwin.resource");
      ws.addGrizzlyAdapter(jerseyAdapter, new String[] { "/" });
      ws.getSelectorThread().enableMonitoring();
      ws.getSelectorThread().setThreadPool(threadPool);
      ws.start();

      // System.in.read();

      boolean done = false;
      while (!done) {

        // LOG.debug("activeCount [" + threadPool.getActiveCount() +
"], poolSize [" + threadPool.getPoolSize() + //
        // "], largestPoolSize [" // + threadPool.getLargestPoolSize()
+ "], taskCount [" + threadPool.getTaskCount() +
        // "], completedTaskCount [" // +
threadPool.getCompletedTaskCount() + "]");

        LOG.debug(threadPool.getStats());

        Thread.sleep(2000);
      }

      // Method 2
      ws.stop();

      // Method 1
      // threadSelector.stopEndpoint();
    } catch (IOException e) {
      LOG.error("Caught IOException [" + e.getMessage() + "]", e);
    } catch (InterruptedException e) {
      LOG.error("Caught InterruptedException [" + e.getMessage() + "]", e);
    }

    System.exit(0);

  }

  public static class MyStatistics extends StatsThreadPool {

    public MyStatistics(int corePoolSize, int maximumPoolSize, int
maxTasksCount, long keepAliveTime, TimeUnit unit) {
      super(corePoolSize, maximumPoolSize, maxTasksCount, keepAliveTime, unit);
    }

    public String getStats() {
      final StringBuilder sb = new StringBuilder();

      sb.append("[Queued = ");
      sb.append(getStatistic().getCountQueued());

      sb.append(", Total Queued = ");
      sb.append(getStatistic().getCountTotalQueued());

      sb.append(", Peak Queued = ");
      sb.append(getStatistic().getPeakQueued());

      sb.append(", Open Connections = ");
      sb.append(getStatistic().getOpenConnectionsCount());

      sb.append(", Total Connections = ");
      sb.append(getStatistic().getCountTotalConnections());

      sb.append(", Max Queued = ");
      sb.append(getStatistic().getMaxQueued());

      sb.append(", Queue Size Bytes = ");
      sb.append(getStatistic().getQueueSizeInBytes());

      sb.append("]");
      return sb.toString();
    }
  }
}


com.asherwin.resource.HelloWorldResource.class
=======================================

package com.asherwin.resource;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Path("/helloworld")
public class HelloWorldResource {

  public HelloWorldResource() {

  }

  @GET
  public Response helloWorld() {
    return Response.ok("Hello World").build();
  }

}


-- 
Alexander Sherwin