public class SlowQueryListener extends Object implements QueryExecutionListener
onSlowQuery(ExecutionInfo, List, long) callback method
is called. The callback is called only once for the target query if it exceeds the threshold time.
NOTE:
ExecutionInfo.elapsedTime contains the time when callback is triggered which usually is the specified threshold time.
If you want to log or do something with AFTER execution that has exceeded specified threshold time, use normal
logging listener like following:
long thresholdInMills = ...
SLF4JQueryLoggingListener listener = new SLF4JQueryLoggingListener(){
@Override
public void afterQuery(ExecutionInfo execInfo, List queryInfoList) {
if (execInfo.getElapsedTime() >= thresholdInMills) {
super.afterQuery(execInfo, queryInfoList);
}
}
};
CommonsSlowQueryListener,
JULSlowQueryListener,
SLF4JSlowQueryListener,
SystemOutSlowQueryListener| Modifier and Type | Class and Description |
|---|---|
protected static class |
SlowQueryListener.RunningQueryContext
Data holder for currently running query.
|
| Modifier and Type | Field and Description |
|---|---|
protected ScheduledExecutorService |
executor |
protected Map<String,SlowQueryListener.RunningQueryContext> |
inExecution |
protected StopwatchFactory |
stopwatchFactory |
protected long |
threshold |
protected TimeUnit |
thresholdTimeUnit |
protected boolean |
useDaemonThread |
DEFAULT| Constructor and Description |
|---|
SlowQueryListener() |
| Modifier and Type | Method and Description |
|---|---|
void |
afterQuery(ExecutionInfo execInfo,
List<QueryInfo> queryInfoList) |
void |
beforeQuery(ExecutionInfo execInfo,
List<QueryInfo> queryInfoList) |
protected String |
getExecutionInfoKey(ExecutionInfo executionInfo)
Calculate a key for given
ExecutionInfo. |
ScheduledExecutorService |
getExecutor() |
long |
getThreshold() |
TimeUnit |
getThresholdTimeUnit() |
protected void |
onSlowQuery(ExecutionInfo execInfo,
List<QueryInfo> queryInfoList,
long startTimeInMills)
Callback when query execution time exceeds the threshold.
|
void |
setStopwatchFactory(StopwatchFactory stopwatchFactory)
Set
StopwatchFactory which is used to calculate ExecutionInfo.getElapsedTime() for slow queries. |
void |
setThreshold(long threshHold) |
void |
setThresholdTimeUnit(TimeUnit thresholdTimeUnit) |
void |
setUseDaemonThread(boolean useDaemonThread)
When set to
true(default), the executor creates daemon threads to check slow queries. |
protected boolean useDaemonThread
protected ScheduledExecutorService executor
protected long threshold
protected TimeUnit thresholdTimeUnit
protected Map<String,SlowQueryListener.RunningQueryContext> inExecution
protected StopwatchFactory stopwatchFactory
public void beforeQuery(ExecutionInfo execInfo, List<QueryInfo> queryInfoList)
beforeQuery in interface QueryExecutionListenerpublic void afterQuery(ExecutionInfo execInfo, List<QueryInfo> queryInfoList)
afterQuery in interface QueryExecutionListenerprotected String getExecutionInfoKey(ExecutionInfo executionInfo)
ExecutionInfo.
This key is passed to the slow query check Runnable as well as for removal in afterQuery(ExecutionInfo, List).
Default implementation uses System.identityHashCode(Object). This does NOT guarantee 100% of uniqueness; however, since
the current usage of the key is short lived and good enough for this use case.
Subclass can override this method to provide different implementation to uniquely represent ExecutionInfo.
executionInfo - execution infoprotected void onSlowQuery(ExecutionInfo execInfo, List<QueryInfo> queryInfoList, long startTimeInMills)
execInfo - query execution infoqueryInfoList - query parameter infostartTimeInMills - time in mills when the query startedpublic void setThreshold(long threshHold)
public void setThresholdTimeUnit(TimeUnit thresholdTimeUnit)
public ScheduledExecutorService getExecutor()
public long getThreshold()
public TimeUnit getThresholdTimeUnit()
public void setUseDaemonThread(boolean useDaemonThread)
true(default), the executor creates daemon threads to check slow queries.useDaemonThread - use daemon thread or not. (default is true)public void setStopwatchFactory(StopwatchFactory stopwatchFactory)
StopwatchFactory which is used to calculate ExecutionInfo.getElapsedTime() for slow queries.stopwatchFactory - factory to create Stopwatch used for calculating elapsed time for slow queriesCopyright © 2025. All rights reserved.