Class SlowQueryReportJmx
- java.lang.Object
-
- org.apache.tomcat.jdbc.pool.JdbcInterceptor
-
- org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor
-
- org.apache.tomcat.jdbc.pool.interceptor.AbstractQueryReport
-
- org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport
-
- org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReportJmx
-
- All Implemented Interfaces:
java.lang.reflect.InvocationHandler,javax.management.NotificationBroadcaster,javax.management.NotificationEmitter,SlowQueryReportJmxMBean
public class SlowQueryReportJmx extends SlowQueryReport implements javax.management.NotificationEmitter, SlowQueryReportJmxMBean
JMX-enabled slow query report interceptor. Publishes slow and failed query statistics to JMX and provides notifications when slow or failed queries are detected.With
notifyPool=falsethis interceptor registers its own MBean and broadcaster. In the defaultnotifyPool=truemode it registers nothing and raises notifications on the pool's own MBean instead. The MBean belongs to a pool and is published by the first borrow under the name the pool started with; the publication is attempted once and given up by the pool's close. Names must be unique among live pools.The published resource is a
SlowQueryReportJmx.Registrationholding the pool name, the statistics store and the pool's JMX state, but not the pool itself, so an abandoned pool is not pinned by the MBean server. Listener subscriptions belong to the registration they were added through and must be re-attached after a pool closes.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classSlowQueryReportJmx.PoolJmxEverything this class publishes for one pool: the notification broadcaster its clients attach their listeners to and, in thenotifyPool=falsemode, the ObjectName the pool's MBean occupies.static classSlowQueryReportJmx.RegistrationThe resource published with the MBeanServer for one pool.static interfaceSlowQueryReportJmx.RegistrationMBeanThe management interface of a publishedSlowQueryReportJmx.Registrationresource.-
Nested classes/interfaces inherited from class org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport
SlowQueryReport.PoolStatistics, SlowQueryReport.QueryStats, SlowQueryReport.QueryStatsComparator
-
Nested classes/interfaces inherited from class org.apache.tomcat.jdbc.pool.interceptor.AbstractQueryReport
AbstractQueryReport.StatementProxy
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringFAILED_QUERY_NOTIFICATIONNotification type for failed query events, shared with the pool's own MBean for the same reason asSLOW_QUERY_NOTIFICATION.protected javax.management.NotificationBroadcasterSupportnotifierNotification broadcaster used while this instance serves no pool, and the one a notification of a pool that has already come down is sent through; while it serves a live pool,getNotifier()returns the broadcaster shared by all interceptor instances of that pool.protected static java.lang.StringNOTIFY_POOLThe name of the property that configuressetNotifyPool(boolean).protected booleannotifyPoolWhether to send notifications to the pool.protected static java.util.concurrent.atomic.AtomicLongnotifySequenceAtomic sequence counter for notifications.static java.lang.StringobjectNameAttributeProperty key for overriding the JMX object name.protected ConnectionPoolpoolThe connection pool reference.protected java.lang.StringpoolNameThe name of the connection pool.static java.lang.StringSLOW_QUERY_NOTIFICATIONNotification type for slow query events.protected static javax.management.openmbean.CompositeTypeSLOW_QUERY_TYPEComposite type used for JMX query statistics data.-
Fields inherited from class org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport
LOG_FAILED, LOG_SLOW, logFailed, logSlow, MAX_QUERIES, maxQueries, perPoolStats, poolStatistics, queries, queryStatsComparator, THRESHOLD
-
Fields inherited from class org.apache.tomcat.jdbc.pool.interceptor.AbstractQueryReport
threshold
-
Fields inherited from class org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor
CREATE_STATEMENT, CREATE_STATEMENT_IDX, EXECUTE, EXECUTE_BATCH, EXECUTE_LARGE_BATCH, EXECUTE_LARGE_UPDATE, EXECUTE_QUERY, EXECUTE_TYPES, EXECUTE_UPDATE, GET_GENERATED_KEYS, GET_RESULTSET, GET_STATEMENT, PREPARE_CALL, PREPARE_CALL_IDX, PREPARE_STATEMENT, PREPARE_STATEMENT_IDX, RESULTSET_IDX, STATEMENT_TYPE_COUNT, STATEMENT_TYPES
-
Fields inherited from class org.apache.tomcat.jdbc.pool.JdbcInterceptor
ABORT_VAL, CLOSE_VAL, EQUALS_VAL, GETCONNECTION_VAL, HASHCODE_VAL, ISCLOSED_VAL, ISVALID_VAL, ISWRAPPERFOR_VAL, properties, TOSTRING_VAL, UNWRAP_VAL
-
-
Constructor Summary
Constructors Constructor Description SlowQueryReportJmx()Creates a new SlowQueryReportJmx instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddNotificationListener(javax.management.NotificationListener listener, javax.management.NotificationFilter filter, java.lang.Object handback)Adds a notification listener.protected static javax.management.openmbean.CompositeTypegetCompositeType()Returns the composite type used for JMX query statistics.javax.management.MBeanNotificationInfo[]getNotificationInfo()Returns information about the notifications this MBean can send.protected java.lang.ObjectgetNotificationSource()Returns the source this MBean's notifications are sent with.protected javax.management.NotificationBroadcasterSupportgetNotifier()Returns the notification broadcaster shared by all interceptor instances of the current pool, so that notifications raised on any connection reach the listeners registered with the pool's MBean.javax.management.ObjectNamegetObjectName(java.lang.Class<?> clazz, java.lang.String poolName)Returns the JMX ObjectName for the given class and pool name.java.lang.StringgetPoolName()JMX operation - return the name of the pool.java.lang.String[]getPoolNames()JMX operation - return the names of all the poolsjavax.management.openmbean.CompositeData[]getSlowQueriesCD()JMX operation - returns all the queries we have collected.protected booleanisKnownProperty(java.lang.String name)Recognises the two names this class reads on top of the ones the base reads:notifyPool, which decides which notification channel reports a query, andobjectName, which names the MBean (seegetObjectName(Class, String)).booleanisNotifyPool()Returns whether notifications are sent to the pool.protected static SlowQueryReportJmx.PoolJmxjmxStateFor(ConnectionPool pool)Returns the JMX state published for the given pool.protected voidnotifyJmx(java.lang.String query, java.lang.String type)Sends a JMX notification for the given query and notification type.voidpoolStarted(ConnectionPool pool)Called when the connection pool is started.protected voidregisterJmx()Registers this pool's MBean with JMX.voidremoveNotificationListener(javax.management.NotificationListener listener)Removes a notification listener.voidremoveNotificationListener(javax.management.NotificationListener listener, javax.management.NotificationFilter filter, java.lang.Object handback)Removes a notification listener with the given filter and handback.protected java.lang.StringreportFailedQuery(java.lang.String query, java.lang.Object[] args, java.lang.String name, long start, long delta, java.lang.Throwable t)Reports a failed query and sends a JMX notification if logging is enabled.protected java.lang.StringreportSlowQuery(java.lang.String query, java.lang.Object[] args, java.lang.String name, long start, long delta)Reports a slow query and sends a JMX notification if logging is enabled.voidreset(ConnectionPool parent, PooledConnection con)Resets this interceptor for a new connection, registering JMX if applicable.voidresetStats()JMX operation - remove all stats for this connection pool.voidsetNotifyPool(boolean notifyPool)Sets whether notifications are sent to the pool.voidsetProperties(java.util.Map<java.lang.String,PoolProperties.InterceptorProperty> properties)Sets the interceptor properties, including the notifyPool flag.-
Methods inherited from class org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport
booksSharedStats, closeInvoked, flagValue, getPoolStats, getQueryStats, isLogFailed, isLogSlow, poolClosed, prepareCall, prepareStatement, releaseStatistics, removeOldest, reportQuery, setLogFailed, setLogSlow, setMaxQueries, statisticsFor
-
Methods inherited from class org.apache.tomcat.jdbc.pool.interceptor.AbstractQueryReport
createStatement, getThreshold, setThreshold
-
Methods inherited from class org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor
elapsedMillis, getConstructor, invoke, isExecute, isStatement, process
-
Methods inherited from class org.apache.tomcat.jdbc.pool.JdbcInterceptor
compare, compare, disconnected, getNext, getProperties, isUseEquals, setNext, setUseEquals, unregisterMBean
-
-
-
-
Field Detail
-
SLOW_QUERY_NOTIFICATION
public static final java.lang.String SLOW_QUERY_NOTIFICATION
Notification type for slow query events. The pool's own MBean reports the same event under the same type when this interceptor is configured withnotifyPool=true, so this is that very type and not a copy of its text: a client filters notifications on the type, and the two channels have to answer to one string.- See Also:
- Constant Field Values
-
FAILED_QUERY_NOTIFICATION
public static final java.lang.String FAILED_QUERY_NOTIFICATION
Notification type for failed query events, shared with the pool's own MBean for the same reason asSLOW_QUERY_NOTIFICATION.- See Also:
- Constant Field Values
-
objectNameAttribute
public static final java.lang.String objectNameAttribute
Property key for overriding the JMX object name.- See Also:
- Constant Field Values
-
NOTIFY_POOL
protected static final java.lang.String NOTIFY_POOL
The name of the property that configuressetNotifyPool(boolean). Spelled once, here, becausesetProperties(java.util.Map)applies it andisKnownProperty(String)reports the names it does not read.- See Also:
- Constant Field Values
-
SLOW_QUERY_TYPE
protected static volatile javax.management.openmbean.CompositeType SLOW_QUERY_TYPE
Composite type used for JMX query statistics data.
-
notifier
protected volatile javax.management.NotificationBroadcasterSupport notifier
Notification broadcaster used while this instance serves no pool, and the one a notification of a pool that has already come down is sent through; while it serves a live pool,getNotifier()returns the broadcaster shared by all interceptor instances of that pool. Like the shared broadcasters, it is built withNOTIFICATION_INFO, so every handle's emitter surface advertises the types it sends.
-
poolName
protected java.lang.String poolName
The name of the connection pool.
-
notifySequence
protected static final java.util.concurrent.atomic.AtomicLong notifySequence
Atomic sequence counter for notifications.
-
notifyPool
protected boolean notifyPool
Whether to send notifications to the pool.
-
pool
protected ConnectionPool pool
The connection pool reference.
-
-
Method Detail
-
addNotificationListener
public void addNotificationListener(javax.management.NotificationListener listener, javax.management.NotificationFilter filter, java.lang.Object handback) throws java.lang.IllegalArgumentExceptionAdds a notification listener.- Specified by:
addNotificationListenerin interfacejavax.management.NotificationBroadcaster- Parameters:
listener- the listener to addfilter- the notification filterhandback- the handback object- Throws:
java.lang.IllegalArgumentException- if the listener is null
-
getNotificationInfo
public javax.management.MBeanNotificationInfo[] getNotificationInfo()
Returns information about the notifications this MBean can send.- Specified by:
getNotificationInfoin interfacejavax.management.NotificationBroadcaster- Returns:
- an array of MBeanNotificationInfo objects
-
removeNotificationListener
public void removeNotificationListener(javax.management.NotificationListener listener) throws javax.management.ListenerNotFoundExceptionRemoves a notification listener.- Specified by:
removeNotificationListenerin interfacejavax.management.NotificationBroadcaster- Parameters:
listener- the listener to remove- Throws:
javax.management.ListenerNotFoundException- if the listener was not previously added
-
removeNotificationListener
public void removeNotificationListener(javax.management.NotificationListener listener, javax.management.NotificationFilter filter, java.lang.Object handback) throws javax.management.ListenerNotFoundExceptionRemoves a notification listener with the given filter and handback.- Specified by:
removeNotificationListenerin interfacejavax.management.NotificationEmitter- Parameters:
listener- the listener to removefilter- the notification filterhandback- the handback object- Throws:
javax.management.ListenerNotFoundException- if the listener was not previously added
-
getCompositeType
protected static javax.management.openmbean.CompositeType getCompositeType()
Returns the composite type used for JMX query statistics.- Returns:
- the composite type, or
nullif initialization failed
-
jmxStateFor
protected static SlowQueryReportJmx.PoolJmx jmxStateFor(ConnectionPool pool)
Returns the JMX state published for the given pool.- Parameters:
pool- the pool whose published state is wanted- Returns:
- what has been published for that pool, or
nullwhile nothing has been - because the pool has no statistics store, because no borrow of it has needed the state yet, or because its close has given the state up along with the store
-
reset
public void reset(ConnectionPool parent, PooledConnection con)
Resets this interceptor for a new connection, registering JMX if applicable.- Overrides:
resetin classSlowQueryReport- Parameters:
parent- the connection poolcon- the pooled connection
-
poolStarted
public void poolStarted(ConnectionPool pool)
Called when the connection pool is started.- Overrides:
poolStartedin classSlowQueryReport- Parameters:
pool- the connection pool being started
-
reportFailedQuery
protected java.lang.String reportFailedQuery(java.lang.String query, java.lang.Object[] args, java.lang.String name, long start, long delta, java.lang.Throwable t)Reports a failed query and sends a JMX notification if logging is enabled.- Overrides:
reportFailedQueryin classSlowQueryReport- Parameters:
query- the SQL queryargs- the query argumentsname- the name of the method used to executestart- the wall clock time the query execution starteddelta- the time the execution took before it failed, measured with the monotonic clockt- the throwable that caused the failure- Returns:
- the formatted query string
-
notifyJmx
protected void notifyJmx(java.lang.String query, java.lang.String type)Sends a JMX notification for the given query and notification type.- Parameters:
query- the SQL query stringtype- the notification type (slow or failed query)
-
getNotifier
protected javax.management.NotificationBroadcasterSupport getNotifier()
Returns the notification broadcaster shared by all interceptor instances of the current pool, so that notifications raised on any connection reach the listeners registered with the pool's MBean.- Returns:
- the shared broadcaster for this interceptor's pool
-
getNotificationSource
protected java.lang.Object getNotificationSource()
Returns the source this MBean's notifications are sent with.Notificationmarshals its source, so a listener outside this JVM - one reached through a JMX connector, or one that hands the notification on to another process - only receives the notification at all if the source is serializable. This MBean is not, and neither is the pool it reports on, so the source is theObjectNamethe MBean is registered under, which names the pool and is serializable. Interceptor instances are created per pooled connection and none of them is the published resource - that is the pool'sSlowQueryReportJmx.Registration- so an instance cannot name itself: the source is the ObjectName the pool's JMX state records as published, which every instance of the pool sends alike. That includes the instances of a second definition on one pool: the two-thresholds configuration publishes one MBean, and an instance that published nothing must not stamp its notifications with the name its own properties would compute - that name is registered under nothing, and a client filtering or grouping by source reads the published one. An instance whose name cannot be formed - a malformedobjectNameproperty or a pool name that cannot be quoted into an ObjectName - was never registered at all, so no listener reached through a connector can exist for it; the source is then a string of the default name's shape, which still names the pool for the local listeners the broadcaster serves. A pool that carries no name is the same case taken to its end: it holds no registration to name itself by, and the shape its source takes is the default one with no name in it - whatever survives delivery, it cannot name a pool that never had a name to register.- Returns:
- the ObjectName this interceptor's pool is published under, or, when nothing is published for it or the published name cannot be told, a name formed from this instance - as an ObjectName where that succeeds, and as a string of the default name's shape where it does not
-
reportSlowQuery
protected java.lang.String reportSlowQuery(java.lang.String query, java.lang.Object[] args, java.lang.String name, long start, long delta)Reports a slow query and sends a JMX notification if logging is enabled.- Overrides:
reportSlowQueryin classSlowQueryReport- Parameters:
query- the SQL queryargs- the query argumentsname- the connection pool namestart- the start time in millisecondsdelta- the elapsed time in milliseconds- Returns:
- the formatted query string
-
getPoolNames
public java.lang.String[] getPoolNames()
JMX operation - return the names of all the pools- Specified by:
getPoolNamesin interfaceSlowQueryReportJmxMBean- Returns:
- - all the names of pools that we have stored data for, which is a view of the JVM-wide statistics registry rather than of this pool alone
-
getPoolName
public java.lang.String getPoolName()
JMX operation - return the name of the pool.- Specified by:
getPoolNamein interfaceSlowQueryReportJmxMBean- Returns:
- the registration name recorded when this instance was bound to its pool, or
nullbefore binding. The value is retained when the chain is released withreset(null, null), so it can remain non-null after that release
-
isNotifyPool
public boolean isNotifyPool()
Returns whether notifications are sent to the pool.- Returns:
- true if notifications are sent to the pool
-
setNotifyPool
public void setNotifyPool(boolean notifyPool)
Sets whether notifications are sent to the pool. Configuration entry point only, applied to every interceptor instance of a pool bysetPropertieswhile its connection chain is built: it is not part of the JMX management interface, because writing it through the registered MBean would reach only the one instance that owns the registration and leave the pool's other connections routing as before. SeeSlowQueryReportJmxMBean.- Parameters:
notifyPool- true if notifications should be sent to the pool
-
resetStats
public void resetStats()
JMX operation - remove all stats for this connection pool. The statistics belong to this interceptor's pool, so what this clears is what that pool has recorded - and nothing another pool collected, however it was named. Only the recorded content goes: the statistics store stays registered with the pool, which keeps accumulating into it.- Specified by:
resetStatsin interfaceSlowQueryReportJmxMBean
-
getSlowQueriesCD
public javax.management.openmbean.CompositeData[] getSlowQueriesCD() throws javax.management.openmbean.OpenDataExceptionJMX operation - returns all the queries we have collected.- Specified by:
getSlowQueriesCDin interfaceSlowQueryReportJmxMBean- Returns:
- - the slow query report as composite data; an empty array when this interceptor's pool publishes no statistics - because it never registered a store, or because its close has released it
- Throws:
javax.management.openmbean.OpenDataException- if the composite data cannot be created; a JMX client gets it wrapped in anMBeanException
-
registerJmx
protected void registerJmx()
Registers this pool's MBean with JMX. The publication is attempted once per pool by the first borrow that needs it; a refusal is logged and not retried. A pool with no statistics store, or whose store has been released, has nothing to publish.
-
setProperties
public void setProperties(java.util.Map<java.lang.String,PoolProperties.InterceptorProperty> properties)
Sets the interceptor properties, including the notifyPool flag.The flag is read the way the inherited properties are: a value that states neither true nor false is reported and leaves the flag as it stands, rather than being read as the
falsethe raw parser answered with - which would have moved the pool's notifications off the route its default selects without saying so.- Overrides:
setPropertiesin classSlowQueryReport- Parameters:
properties- the interceptor properties
-
getObjectName
public javax.management.ObjectName getObjectName(java.lang.Class<?> clazz, java.lang.String poolName) throws javax.management.MalformedObjectNameExceptionReturns the JMX ObjectName for the given class and pool name.A property carrying no value - which the interceptor-string grammar cannot produce, but a programmatically built
PoolProperties.InterceptorDefinitioncan hand through - names nothing to override, and reads as if the property were absent, the way every sibling property of this family reads its null value.- Parameters:
clazz- the interceptor classpoolName- the name of the connection pool- Returns:
- the JMX ObjectName
- Throws:
javax.management.MalformedObjectNameException- if the object name is malformed
-
isKnownProperty
protected boolean isKnownProperty(java.lang.String name)
Recognises the two names this class reads on top of the ones the base reads:notifyPool, which decides which notification channel reports a query, andobjectName, which names the MBean (seegetObjectName(Class, String)). Without them here, every correct configuration of this interceptor would be reported as a typo by the property check it inherits.- Overrides:
isKnownPropertyin classSlowQueryReport- Parameters:
name- the property name to test- Returns:
trueif this configuration applies that name
-
-