We are building web services using Grizzly and Jersey and learning along the way by doing web searches. We noticed two distinct way of launching a grizzly webserver, but do not see whether one way is better than the other. Can anyone help, please?
1 – using GrizzlyWebContainerFactory:
public static void main(String[] args) throws IOException {
Map initParams = new HashMap();
initParams.put( "com.sun.jersey.config.property.packages", "net.gilstraps.server" );
SelectorThread selector = GrizzlyWebContainerFactory
.create( "http://localhost:9998/", initParams ); //noinspection ResultOfMethodCallIgnored
System.in.read();
threadSelector.stopEndpoint();
System.exit(0);
}
}
2 – using GrizzlyWebServer
GrizzlyWebServer gws = new GrizzlyWebServer(8080, "/var/www");
// Jersey web resources
ServletAdapter jerseyAdapter = new ServletAdapter();
jerseyAdapter.addInitParameter("com.sun.jersey.config.property.packages",
"com.example");
jerseyAdapter.setContextPath("/jersey");
jerseyAdapter.setServletInstance(new ServletContainer());
// Another non-Jersey servlet
ServletAdapter simpleServletAdapter = new ServletAdapter();
simpleServletAdapter.setContextPath("/simple");
simpleServletAdapter.setServletInstance(new SimpleServlet());
// register all above defined adapters
gws.addGrizzlyAdapter(jerseyAdapter, new String[] {"/jersey"});
gws.addGrizzlyAdapter(simpleServletAdapter, new String[] {"/simple"});
// let Grizzly run
gws.start();
So which way is better?
Thanks
–Alex
At first I was using your method #1, but then once I needed to provide a more complex configuration, I found that i needed method #2 to have access to the GrizzlyWebServer object.
For example using GrizzlyWebServer you can control the size of the worker thread pool. (I should note Grizzly docs only recommend doing this is you have IO bound work to be done).
I could not find a way to adjust the thread pool size with method #1, therefore I’d recommend method #2 for improved configurability.