12

I'm running an embedded jetty server (jetty 6.1.24) inside my application like this:

    Handler handler=new AbstractHandler()
    {
        @Override
        public void handle(String target, HttpServletRequest request,
                HttpServletResponse response, int dispatch)
                throws IOException, ServletException {
              //this can take a long time
              doSomething();  
        }
    };


    Server server = new Server(8080);
    Connector connector = new org.mortbay.jetty.nio.SelectChannelConnector();      
    server.addConnector(connector);

    server.setHandler(handler);
    server.start();

I would like to set a timeout value (2 seconds) so that if handler.handle() method takes more than 2 seconds, jetty server will timeout and response to the client with 408 http code (request timeout).

This is to guarantee that my application will not hold the client request for a long time and always response within 2 seconds.

I did some research and tested it with "connector.setMaxIdleTime(2000);" but it doesn't work.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Hung Duong
  • 121
  • 1
  • 1
  • 4
  • I found a code sample [here](http://eclipsesource.com/blogs/2009/10/02/executable-wars-with-jetty/) which might be helpful for you. – FrVaBe May 04 '11 at 20:07
  • 1
    please don't start trying to figure this out using jetty 6...that has been out of maintenance for years now and there have been over 150 releases since then, consider using Jetty 7, Jetty 8, or even better Jetty 9. Docs for Jetty 9 are here: https://www.eclipse.org/jetty/documentation/current/index.html – jesse mcconnell Aug 17 '13 at 18:02
  • have you find the way to do it? - @"Hung Duong" – Udy Dec 01 '13 at 12:33

2 Answers2

3

Take a look at the API for SelectChannelConnector (Jetty):

http://download.eclipse.org/jetty/7.6.17.v20150415/apidocs/org/eclipse/jetty/server/nio/SelectChannelConnector.html

I've tried to locate any timeout features of the channel (which controls incoming connections): setMaxIdleTime(), setLowResourceMaxIdleTime() and setSoLingerTime() are available it appears.

NOTE: the reason for your timeout feature not to work has to do with the nature of the socket on your operating system. Perhaps even the nature of Jetty (i've read about it somewhere, but cannot remember where it was).

NOTE2: i'm not sure why you try to limit the timeout, perhaps a better approach is limiting the buffer sizes? If you're trying to prevent denial of service...

Dennis
  • 780
  • 6
  • 17
0

Yes, this is possible. You could do this using DosFilter of Jetty. This filter is generally used to configure a DOS attack prevention mechanism for your Jetty web server. A property of this filter called 'MaxRequestMs' provides what you are looking for.

For more details, check this.

https://www.eclipse.org/jetty/javadoc/jetty-9/org/eclipse/jetty/servlets/DoSFilter.html