Top Description Inners Fields Constructors Methods
org.apache.catalina.valves

public Class StuckThreadDetectionValve

extends ValveBase
Class Inheritance
Imports
java.io.IOException, java.util.ArrayList, .Date, .List, .Map, .Queue, java.util.concurrent.ConcurrentHashMap, .ConcurrentLinkedQueue, .Semaphore, java.util.concurrent.atomic.AtomicInteger, .AtomicLong, jakarta.servlet.ServletException, org.apache.catalina.LifecycleException, org.apache.catalina.connector.Request, .Response, org.apache.juli.logging.Log, .LogFactory, org.apache.tomcat.util.res.StringManager

This valve allows to detect requests that take a long time to process, which might indicate that the thread that is processing it is stuck.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
private static enum

Field Summary

Modifier and TypeField and Description
private final Map<Long, StuckThreadDetectionValve.MonitoredThread>
activeThreads

The only references we keep to actual running Thread objects are in this Map (which is automatically cleaned in invoke()s finally clause).

private final Queue<StuckThreadDetectionValve.CompletedStuckThread>
private AtomicLong
interruptedThreadsCount

Keeps count of the number of stuck threads that have been interrupted

private int
private static final Log
private static final StringManager
sm

Hides org.apache.catalina.valves.ValveBase.sm.

The string manager for this package.
private final AtomicInteger
stuckCount

Keeps count of the number of stuck threads detected

private int
threshold

In seconds.

Inherited from org.apache.catalina.valves.ValveBase:
asyncSupportedcontainercontainerLognext

Constructor Summary

AccessConstructor and Description
public
StuckThreadDetectionValve()

Required to enable async support.

Method Summary

Modifier and TypeMethod and Description
public void
backgroundProcess()

Overrides org.apache.catalina.valves.ValveBase.backgroundProcess.

Implements org.apache.catalina.Valve.backgroundProcess.

Execute a periodic task, such as reloading, etc. This method will be invoked inside the classloading context of this container.

public long
public int
public int
public long[]
public String[]
public int

Returns:

The current threshold in seconds
getThreshold
()

protected void
initInternal()

Overrides org.apache.catalina.valves.ValveBase.initInternal.

Sub-classes wishing to perform additional initialization should override this method, ensuring that super.initInternal() is the first call in the overriding method.

public void
invoke(Request
The servlet request to be processed
request
,
Response
The servlet response to be created
response
)

Implements org.apache.catalina.Valve.invoke.

Perform request processing as required by this Valve.

private void
private void
notifyStuckThreadDetected(StuckThreadDetectionValve.MonitoredThread monitoredThread, long activeTime, int numStuckThreads)

public void
setInterruptThreadThreshold(int
The new thread interruption threshold in seconds
interruptThreadThreshold
)

Specifies the threshold (in seconds) before stuck threads are interrupted.

public void
setThreshold(int
The new threshold in seconds
threshold
)

Specifies the threshold (in seconds) used when checking for stuck threads.

Inherited from org.apache.catalina.valves.ValveBase:
getContainergetDomainInternalgetNextgetObjectNameKeyPropertiesisAsyncSupportedsetAsyncSupportedsetContainersetNextstartInternalstopInternaltoString

Field Detail

activeThreadsback to summary
private final Map<Long, StuckThreadDetectionValve.MonitoredThread> activeThreads

The only references we keep to actual running Thread objects are in this Map (which is automatically cleaned in invoke()s finally clause). That way, Threads can be GC'ed, even though the Valve still thinks they are stuck (caused by a long monitor interval)

completedStuckThreadsQueueback to summary
private final Queue<StuckThreadDetectionValve.CompletedStuckThread> completedStuckThreadsQueue
interruptedThreadsCountback to summary
private AtomicLong interruptedThreadsCount

Keeps count of the number of stuck threads that have been interrupted

interruptThreadThresholdback to summary
private int interruptThreadThreshold

In seconds. Default is -1 to disable interruption.

logback to summary
private static final Log log

Hides org.apache.catalina.util.LifecycleMBeanBase.log.

Logger

smback to summary
private static final StringManager sm

Hides org.apache.catalina.valves.ValveBase.sm.

The string manager for this package.

stuckCountback to summary
private final AtomicInteger stuckCount

Keeps count of the number of stuck threads detected

thresholdback to summary
private int threshold

In seconds. Default 600 (10 minutes).

Constructor Detail

StuckThreadDetectionValveback to summary
public StuckThreadDetectionValve()

Required to enable async support.

Method Detail

backgroundProcessback to summary
public void backgroundProcess()

Overrides org.apache.catalina.valves.ValveBase.backgroundProcess.

Implements org.apache.catalina.Valve.backgroundProcess.

Doc from org.apache.catalina.valves.ValveBase.backgroundProcess.

Execute a periodic task, such as reloading, etc. This method will be invoked inside the classloading context of this container. Unexpected throwables will be caught and logged.

The default implementation is NO-OP.

Annotations
@Override
getInterruptedThreadsCountback to summary
public long getInterruptedThreadsCount()
getInterruptThreadThresholdback to summary
public int getInterruptThreadThreshold()
getStuckThreadCountback to summary
public int getStuckThreadCount()
getStuckThreadIdsback to summary
public long[] getStuckThreadIds()
getStuckThreadNamesback to summary
public String[] getStuckThreadNames()
getThresholdback to summary
public int getThreshold()
Returns:int

The current threshold in seconds

See Also
setThreshold(int)
initInternalback to summary
protected void initInternal() throws LifecycleException

Overrides org.apache.catalina.valves.ValveBase.initInternal.

Doc from org.apache.catalina.util.LifecycleMBeanBase.initInternal.

Sub-classes wishing to perform additional initialization should override this method, ensuring that super.initInternal() is the first call in the overriding method.

Annotations
@Override
Exceptions
LifecycleException:

Doc from org.apache.catalina.util.LifecycleBase.initInternal.

If the initialisation fails

invokeback to summary
public void invoke(Request request, Response response) throws IOException, ServletException

Implements org.apache.catalina.Valve.invoke.

Doc from org.apache.catalina.Valve.invoke.

Perform request processing as required by this Valve.

An individual Valve MAY perform the following actions, in the specified order:

  • Examine and/or modify the properties of the specified Request and Response.
  • Examine the properties of the specified Request, completely generate the corresponding Response, and return control to the caller.
  • Examine the properties of the specified Request and Response, wrap either or both of these objects to supplement their functionality, and pass them on.
  • If the corresponding Response was not generated (and control was not returned, call the next Valve in the pipeline (if there is one) by executing getNext().invoke().
  • Examine, but not modify, the properties of the resulting Response (which was created by a subsequently invoked Valve or Container).

A Valve MUST NOT do any of the following things:

  • Change request properties that have already been used to direct the flow of processing control for this request (for instance, trying to change the virtual host to which a Request should be sent from a pipeline attached to a Host or Context in the standard implementation).
  • Create a completed Response AND pass this Request and Response on to the next Valve in the pipeline.
  • Consume bytes from the input stream associated with the Request, unless it is completely generating the response, or wrapping the request before passing it on.
  • Modify the HTTP headers included with the Response after the getNext().invoke() method has returned.
  • Perform any actions on the output stream associated with the specified Response after the getNext().invoke() method has returned.
Parameters
request:Request

The servlet request to be processed

response:Response

The servlet response to be created

Annotations
@Override
Exceptions
IOException:
if an input/output error occurs, or is thrown by a subsequently invoked Valve, Filter, or Servlet
ServletException:
if a servlet error occurs, or is thrown by a subsequently invoked Valve, Filter, or Servlet
notifyStuckThreadCompletedback to summary
private void notifyStuckThreadCompleted(StuckThreadDetectionValve.CompletedStuckThread thread, int numStuckThreads)
notifyStuckThreadDetectedback to summary
private void notifyStuckThreadDetected(StuckThreadDetectionValve.MonitoredThread monitoredThread, long activeTime, int numStuckThreads)
setInterruptThreadThresholdback to summary
public void setInterruptThreadThreshold(int interruptThreadThreshold)

Specifies the threshold (in seconds) before stuck threads are interrupted. If <=0, the interruption is disabled. The default is -1. If >=0, the value must actually be >= threshold.

Parameters
interruptThreadThreshold:int

The new thread interruption threshold in seconds

setThresholdback to summary
public void setThreshold(int threshold)

Specifies the threshold (in seconds) used when checking for stuck threads. If <=0, the detection is disabled. The default is 600 seconds.

Parameters
threshold:int

The new threshold in seconds

org.apache.catalina.valves back to summary

private Class StuckThreadDetectionValve.CompletedStuckThread

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.catalina.valves.StuckThreadDetectionValve.CompletedStuckThread

Field Summary

Modifier and TypeField and Description
private final long
private final String
private final long

Constructor Summary

AccessConstructor and Description
pack-priv
CompletedStuckThread(Thread thread, long totalActiveTime)

Method Summary

Modifier and TypeMethod and Description
public long
public String
public long
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

threadIdback to summary
private final long threadId
threadNameback to summary
private final String threadName
totalActiveTimeback to summary
private final long totalActiveTime

Constructor Detail

CompletedStuckThreadback to summary
pack-priv CompletedStuckThread(Thread thread, long totalActiveTime)

Method Detail

getIdback to summary
public long getId()
getNameback to summary
public String getName()
getTotalActiveTimeback to summary
public long getTotalActiveTime()
org.apache.catalina.valves back to summary

private Class StuckThreadDetectionValve.MonitoredThread

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.catalina.valves.StuckThreadDetectionValve.MonitoredThread

Field Summary

Modifier and TypeField and Description
private boolean
interrupted

Set to true after the thread is interrupted.

private final Semaphore
interruptionSemaphore

Semaphore to synchronize the stuck thread with the background-process thread.

private final String
private final long
private final AtomicInteger
private final Thread
thread

Reference to the thread to get a stack trace from background task

Constructor Summary

AccessConstructor and Description
pack-priv
MonitoredThread(Thread thread, String requestUri, boolean interruptible)

Method Summary

Modifier and TypeMethod and Description
public long
public String
public Date
public Thread
public boolean
interruptIfStuck(long interruptThreadThreshold)

pack-priv boolean
public StuckThreadDetectionValve.MonitoredThreadState
public boolean
public boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

interruptedback to summary
private boolean interrupted

Set to true after the thread is interrupted. No need to make it volatile since it is accessed right after acquiring the semaphore.

interruptionSemaphoreback to summary
private final Semaphore interruptionSemaphore

Semaphore to synchronize the stuck thread with the background-process thread. It's not used if the interruption feature is not active.

requestUriback to summary
private final String requestUri
startback to summary
private final long start
stateback to summary
private final AtomicInteger state
threadback to summary
private final Thread thread

Reference to the thread to get a stack trace from background task

Constructor Detail

MonitoredThreadback to summary
pack-priv MonitoredThread(Thread thread, String requestUri, boolean interruptible)

Method Detail

getActiveTimeInMillisback to summary
public long getActiveTimeInMillis()
getRequestUriback to summary
public String getRequestUri()
getStartTimeback to summary
public Date getStartTime()
getThreadback to summary
public Thread getThread()
interruptIfStuckback to summary
public boolean interruptIfStuck(long interruptThreadThreshold)
isMarkedAsStuckback to summary
pack-priv boolean isMarkedAsStuck()
markAsDoneback to summary
public StuckThreadDetectionValve.MonitoredThreadState markAsDone()
markAsStuckIfStillRunningback to summary
public boolean markAsStuckIfStillRunning()
wasInterruptedback to summary
public boolean wasInterrupted()
org.apache.catalina.valves back to summary

private final Enum StuckThreadDetectionValve.MonitoredThreadState

extends Enum<StuckThreadDetectionValve.MonitoredThreadState>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final StuckThreadDetectionValve.MonitoredThreadState
public static final StuckThreadDetectionValve.MonitoredThreadState
public static final StuckThreadDetectionValve.MonitoredThreadState

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static StuckThreadDetectionValve.MonitoredThreadState
public static StuckThreadDetectionValve.MonitoredThreadState[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

DONEback to summary
public static final StuckThreadDetectionValve.MonitoredThreadState DONE
RUNNINGback to summary
public static final StuckThreadDetectionValve.MonitoredThreadState RUNNING
STUCKback to summary
public static final StuckThreadDetectionValve.MonitoredThreadState STUCK

Constructor Detail

MonitoredThreadStateback to summary
private MonitoredThreadState()

Method Detail

valueOfback to summary
public static StuckThreadDetectionValve.MonitoredThreadState valueOf(String name)
valuesback to summary
public static StuckThreadDetectionValve.MonitoredThreadState[] values()