I created a grizzly web server, to run my jersey application out of tomcat, to speed up tests.
I'm a grizzly beginner, but looking through the net I put some code lines together, to make a grizzly web server up and running in less than a working day :)
Unfortunately my class has some troubles on concurrent request, often one or more fails in an inexplicable NullPointerException.
Troubles are typically when I refresh my web page, where grizzly must return about 25 non cached files. This is registered exception:
9-set-2010 10.45.21 com.sun.grizzly.http.servlet.ServletAdapter doService
GRAVE: service exception:
java.lang.NullPointerException
at com.sun.grizzly.http.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:178)
at com.sun.grizzly.http.servlet.FilterChainImpl.invokeFilterChain(FilterChainImpl.java:139)
at com.sun.grizzly.http.servlet.ServletAdapter.doService(ServletAdapter.java:376)
at com.sun.grizzly.http.servlet.ServletAdapter.service(ServletAdapter.java:324)
.....
Static files are served from a class of mine, but logs tells me everithing is ok and when I use application under tomcat, everything is ok. I really don't know how to solve this problem..
This is the code I created/copied from internet, to startup project:
public class LaunchApp {
/** Find in internet, used to use argument port as default, only if there is no JERSEY_HTTP_PORT env port enabled*/
private static int getPort(int defaultPort) {
String port = System.getenv("JERSEY_HTTP_PORT");
if (null != port) {
try {
return Integer.parseInt(port);
} catch (NumberFormatException e) {
}
}
return defaultPort;
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost/").port(getPort(8080)).build();
}
public static final URI BASE_URI = getBaseURI();
protected static GrizzlyWebServer startServer() throws IOException {
final String rootFolder = "/Users/davide/dev/my-project/src/main/webapp";
GrizzlyWebServer ws = new GrizzlyWebServer("/Users/davide/dev/my-project/src/main/webapp");
try{
ServletAdapter adapter = new ServletAdapter();
adapter.addContextParameter( "contextConfigLocation","classpath:applicationContext.xml" );
adapter.addServletListener("org.springframework.web.context.ContextLoaderListener");
adapter.addServletListener("org.springframework.web.context.request.RequestContextListener");
adapter.addInitParameter( "com.sun.jersey.config.property.packages", "it.treis.zero.web.rest");
adapter.addInitParameter( "com.sun.jersey.spi.container.ContainerRequestFilters","com.sun.jersey.api.container.filter.LoggingFilter");
adapter.addInitParameter( "com.sun.jersey.spi.container.ContainerResponseFilters","com.sun.jersey.api.container.filter.LoggingFilter");
开发者_JAVA技巧 adapter.setProperty( "load-on-startup", 1 );
adapter.setServletInstance( new SpringServlet() );
adapter.setRootFolder(rootFolder);
// Add Open Session In View Hibernate Filter.
adapter.addFilter(new org.springframework.orm.hibernate3.support.OpenSessionInViewFilter(), "openSessionInViewFilter", null);
ws.addGrizzlyAdapter(adapter);
ws.start();
} catch(IOException ex){
ex.printStackTrace();
}
return ws;
}
public static void main(String[] args) throws IOException {
System.out.println("Starting Jersey");
GrizzlyWebServer ws = startServer();
System.out.println("Jersey rightly started, press any key to shutdown");
System.in.read();
ws.stop();
System.exit(0);
}
}
Any suggestions are appreciated.
Ciao, Davide.
This is due to a bug in Grizzly, the implementation of FilterChainImpl is not threadsafe. The issue has been reported here:https://grizzly.dev.java.net/issues/show_bug.cgi?id=819, and got fixed a couple months ago.
Updating to the latest release (1.9.21) solved the problem for me.
you have
adapter.addInitParameter( "com.sun.jersey.spi.container.ContainerRequestFilters","com.sun.jersey.api.container.filter.LoggingFilter");
adapter.addInitParameter( "com.sun.jersey.spi.container.ContainerResponseFilters","com.sun.jersey.api.container.filter.LoggingFilter");
twice in your configuration FYI..not sure if that could be an issue.
https://java.net/jira/browse/GRIZZLY-819
Says in above error ticket..if you register more than one filter this occurs: "I propose at least to make this variable threadsafe. The problem only shows if more than filter is registered."
精彩评论