Created
May 31, 2018 16:31
-
-
Save gregw/950113e434ca9b41b89e8b5106e34e39 to your computer and use it in GitHub Desktop.
No jetty classes timeout filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.eclipse.jetty.servlets; | |
import java.io.IOException; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
import java.util.concurrent.TimeUnit; | |
import java.util.concurrent.atomic.AtomicReference; | |
import javax.servlet.Filter; | |
import javax.servlet.FilterChain; | |
import javax.servlet.FilterConfig; | |
import javax.servlet.ServletException; | |
import javax.servlet.ServletRequest; | |
import javax.servlet.ServletResponse; | |
public class TimeoutFilter implements Filter | |
{ | |
private final Timer _timer = new Timer("TimeoutFilter",false); | |
private final TimerTask IDLE = new TimerTask() | |
{ | |
@Override | |
public void run() | |
{} | |
}; | |
private int _interruptTimeoutSeconds; | |
private int _stopTimeoutSeconds; | |
@Override | |
public void init(FilterConfig filterConfig) throws ServletException | |
{ | |
try | |
{ | |
String s = filterConfig.getInitParameter("interruptTimeoutSeconds"); | |
_interruptTimeoutSeconds = s==null?120:Integer.parseInt(s); | |
s = filterConfig.getInitParameter("stopTimeoutSeconds"); | |
_stopTimeoutSeconds = s==null?30:Integer.parseInt(s); | |
} | |
catch (Exception e) | |
{ | |
throw new ServletException(e); | |
} | |
} | |
@Override | |
public void destroy() | |
{ | |
_timer.cancel(); | |
} | |
protected void interrupt(Thread thread) | |
{ | |
thread.interrupt(); | |
} | |
@SuppressWarnings("deprecation") | |
protected void stop(Thread thread) | |
{ | |
thread.stop(); | |
} | |
@Override | |
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException | |
{ | |
InterruptTask task = new InterruptTask(); | |
_timer.schedule(task,TimeUnit.SECONDS.toMillis(_interruptTimeoutSeconds)); | |
try | |
{ | |
chain.doFilter(request,response); | |
} | |
finally | |
{ | |
TimerTask state = task._state.get(); | |
if (state!=null && state!=IDLE && task._state.compareAndSet(state,IDLE)) | |
state.cancel(); | |
} | |
} | |
private class InterruptTask extends TimerTask | |
{ | |
private final AtomicReference<TimerTask> _state = new AtomicReference<>(this); | |
private final Thread _thread = Thread.currentThread(); | |
@Override | |
public void run() | |
{ | |
TimerTask stop = new TimerTask() | |
{ | |
@SuppressWarnings("deprecation") | |
@Override | |
public void run() | |
{ | |
if (_state.compareAndSet(this,null)) | |
_thread.stop(); | |
} | |
}; | |
if (_state.compareAndSet(this,stop)) | |
{ | |
_thread.interrupt(); | |
_timer.schedule(stop,TimeUnit.SECONDS.toMillis(_stopTimeoutSeconds)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment