Jetty9 - Refactored usage of components that were using custom stop timeout to use the get/setStopTimeout() methods inherited from AbstractLifeCycle.

This commit is contained in:
Simone Bordet 2012-08-10 15:51:27 +02:00
parent 4d74adc1e5
commit 136f7924fd
17 changed files with 138 additions and 232 deletions

View File

@ -378,8 +378,7 @@ public class XmlConfiguredJetty
Assert.assertEquals("Server load count",1,serverCount);
this._server = foundServer;
this._server.setGracefulShutdown(10);
this._server.setStopTimeout(10);
}
public void removeContext(String name)

View File

@ -123,6 +123,6 @@
<Set name="stopAtShutdown">true</Set>
<Set name="sendServerVersion">true</Set>
<Set name="sendDateHeader">true</Set>
<Set name="gracefulShutdown">1000</Set>
<Set name="stopTimeout">1000</Set>
</Configure>

View File

@ -280,9 +280,9 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
@Override
protected void doStop() throws Exception
{
Stop task = new Stop();
submit(task);
task.await(getStopTimeout(), TimeUnit.MILLISECONDS);
Stop stop = new Stop();
submit(stop);
stop.await(getStopTimeout());
}
/**
@ -677,11 +677,11 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
}
}
public boolean await(long timeout, TimeUnit unit)
public boolean await(long timeout)
{
try
{
return latch.await(timeout, unit);
return latch.await(timeout, TimeUnit.MILLISECONDS);
}
catch (InterruptedException x)
{

View File

@ -25,7 +25,7 @@
<!-- =========================================================== -->
<!-- Set handler Collection Structure -->
<!-- Set handler Collection Structure -->
<!-- =========================================================== -->
<Set name="handler">
<New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
@ -52,11 +52,11 @@
<Set name="stopAtShutdown">true</Set>
<Set name="sendServerVersion">true</Set>
<Set name="sendDateHeader">true</Set>
<Set name="gracefulShutdown">1000</Set>
<Set name="stopTimeout">1000</Set>
<Set name="dumpAfterStart">false</Set>
<Set name="dumpBeforeStop">false</Set>
<!-- =========================================================== -->
<!-- jetty-jndi by default -->
<!-- =========================================================== -->

View File

@ -276,6 +276,6 @@
<Set name="stopAtShutdown">true</Set>
<Set name="sendServerVersion">true</Set>
<Set name="sendDateHeader">true</Set>
<Set name="gracefulShutdown">1000</Set>
<Set name="stopTimeout">1000</Set>
</Configure>

View File

@ -57,6 +57,6 @@
<Set name="stopAtShutdown">true</Set>
<Set name="sendServerVersion">true</Set>
<Set name="sendDateHeader">true</Set>
<Set name="gracefulShutdown">1000</Set>
<Set name="stopTimeout">1000</Set>
</Configure>

View File

@ -70,7 +70,7 @@
<Set name="stopAtShutdown">true</Set>
<Set name="sendServerVersion">true</Set>
<Set name="sendDateHeader">true</Set>
<Set name="gracefulShutdown">1000</Set>
<Set name="stopTimeout">1000</Set>
<Set name="dumpAfterStart">false</Set>
<Set name="dumpBeforeStop">false</Set>

View File

@ -27,7 +27,6 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpGenerator;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.server.handler.HandlerWrapper;
import org.eclipse.jetty.util.ArrayUtil;
import org.eclipse.jetty.util.Attributes;
import org.eclipse.jetty.util.AttributesMap;
import org.eclipse.jetty.util.MultiException;
@ -71,7 +70,6 @@ public class Server extends HandlerWrapper implements Attributes
private SessionIdManager _sessionIdManager;
private boolean _sendServerVersion = true; //send Server: header
private boolean _sendDateHeader = false; //send Date: header
private int _graceful=0;
private boolean _stopAtShutdown;
private boolean _dumpAfterStart=false;
private boolean _dumpBeforeStop=false;
@ -306,8 +304,9 @@ public class Server extends HandlerWrapper implements Attributes
MultiException mex=new MultiException();
long gracefulTimeout = getGracefulShutdown();
if (gracefulTimeout>0)
// TODO: review this logic ...
long stopTimeout = getStopTimeout();
if (stopTimeout>0)
{
for (Connector connector : _connectors)
{
@ -321,9 +320,9 @@ public class Server extends HandlerWrapper implements Attributes
{
Graceful graceful = (Graceful)context;
LOG.info("Graceful shutdown {}", graceful);
graceful.setShutdown(true);
graceful.shutdown();
}
Thread.sleep(gracefulTimeout);
Thread.sleep(stopTimeout);
}
for (Connector connector : _connectors)
@ -564,15 +563,6 @@ public class Server extends HandlerWrapper implements Attributes
_attributes.setAttribute(name, attribute);
}
/* ------------------------------------------------------------ */
/**
* @return the graceful
*/
public int getGracefulShutdown()
{
return _graceful;
}
/* ------------------------------------------------------------ */
/**
* Set graceful shutdown timeout. If set, the internal <code>doStop()</code> method will not immediately stop the
@ -585,7 +575,7 @@ public class Server extends HandlerWrapper implements Attributes
*/
public void setGracefulShutdown(int timeoutMS)
{
_graceful=timeoutMS;
// TODO
}
/* ------------------------------------------------------------ */
@ -618,11 +608,13 @@ public class Server extends HandlerWrapper implements Attributes
/* ------------------------------------------------------------ */
/* A handler that can be gracefully shutdown.
* Called by doStop if a {@link #setGracefulShutdown} period is set.
* TODO move this somewhere better
* TODO: this interface should be part of a restructuring of how we manage the lifecycle of components
* TODO: it should extend LifeCycle rather than Handler, for example, and should play in concert with
* TODO: LifeCycle.stop() so that stop==shutdown+await(stopTimeout) to keep the stop semantic.
*/
public interface Graceful extends Handler
{
public void setShutdown(boolean shutdown);
public void shutdown();
}
/* ------------------------------------------------------------ */

View File

@ -31,7 +31,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
@ -88,13 +87,13 @@ import org.eclipse.jetty.util.resource.Resource;
* <p>
* The maximum size of a form that can be processed by this context is controlled by the system properties org.eclipse.jetty.server.Request.maxFormKeys
* and org.eclipse.jetty.server.Request.maxFormContentSize. These can also be configured with {@link #setMaxFormContentSize(int)} and {@link #setMaxFormKeys(int)}
*
*
* @org.apache.xbean.XBean description="Creates a basic HTTP context"
*/
@Managed("URI Context")
public class ContextHandler extends ScopedHandler implements Attributes, Server.Graceful
{
private static final Logger LOG = Log.getLogger(ContextHandler.class);
private static final ThreadLocal<Context> __context = new ThreadLocal<Context>();
@ -121,44 +120,44 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
private final AttributesMap _attributes;
private final AttributesMap _contextAttributes;
@Managed("Initial Parameter map for the context")
private final Map<String, String> _initParams;
private ClassLoader _classLoader;
private String _contextPath = "/";
@Managed(value="Display name of the Context", readonly=true)
private String _displayName;
private Resource _baseResource;
private MimeTypes _mimeTypes;
private Map<String, String> _localeEncodingMap;
@Managed("Partial URIs of directory welcome files")
private String[] _welcomeFiles;
@Managed(value="The error handler to use for the context", managed=true)
private ErrorHandler _errorHandler;
@Managed("Virtual hosts accepted by the context")
private String[] _vhosts;
private Set<String> _connectors;
private EventListener[] _eventListeners;
private Logger _logger;
@Managed("Checks if the /context is not redirected to /context/")
private boolean _allowNullPathInfo;
private int _maxFormKeys = Integer.getInteger("org.eclipse.jetty.server.Request.maxFormKeys",1000).intValue();
@Managed("The maximum content size")
private int _maxFormContentSize = Integer.getInteger("org.eclipse.jetty.server.Request.maxFormContentSize",200000).intValue();
@Managed("True if URLs are compacted to replace the multiple '/'s with a single '/'")
private boolean _compactPath = false;
private boolean _aliases = false;
private Object _contextListeners;
@ -170,7 +169,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
@Managed("False if this context is accepting new requests. True for graceful shutdown, which allows existing requests to complete")
private boolean _shutdown = false;
private boolean _available = true;
private volatile int _availability; // 0=STOPPED, 1=AVAILABLE, 2=SHUTDOWN, 3=UNAVAILABLE
@ -608,14 +607,14 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
{
setEventListeners((EventListener[])ArrayUtil.addToArray(getEventListeners(),listener,EventListener.class));
}
/**
* Apply any necessary restrictions on a programmatically added
* listener.
*
*
* Superclasses should implement.
*
*
* @param listener
*/
public void restrictEventListener (EventListener listener)
@ -639,15 +638,13 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
* Set shutdown status. This field allows for graceful shutdown of a context. A started context may be put into non accepting state so that existing
* requests can complete, but no new requests are accepted.
*
* @param shutdown
* true if this context is (not?) accepting new requests
*/
public void setShutdown(boolean shutdown)
public void shutdown()
{
synchronized (this)
{
_shutdown = shutdown;
_availability = isRunning()?(_shutdown?__SHUTDOWN:_available?__AVAILABLE:__UNAVAILABLE):__STOPPED;
_shutdown = true;
_availability = isRunning() ? __SHUTDOWN : __STOPPED;
}
}
@ -796,7 +793,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
{
l.contextDestroyed(e);
}
/* ------------------------------------------------------------ */
/*
* @see org.eclipse.thread.AbstractLifeCycle#doStop()
@ -968,7 +965,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
target = URIUtil.compactPath(target);
if (!checkContext(target,baseRequest,response))
return;
if (target.length() > _contextPath.length())
{
if (_contextPath.length() > 1)
@ -1156,17 +1153,17 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
/* ------------------------------------------------------------ */
/**
* Check the target. Called by {@link #handle(String, Request, HttpServletRequest, HttpServletResponse)} when a target within a context is determined. If
* the target is protected, 404 is returned.
* the target is protected, 404 is returned.
*/
/* ------------------------------------------------------------ */
public boolean isProtectedTarget(String target)
{
if (target == null || _protectedTargets == null)
return false;
while (target.startsWith("//"))
target=URIUtil.compactPath(target);
boolean isProtected = false;
int i=0;
while (!isProtected && i<_protectedTargets.length)
@ -1175,8 +1172,8 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
}
return isProtected;
}
public void setProtectedTargets (String[] targets)
{
if (targets == null)
@ -1184,21 +1181,21 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
_protectedTargets = null;
return;
}
_protectedTargets = new String[targets.length];
System.arraycopy(targets, 0, _protectedTargets, 0, targets.length);
}
public String[] getProtectedTargets ()
{
if (_protectedTargets == null)
return null;
String[] tmp = new String[_protectedTargets.length];
System.arraycopy(_protectedTargets, 0, tmp, 0, _protectedTargets.length);
return tmp;
}
/* ------------------------------------------------------------ */
/*
@ -1795,7 +1792,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
{
return 3;
}
/* ------------------------------------------------------------ */
/*
@ -2293,7 +2290,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
{
if (!_enabled)
throw new UnsupportedOperationException();
try
{
Class<? extends EventListener> clazz = _classLoader==null?Loader.loadClass(ContextHandler.class,className):_classLoader.loadClass(className);
@ -2307,7 +2304,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
@Override
public <T extends EventListener> void addListener(T t)
{
{
if (!_enabled)
throw new UnsupportedOperationException();
ContextHandler.this.addEventListener(t);
@ -2315,7 +2312,7 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
@Override
public void addListener(Class<? extends EventListener> listenerClass)
{
{
if (!_enabled)
throw new UnsupportedOperationException();
@ -2371,12 +2368,12 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
{
_majorVersion = v;
}
public void setEffectiveMinorVersion (int v)
{
_minorVersion = v;
}
@Override
public JspConfigDescriptor getJspConfigDescriptor()
{
@ -2386,9 +2383,9 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
public void setJspConfigDescriptor(JspConfigDescriptor d)
{
}
@Override
public void declareRoles(String... roleNames)
{
@ -2396,9 +2393,9 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
throw new IllegalStateException ();
if (!_enabled)
throw new UnsupportedOperationException();
// TODO Auto-generated method stub
}
public void setEnabled(boolean enabled)

View File

@ -29,7 +29,7 @@ import org.eclipse.jetty.util.log.Logger;
public abstract class AbstractLifeCycle implements LifeCycle
{
private static final Logger LOG = Log.getLogger(AbstractLifeCycle.class);
public static final String STOPPED="STOPPED";
public static final String FAILED="FAILED";
public static final String STARTING="STARTING";
@ -196,6 +196,7 @@ public abstract class AbstractLifeCycle implements LifeCycle
listener.lifeCycleFailure(this,th);
}
@ManagedAttribute(value="The stop timeout in milliseconds")
public long getStopTimeout()
{
return _stopTimeout;

View File

@ -310,6 +310,20 @@ public class AggregateLifeCycle extends AbstractLifeCycle implements Destroyable
return false;
}
@Override
public void setStopTimeout(long stopTimeout)
{
super.setStopTimeout(stopTimeout);
for (Bean bean : _beans)
{
Object component = bean._bean;
if (component instanceof AbstractLifeCycle)
{
((AbstractLifeCycle)component).setStopTimeout(stopTimeout);
}
}
}
/**
* Dumps to {@link System#err}.
* @see #dump()

View File

@ -48,52 +48,33 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
private final AtomicInteger _threadsStarted = new AtomicInteger();
private final AtomicInteger _threadsIdle = new AtomicInteger();
private final AtomicLong _lastShrink = new AtomicLong();
private final ConcurrentLinkedQueue<Thread> _threads=new ConcurrentLinkedQueue<Thread>();
private final ConcurrentLinkedQueue<Thread> _threads=new ConcurrentLinkedQueue<>();
private final Object _joinLock = new Object();
private BlockingQueue<Runnable> _jobs;
private String _name;
private int _maxIdleTimeMs=60000;
private int _maxThreads;
private int _minThreads;
private int _maxQueued=-1;
private int _priority=Thread.NORM_PRIORITY;
private boolean _daemon=false;
private int _maxStopTime=100;
private boolean _detailedDump=false;
/* ------------------------------------------------------------------- */
/** Construct
*/
public QueuedThreadPool()
{
this(200,8,60000);
}
/* ------------------------------------------------------------------- */
/** Construct
*/
public QueuedThreadPool(int maxThreads)
{
this(maxThreads,8,60000);
}
/* ------------------------------------------------------------------- */
/** Construct
*/
public QueuedThreadPool(int maxThreads, int minThreads)
{
this(maxThreads,8,60000);
this(maxThreads,minThreads,60000);
}
/* ------------------------------------------------------------------- */
/** Construct
*/
public QueuedThreadPool(int maxThreads, int minThreads, int maxIdleTimeMs)
{
_name="qtp"+super.hashCode();
@ -102,7 +83,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
setMaxIdleTimeMs(maxIdleTimeMs);
}
/* ------------------------------------------------------------ */
@Override
protected void doStart() throws Exception
{
@ -111,7 +91,8 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
if (_jobs==null)
{
_jobs=_maxQueued>0 ?new ArrayBlockingQueue<Runnable>(_maxQueued)
int maxQueued = getMaxQueued();
_jobs=maxQueued>0 ?new ArrayBlockingQueue<Runnable>(maxQueued)
:new BlockingArrayQueue<Runnable>(_minThreads,_minThreads);
}
@ -123,7 +104,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
}
}
/* ------------------------------------------------------------ */
@Override
protected void doStop() throws Exception
{
@ -131,14 +111,15 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
long start=System.currentTimeMillis();
// let jobs complete naturally for a while
while (_threadsStarted.get()>0 && (System.currentTimeMillis()-start) < (_maxStopTime/2))
while (_threadsStarted.get()>0 && (System.currentTimeMillis()-start) < (getStopTimeout()/2))
Thread.sleep(1);
// kill queued jobs and flush out idle jobs
_jobs.clear();
BlockingQueue<Runnable> jobs = getQueue();
jobs.clear();
Runnable noop = new Runnable(){@Override public void run(){}};
for (int i=_threadsIdle.get();i-->0;)
_jobs.offer(noop);
jobs.offer(noop);
Thread.yield();
// interrupt remaining threads
@ -147,7 +128,7 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
thread.interrupt();
// wait for remaining threads to die
while (_threadsStarted.get()>0 && (System.currentTimeMillis()-start) < _maxStopTime)
while (_threadsStarted.get()>0 && (System.currentTimeMillis()-start) < getStopTimeout())
{
Thread.sleep(1);
}
@ -177,7 +158,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
}
}
/* ------------------------------------------------------------ */
/**
* Delegated to the named or anonymous Pool.
*/
@ -186,7 +166,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
_daemon=daemon;
}
/* ------------------------------------------------------------ */
/** Set the maximum thread idle time.
* Threads that are idle for longer than this period may be
* stopped.
@ -199,16 +178,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
_maxIdleTimeMs=maxIdleTimeMs;
}
/* ------------------------------------------------------------ */
/**
* @param stopTimeMs maximum total time that stop() will wait for threads to die.
*/
public void setMaxStopTimeMs(int stopTimeMs)
{
_maxStopTime = stopTimeMs;
}
/* ------------------------------------------------------------ */
/** Set the maximum number of threads.
* Delegated to the named or anonymous Pool.
* @see #getMaxThreads
@ -222,7 +191,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
_minThreads=_maxThreads;
}
/* ------------------------------------------------------------ */
/** Set the minimum number of threads.
* Delegated to the named or anonymous Pool.
* @see #getMinThreads
@ -244,7 +212,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
}
}
/* ------------------------------------------------------------ */
/**
* @param name Name of the BoundedThreadPool to use when naming Threads.
*/
@ -255,7 +222,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
_name= name;
}
/* ------------------------------------------------------------ */
/** Set the priority of the pool threads.
* @param priority the new thread priority.
*/
@ -264,7 +230,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
_priority=priority;
}
/* ------------------------------------------------------------ */
/**
* @return maximum queue size
*/
@ -273,7 +238,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
return _maxQueued;
}
/* ------------------------------------------------------------ */
/**
* @param max job queue size
*/
@ -284,7 +248,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
_maxQueued=max;
}
/* ------------------------------------------------------------ */
/** Get the maximum thread idle time.
* Delegated to the named or anonymous Pool.
* @see #setMaxIdleTimeMs
@ -295,16 +258,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
return _maxIdleTimeMs;
}
/* ------------------------------------------------------------ */
/**
* @return maximum total time that stop() will wait for threads to die.
*/
public int getMaxStopTimeMs()
{
return _maxStopTime;
}
/* ------------------------------------------------------------ */
/** Set the maximum number of threads.
* Delegated to the named or anonymous Pool.
* @see #setMaxThreads
@ -316,7 +269,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
return _maxThreads;
}
/* ------------------------------------------------------------ */
/** Get the minimum number of threads.
* Delegated to the named or anonymous Pool.
* @see #setMinThreads
@ -329,7 +281,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
return _minThreads;
}
/* ------------------------------------------------------------ */
/**
* @return The name of the BoundedThreadPool.
*/
@ -339,7 +290,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
return _name;
}
/* ------------------------------------------------------------ */
/** Get the priority of the pool threads.
* @return the priority of the pool threads.
*/
@ -349,7 +299,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
return _priority;
}
/* ------------------------------------------------------------ */
/**
* Delegated to the named or anonymous Pool.
*/
@ -359,20 +308,16 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
return _daemon;
}
/* ------------------------------------------------------------ */
@ManagedAttribute("full stack detail on dump output")
public boolean isDetailedDump()
{
return _detailedDump;
}
/* ------------------------------------------------------------ */
public void setDetailedDump(boolean detailedDump)
{
_detailedDump = detailedDump;
}
/* ------------------------------------------------------------ */
@Override
public boolean dispatch(Runnable job)
{
@ -396,7 +341,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
return false;
}
/* ------------------------------------------------------------ */
@Override
public void execute(Runnable job)
{
@ -404,7 +348,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
throw new RejectedExecutionException(toString());
}
/* ------------------------------------------------------------ */
/**
* Blocks until the thread pool is {@link LifeCycle#stop stopped}.
*/
@ -421,7 +364,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
Thread.sleep(1);
}
/* ------------------------------------------------------------ */
/**
* @return The total number of threads currently in the pool
*/
@ -431,7 +373,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
return _threadsStarted.get();
}
/* ------------------------------------------------------------ */
/**
* @return The number of idle threads in the pool
*/
@ -441,7 +382,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
return _threadsIdle.get();
}
/* ------------------------------------------------------------ */
/**
* @return True if the pool is at maxThreads and there are not more idle threads than queued jobs
*/
@ -451,7 +391,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
return _threadsStarted.get()==_maxThreads && _jobs.size()>=_threadsIdle.get();
}
/* ------------------------------------------------------------ */
private boolean startThread(int threads)
{
final int next=threads+1;
@ -462,8 +401,8 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
try
{
Thread thread=newThread(_runnable);
thread.setDaemon(_daemon);
thread.setPriority(_priority);
thread.setDaemon(isDaemon());
thread.setPriority(getThreadsPriority());
thread.setName(_name+"-"+thread.getId());
_threads.add(thread);
@ -478,14 +417,12 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
return started;
}
/* ------------------------------------------------------------ */
protected Thread newThread(Runnable runnable)
{
return new Thread(runnable);
}
/* ------------------------------------------------------------ */
@Override
@ManagedOperation("dump thread state")
public String dump()
@ -493,30 +430,25 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
return AggregateLifeCycle.dump(this);
}
/* ------------------------------------------------------------ */
@Override
public void dump(Appendable out, String indent) throws IOException
{
List<Object> dump = new ArrayList<Object>(getMaxThreads());
List<Object> dump = new ArrayList<>(getMaxThreads());
for (final Thread thread: _threads)
{
final StackTraceElement[] trace=thread.getStackTrace();
boolean inIdleJobPoll=false;
// trace can be null on early java 6 jvms
if (trace != null)
for (StackTraceElement t : trace)
{
for (StackTraceElement t : trace)
if ("idleJobPoll".equals(t.getMethodName()))
{
if ("idleJobPoll".equals(t.getMethodName()))
{
inIdleJobPoll = true;
break;
}
inIdleJobPoll = true;
break;
}
}
final boolean idle=inIdleJobPoll;
if (_detailedDump)
if (isDetailedDump())
{
dump.add(new Dumpable()
{
@ -545,20 +477,17 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
AggregateLifeCycle.dump(out,indent,dump);
}
/* ------------------------------------------------------------ */
@Override
public String toString()
{
return String.format("%s{%s,%d<=%d<=%d/%d,%d}",_name,getState(),getMinThreads(),getIdleThreads(),getThreads(),getMaxThreads(),(_jobs==null?-1:_jobs.size()));
}
/* ------------------------------------------------------------ */
private Runnable idleJobPoll() throws InterruptedException
{
return _jobs.poll(_maxIdleTimeMs,TimeUnit.MILLISECONDS);
}
/* ------------------------------------------------------------ */
private Runnable _runnable = new Runnable()
{
@Override
@ -629,7 +558,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
}
};
/* ------------------------------------------------------------ */
/**
* <p>Runs the given job in the {@link Thread#currentThread() current thread}.</p>
* <p>Subclasses may override to perform pre/post actions before/after the job is run.</p>
@ -641,7 +569,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
job.run();
}
/* ------------------------------------------------------------ */
/**
* @return the job queue
*/
@ -650,27 +577,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
return _jobs;
}
/* ------------------------------------------------------------ */
/**
* @param id The thread ID to stop.
* @return true if the thread was found and stopped.
* @deprecated Use {@link #interruptThread(long)} in preference
*/
@Deprecated
public boolean stopThread(long id)
{
for (Thread thread: _threads)
{
if (thread.getId()==id)
{
thread.stop();
return true;
}
}
return false;
}
/* ------------------------------------------------------------ */
/**
* @param id The thread ID to interrupt.
* @return true if the thread was found and interrupted.
@ -689,7 +595,6 @@ public class QueuedThreadPool extends AbstractLifeCycle implements SizedThreadPo
return false;
}
/* ------------------------------------------------------------ */
/**
* @param id The thread ID to interrupt.
* @return true if the thread was found and interrupted.

View File

@ -4,33 +4,32 @@
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.util.thread;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import junit.framework.Assert;
import org.eclipse.jetty.toolchain.test.AdvancedRunner;
import org.eclipse.jetty.toolchain.test.annotation.Slow;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertTrue;
@RunWith(AdvancedRunner.class)
public class QueuedThreadPoolTest
{
final AtomicInteger _jobs=new AtomicInteger();
class RunningJob implements Runnable
{
private final CountDownLatch _run = new CountDownLatch(1);
@ -38,7 +37,7 @@ public class QueuedThreadPoolTest
private final CountDownLatch _stopped = new CountDownLatch(1);
public void run()
{
try
try
{
_run.countDown();
_stopping.await();
@ -53,21 +52,21 @@ public class QueuedThreadPoolTest
_stopped.countDown();
}
}
public void stop() throws InterruptedException
{
if (_run.await(10,TimeUnit.SECONDS))
_stopping.countDown();
if (!_stopped.await(10,TimeUnit.SECONDS))
throw new IllegalStateException();
throw new IllegalStateException();
}
};
};
@Test
@Slow
public void testThreadPool() throws Exception
{
{
QueuedThreadPool tp= new QueuedThreadPool();
tp.setMinThreads(5);
tp.setMaxThreads(10);
@ -75,14 +74,14 @@ public class QueuedThreadPoolTest
tp.setThreadsPriority(Thread.NORM_PRIORITY-1);
tp.start();
waitForThreads(tp,5);
waitForIdle(tp,5);
Thread.sleep(1000);
waitForThreads(tp,5);
waitForIdle(tp,5);
RunningJob job=new RunningJob();
tp.dispatch(job);
waitForIdle(tp,4);
@ -91,7 +90,7 @@ public class QueuedThreadPoolTest
job.stop();
waitForIdle(tp,5);
waitForThreads(tp,5);
Thread.sleep(200);
waitForIdle(tp,5);
waitForThreads(tp,5);
@ -104,24 +103,24 @@ public class QueuedThreadPoolTest
}
waitForIdle(tp,0);
waitForThreads(tp,5);
job=new RunningJob();
tp.dispatch(job);
waitForThreads(tp,6);
job.stop();
waitForThreads(tp,5);
jobs[0].stop();
waitForIdle(tp,1);
waitForThreads(tp,5);
for (int i=1;i<jobs.length;i++)
jobs[i].stop();
waitForIdle(tp,5);
waitForThreads(tp,5);
jobs = new RunningJob[15];
for (int i=0;i<jobs.length;i++)
{
@ -133,7 +132,7 @@ public class QueuedThreadPoolTest
for (int i=0;i<9;i++)
jobs[i].stop();
waitForThreads(tp,9);
for (int i=9;i<jobs.length;i++)
jobs[i].stop();
waitForIdle(tp,5);
@ -149,7 +148,7 @@ public class QueuedThreadPoolTest
{
public void run()
{
try
try
{
Thread.sleep(sleep.get());
}
@ -158,19 +157,19 @@ public class QueuedThreadPoolTest
e.printStackTrace();
}
}
};
QueuedThreadPool tp= new QueuedThreadPool();
tp.setMinThreads(2);
tp.setMaxThreads(10);
tp.setMaxIdleTimeMs(400);
tp.setThreadsPriority(Thread.NORM_PRIORITY-1);
tp.start();
waitForIdle(tp,2);
waitForThreads(tp,2);
sleep.set(200);
tp.dispatch(job);
tp.dispatch(job);
@ -179,7 +178,7 @@ public class QueuedThreadPoolTest
waitForThreads(tp,10);
waitForIdle(tp,0);
sleep.set(5);
for (int i=0;i<500;i++)
{
@ -194,7 +193,7 @@ public class QueuedThreadPoolTest
public void testMaxStopTime() throws Exception
{
QueuedThreadPool tp= new QueuedThreadPool();
tp.setMaxStopTimeMs(500);
tp.setStopTimeout(500);
tp.start();
tp.dispatch(new Runnable(){
public void run () {
@ -242,7 +241,7 @@ public class QueuedThreadPoolTest
Thread.sleep(10);
}
catch(InterruptedException e)
{}
{}
now=System.currentTimeMillis();
}
Assert.assertEquals(threads,tp.getThreads());

View File

@ -15,8 +15,6 @@
//========================================================================
package org.eclipse.jetty.websocket.server;
import static org.junit.Assert.assertTrue;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
@ -31,7 +29,6 @@ import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import junit.framework.Assert;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.server.SelectChannelConnector;
import org.eclipse.jetty.server.Server;
@ -50,6 +47,8 @@ import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class WebSocketLoadRFC6455Test
{
private class WebSocketClient implements Runnable
@ -66,7 +65,7 @@ public class WebSocketLoadRFC6455Test
{
/*
* public void close(int code,String message) { }
*
*
* public void onFrame(byte flags, byte opcode, ByteBuffer buffer) { _response=buffer; }
*/
@ -159,7 +158,7 @@ public class WebSocketLoadRFC6455Test
public static void startServer() throws Exception
{
QueuedThreadPool threadPool = new QueuedThreadPool(200);
threadPool.setMaxStopTimeMs(1000);
threadPool.setStopTimeout(1000);
_server = new Server(threadPool);
_server.manage(threadPool);

View File

@ -27,7 +27,7 @@
<Set name="stopAtShutdown">true</Set>
<Set name="sendServerVersion">true</Set>
<Set name="sendDateHeader">true</Set>
<Set name="gracefulShutdown">1000</Set>
<Set name="stopTimeout">1000</Set>
<Set name="dumpAfterStart">true</Set>
<Set name="dumpBeforeStop">false</Set>

View File

@ -65,6 +65,6 @@
<Set name="stopAtShutdown">true</Set>
<Set name="sendServerVersion">true</Set>
<Set name="sendDateHeader">true</Set>
<Set name="gracefulShutdown">1000</Set>
<Set name="stopTimeout">1000</Set>
</Configure>

View File

@ -141,6 +141,6 @@
<Set name="stopAtShutdown">true</Set>
<Set name="sendServerVersion">true</Set>
<Set name="sendDateHeader">true</Set>
<Set name="gracefulShutdown">1000</Set>
<Set name="stopTimeout">1000</Set>
</Configure>