We got something really cool for Grizzly. It’s a Servlet AutoDeployer for GrizzlyWebServer.
Now that it said.. let’s talk more about that.
When you have servlets, you will need a AppServer or a WebServer to deploy your applications. Nothing new there.. it’s a been like that for years. Mostly you had to create a web.xml, put that in a war and deploy it.
There are others alternatives, like using GrizzlyWebServer, but you will have to put your web.xml into code.
GrizzlyWebServer ws = new GrizzlyWebServer(80);
try { ServletAdapter sa = new ServletAdapter(); sa.setContextPath("/");
// need to load JspRuntimeContext Class.forName("org.apache.jasper.compiler.JspRuntimeContext");
Servlet servlet = (Servlet)ClassLoaderUtil.load("ca.sebastiendionne.welcome_jsp"); sa.setServletInstance(servlet);
ws.addGrizzlyAdapter(sa, new String[]{}); ws.start();} catch (Exception e){ s_logger.error("Error Launching GrizzlyWebServer",e);}
What’s wrong with that ?
In my point of view.. it’s not efficient. I don’t want to use a WebServer to deploy a simple servlet and I don’t want code what I could simply extract from a config file.
That why a servlet autodeployer could be useful. Yes a WebServer or AppServer do that already, but like I said.. you can skip it and go for a smaller and faster solution.
The GrizzlyServletDeployer (or the super fast and super easy thing for GrizzlyWebServer) will do everything for you.
In one simple command line you can deploy multiple applications.
Let’s take a look at the command line first.
java -jar grizzly-servlet-deployer-1.9.11-SNAPSHOT.jar [options]
where options are :
-p : the port [default 8080]
-a : applications to deploy (everything is done from that)
You can deploy many things from the option “-a” (servlets, war, multiple wars, expanded wars). Let’s decribe what can be done.
Syntax examples :
# 1 : Deploy a war
java -jar grizzly-servlet-deployer-1.9.11-SNAPSHOT.jar -a c:/temp/hudson.war
That will deploy the war file. The same way a WebServer will deploy it. The web.xml will be read and the servlets will be deployed.
The context will be the war file. In this example : http://localhost:8080/hudson/
#2 – Deploy a expanded war.
java -jar grizzly-servlet-deployer-1.9.11-SNAPSHOT.jar -a c:/temp/hudson
Your application could be a expanded war, or have the same hierarchy.
That will give the same result as #1
#3 Deploy servlets
java -classpath myservlet.jar;.;grizzly-servlet-deployer-1.9.11-SNAPSHOT.jar com.sun.grizzly.http.servlet.deployer.GrizzlyWebServerDeployer -a web.xml
You can skip the step to create a war file if you want. Simply declare a web.xml and put the jars into the classpath (in this case the servlet is in myservlet.jar)
The context will be the default “/”
http://localhost:8080/
#4 – Deploy multiples war in the same time.
This one is more advanced. It will deploy all the war files or expanded wars that are in a root folder.
java -jar grizzly-servlet-deployer-1.9.11-SNAPSHOT.jar -a /MultipleWarFolder
(yes it’s the same syntax as #2)
Example :
Here what the folders will look like
...../MultipleWarFolder/ /freemarker /WEB-INF /classes/ /lib/ /templates/ /web.xml hudson.war struts2-showcase-2.0.12.war cometd-demo.war sebastiendionne-is-the-man.war
The folder /MultipleWarFolder contains 4 war files and 1 expanded folder.
Serlvet-Deployer will check all the folders in the root (/MultipleWarFolder) and check if it can apply #1 or #2.
If it find one that contains /WEB-INF/web.xml it will consider it as #2 (expanded war) and if it find a .war it will do #1 (war file).
The context for each application will be the folder name (freemarker) or the war file.
If you want to can use it in a testcase easily.
GrizzlyWebServerDeployer gws = new GrizzlyWebServerDeployer();
try {
args = new String[]{"-a","./web.xml"};
// ready to launch gws.init(args); gws.launch();
... launch your tests...
} catch (Exception e){ e.printStackTrace();}
I think when you will start using that, you will love it
GrizzlyWebServer is really powerful and so small. It’s a Asynchronous IO server that support multiples protocols : HTTP(s), Comet, PHP, JSP, Freemarker, AJP protocol (mod_jk) and more check (https://grizzly.dev.java.net/).