Home > Uncategorized > GlassFish v3 Extreme Makeover using GrizzlyAdapter part 1: Hello World

GlassFish v3 Extreme Makeover using GrizzlyAdapter part 1: Hello World

GlassFish v3 offer a lot of extensibility point, and one of them is quite interesting: GrizzlyAdapter. Any applications developed using the GrizzlyAdapter API can be deployed inside v3 and transform a fish into a monster…an extreme makeover!

IMG_0433.JPG

As Jerome described, GlassFish v3 is built on top of Grizzly, yet up to now, it was not possibly to deploy a native GrizzlyAdapter directly inside V3. Technically, at startup GlassFish runtime (OSGi or not) bootstrap and instantiate a Grizzly’s SelectorThread, one of the main entry point when embedding Grizzly (more info here I,II,II,IV, V, VI). Next it configure the special GrizzlyAdapter called ContainerMapper. That mapper is responsible for mapping request to its associated container (GrizzlyAdapter). It is not always clear in the GlassFish official documentation (they hide the monster ;-)), but many cool features of v3 are GrizzlyAdapter: JRuby, Groovy, admin CLI support, automatic download of admin gui, etc. So technically it was “easy” to extend that functionality and allow any GrizzlyAdapter to be deployed. It also means that any GrizzlyAdapter can now be deployed in GlassFish and gets nice features like admin support, monitoring, etc., features that are not always available in Grizzly itself.

So let’s start simple. Let’s write an HelloWord example. The code looks like

  1 
  2 import com.sun.grizzly.tcp.http11.GrizzlyAdapter;
  3 import com.sun.grizzly.tcp.http11.GrizzlyRequest;
  4 import com.sun.grizzly.tcp.http11.GrizzlyResponse;
  5 import java.io.IOException;
  6 
  7 public class HelloWorldAdapter extends GrizzlyAdapter {
  8 
  9     @Override
 10     public void service(GrizzlyRequest request, GrizzlyResponse response) {
 11         try {
 12             response.getWriter().println("HelloWorld");
 13         } catch (IOException ex) {
 14             ex.printStackTrace();
 15         }
 16     }
 17 }

In order to be deployed in GlassFish v3, let’s just jar this class, and add under META-INF a file called grizzly-glassfish.xml which tells GlassFish that this jar is a Grizzly application, and which context path will map to this application:

<adapters>
    <adapter context-root="/helloworld" class-name="com.sun.grizzly.http.HelloWorldAdapter"/>
</adapters>

Now just deploy this mini monster using GlassFish v3 admin cli:

% ${glassfish.home}/bin/asadmin deploy helloworld.jar

The v3 log will looks like:

INFO: The Admin Console is already installed, but not yet loaded.
9-Mar-2009 5:15:35 PM com.sun.enterprise.v3.server.AppServerStartup run
INFO: GlassFish v3  startup time : Felix(1257ms) startup services(1162ms) total(2419ms)
9-Mar-2009 5:15:37 PM  
INFO: Deployment expansion took 4
9-Mar-2009 5:15:37 PM org.glassfish.deployment.admin.DeployCommand execute
INFO: Deployment of helloworld done is 368 ms

Now let add complexity to our GrizzlyAdapter by adding some properties. Let’s customize the returned message:

  1 package com.sun.grizzly.http;
  2 
  3 import com.sun.grizzly.tcp.http11.GrizzlyAdapter;
  4 import com.sun.grizzly.tcp.http11.GrizzlyRequest;
  5 import com.sun.grizzly.tcp.http11.GrizzlyResponse;
  6 import java.io.IOException;
  7 
  8 public class HelloWorldAdapter extends GrizzlyAdapter {
  9 
 10     private String helloWorld = "HelloWorld";
 11 
 12     @Override
 13     public void service(GrizzlyRequest request, GrizzlyResponse response) {
 14         try {
 15             response.getWriter().println(helloWorld);
 16         } catch (IOException ex) {
 17             ex.printStackTrace();
 18         }
 19     }
 20 
 21     /**
 22      * Set the returned message
 23      */
 24     public void setMessage(String helloWorld){
 25         this.helloWorld = helloWorld;
 26     }
 27 
 28 }

Now to customize the message, let’s add a new property in our grizzly-config.xml file:

<adapters>
    <adapter context-root="/helloworld" class-name="com.sun.grizzly.http.HelloWorldAdapter">
        <property name="message" value="I like GrizzlyAdapter!"/>
    </adapter>
</adapters>

Freaking simple :-). So the sky is the limit…any Grizzly based application can now be deployed in v3. One of them can be found here. That project aim is to develop a GrizzlyAdapter acting as a proxy…which means you can transform v3 into a proxy! Note that you don’t need to anything else installed (just the smallest distribution of v3). Another example is are Grizzlet, which are simple POJO for doing Comet based application. Since the support for Grizzlet was made from a GrizzlyAdapter, you can always “jar|war” your Grizzlet and deploy them in v3 now. An example can be downloaded here. The grizzly-glassfisn.xml looks like:

<adapters>
    <adapter context-root="/JMakiGrizzlet" class-name="com.sun.grizzly.container.GrizzletAdapter">
        <property name="grizzletName" value="com.sun.grizzly.grizzlet.JMakiGrizzlet"/>
        <property name="rootFolder" value="../domains/domain1/applications/"/>
    </adapter>
</adapters>

This is just the beginning, but I recommend you follow Jerome’s blog on what can also be extended in v3…as they will also apply to GrizzlyAdapter based application!! For any questions, post them to users@grizzly.dev.java.net or tweet us here.

_uacct = “UA-3111670-1″;
urchinTracker();

technorati:

About these ads
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 50 other followers

%d bloggers like this: