What’s up with Grizzly code and examples?

I was excited to read that the Grizzly Java server framework has had an update as I am currently trying to evaluate embeddable HTTP servers for several projects.

I downloaded the grizzly http server jar and clicked through to the associated “Getting Started!” page of example code.

It seemed so simple: knock together a fresh Eclipse project, and create a new test class containing the example code:

package test;
 
import java.io.IOException;
 
import junit.framework.TestCase;
 
import com.sun.grizzly.http.embed.GrizzlyWebServer;
import com.sun.grizzly.tcp.http11.GrizzlyAdapter;
import com.sun.grizzly.tcp.http11.GrizzlyRequest;
import com.sun.grizzly.tcp.http11.GrizzlyResponse;
 
public class GrizzlyHTTPTest extends TestCase {
  public void testServer() {
    GrizzlyWebServer ws = new GrizzlyWebServer("/var/www");
    try{
      ws.addGrizzlyAdapter(new GrizzlyAdapter(){  
        public void service(GrizzlyRequest request, GrizzlyResponse response){
          try {
            response.getWriter().println("Grizzly is soon cool");
          } catch (IOException ex) {            
          }
        }
      });
      ws.start();
    } catch (IOException ex){
      ex.printStackTrace();
    }
  }
}

Once I had found the right includes etc. it compiled happily. Although I was a little worried that the “addGrizzlyAdapter” method appears as deprecated. So I ran it.

It ran. Or more to the point, it ran and finished. My test gave a me a green bar, when I was expecting a hung process while it waited to serve pages. When I tested it with a browser (trying several common ports) I got no response.

I am now thoroughly puzzled, This is completely different behaviour to any server framework I have tried so far, and the “Getting Started!” page offers no help.

Does anyone know how I could start a Grizzly HTTP server and have it run for long enough to actually serve some pages?

Comments (5) left to “What’s up with Grizzly code and examples?”

  1. Curt Cox wrote:

    It is probably more than you’re looking for and recent changes seem to have broken the Maven goal that actually runs things, but…

    1. Download the Jersey samples.
    http://download.java.net/maven/2/com/sun/jersey/samples/jersey-samples/1.0.1/jersey-samples-1.0.1-project.zip
    2. I found the easiest way to get started was NetBeans 6.5 with the Maven plugin installed.
    3. Load the Helloworld example.
    4. Comment out the whole exec-maven-plugin plugin.
    5. Run the Main file. In the output, you will see:
    [exec:exec]
    Starting grizzly…
    Jersey app started with WADL available at http://localhost:9998/application.wadl
    Try out http://localhost:9998/helloworld

    I’m coming at this from the other side. I want to use JAX-RS, and Grizzly looks like it will allow me to easily include more extensive tests on the build server.

  2. Frank wrote:

    Thanks, I’ll give that a try.

  3. Jeanfrancois Arcand wrote:

    Salut, it must be as simple as your wrote, but we deprecated this API recently and I suspect that the issue. I don’t have access to my computer now, but just replace addGrizzlyAdapter(…., new String[]{“/”}); to see if that works. I will update the blog as soon as I’m back to my computer :-) . A+
    – Jeanfrancois

  4. Alexey wrote:

    I tried your code on Grizzly 1.9.0, and it works fine for me.
    Can you pls. check:
    1) if port 8080 is not occupied by any other application.
    2) try to run application outside Eclipse.

    If it will not help – can you pls. provide the details: OS, JDK version?

  5. Frank wrote:

    Thanks Guys. That has certainly provided me with extra information. If I put a “main” in the same class as the above code

      public static void main(String[] args) {
    	  new GrizzlyHTTPTest().testServer();
      }

    and run it (even from Eclipse) it works exactly as I would expect.

    But if I run it from JUnit it drops out immediately. It does not even appear to try and bind the port – it does not complain even if the “main” version is already running.

    Does anyone have any unit tests which run up a server? This is a vital feature for me as I develop all my web apps using Test-Driven Development.

Post a Comment

*Required
*Required (Never published)