# 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:
  `InvocationHandler, NotificationBroadcaster, NotificationEmitter, SlowQueryReportJmxMBean`

public class SlowQueryReportJmx extends SlowQueryReport implements 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.

- ## Nested Class Summary

  ### Nested classes/interfaces inherited from class SlowQueryReport

  `SlowQueryReport.QueryStats, SlowQueryReport.QueryStatsComparator`

  ### Nested classes/interfaces inherited from class AbstractQueryReport

  `AbstractQueryReport.StatementProxy`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final String`
  `FAILED_QUERY_NOTIFICATION`
  Notification type for failed query events.
  `protected static final ConcurrentHashMap<String, SlowQueryReportJmxMBean>`
  `mbeans`
  Registry of JMX MBeans for each connection pool.
  `protected NotificationBroadcasterSupport`
  `notifier`
  Notification broadcaster support for JMX notifications.
  `protected boolean`
  `notifyPool`
  Whether to send notifications to the pool.
  `protected static final AtomicLong`
  `notifySequence`
  Atomic sequence counter for notifications.
  `static final String`
  `objectNameAttribute`
  Property key for overriding the JMX object name.
  `protected ConnectionPool`
  `pool`
  The connection pool reference.
  `protected String`
  `poolName`
  The name of the connection pool.
  `static final String`
  `SLOW_QUERY_NOTIFICATION`
  Notification type for slow query events.
  `protected static CompositeType`
  `SLOW_QUERY_TYPE`
  Composite type used for JMX query statistics data.

  ### Fields inherited from class SlowQueryReport

  `logFailed, logSlow, maxQueries, perPoolStats, queries, queryStatsComparator`

  ### Fields inherited from class AbstractQueryReport

  `threshold`

  ### Fields inherited from class AbstractCreateStatementInterceptor

  `constructors, CREATE_STATEMENT, CREATE_STATEMENT_IDX, EXECUTE, EXECUTE_BATCH, 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 JdbcInterceptor

  `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

  Modifier and Type
  Method
  Description
  `void`
  `addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback)`
  Adds a notification listener.
  `protected void`
  `deregisterJmx()`
  Deregisters this interceptor from JMX.
  `protected static CompositeType`
  `getCompositeType()`
  Returns the composite type used for JMX query statistics.
  `MBeanNotificationInfo[]`
  `getNotificationInfo()`
  Returns information about the notifications this MBean can send.
  `ObjectName`
  `getObjectName(Class<?> clazz, String poolName)`
  Returns the JMX ObjectName for the given class and pool name.
  `String`
  `getPoolName()`
  JMX operation - return the name of the pool
  `String[]`
  `getPoolNames()`
  JMX operation - return the names of all the pools
  `CompositeData[]`
  `getSlowQueriesCD()`
  JMX operation - returns all the queries we have collected.
  `boolean`
  `isNotifyPool()`
  Returns whether notifications are sent to the pool.
  `protected void`
  `notifyJmx(String query, String type)`
  Sends a JMX notification for the given query and notification type.
  `void`
  `poolClosed(ConnectionPool pool)`
  Called when the connection pool is closed; deregisters JMX.
  `void`
  `poolStarted(ConnectionPool pool)`
  Called when the connection pool is started.
  `protected void`
  `registerJmx()`
  Registers this interceptor with JMX.
  `void`
  `removeNotificationListener(NotificationListener listener)`
  Removes a notification listener.
  `void`
  `removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback)`
  Removes a notification listener with the given filter and handback.
  `protected String`
  `reportFailedQuery(String query, Object[] args, String name, long start, Throwable t)`
  Reports a failed query and sends a JMX notification if logging is enabled.
  `protected String`
  `reportSlowQuery(String query, Object[] args, String name, long start, long delta)`
  Reports a slow query and sends a JMX notification if logging is enabled.
  `void`
  `reset(ConnectionPool parent, PooledConnection con)`
  Resets this interceptor for a new connection, registering JMX if applicable.
  `void`
  `resetStats()`
  JMX operation - remove all stats for this connection pool
  `void`
  `setNotifyPool(boolean notifyPool)`
  Sets whether notifications are sent to the pool.
  `void`
  `setProperties(Map<String, PoolProperties.InterceptorProperty> properties)`
  Sets the interceptor properties, including the notifyPool flag.

  ### Methods inherited from class SlowQueryReport

  `closeInvoked, getPoolStats, getQueryStats, isLogFailed, isLogSlow, prepareCall, prepareStatement, removeOldest, reportQuery, setLogFailed, setLogSlow, setMaxQueries`

  ### Methods inherited from class AbstractQueryReport

  `createStatement, getThreshold, setThreshold`

  ### Methods inherited from class AbstractCreateStatementInterceptor

  `getConstructor, invoke, isExecute, isStatement, process`

  ### Methods inherited from class JdbcInterceptor

  `compare, compare, disconnected, getNext, getProperties, isUseEquals, setNext, setUseEquals`

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### SLOW_QUERY_NOTIFICATION

    public static final String SLOW_QUERY_NOTIFICATION
    Notification type for slow query events.
    See Also:
      - Constant Field Values

  - ### FAILED_QUERY_NOTIFICATION

    public static final String FAILED_QUERY_NOTIFICATION
    Notification type for failed query events.
    See Also:
      - Constant Field Values

  - ### objectNameAttribute

    public static final String objectNameAttribute
    Property key for overriding the JMX object name.
    See Also:
      - Constant Field Values

  - ### SLOW_QUERY_TYPE

    protected static volatile CompositeType SLOW_QUERY_TYPE
    Composite type used for JMX query statistics data.

  - ### mbeans

    protected static final ConcurrentHashMap<String, SlowQueryReportJmxMBean> mbeans
    Registry of JMX MBeans for each connection pool.

  - ### notifier

    protected volatile NotificationBroadcasterSupport notifier
    Notification broadcaster support for JMX notifications.

  - ### poolName

    protected String poolName
    The name of the connection pool.

  - ### notifySequence

    protected static final 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.

- ## Constructor Details

  - ### SlowQueryReportJmx

    public SlowQueryReportJmx()
    Creates a new SlowQueryReportJmx instance.

- ## Method Details

  - ### addNotificationListener

    public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws IllegalArgumentException
    Adds a notification listener.
    Specified by:
      `addNotificationListener` in interface `NotificationBroadcaster`
    Parameters:
      `listener` - the listener to add
      `filter` - the notification filter
      `handback` - the handback object
    Throws:
      `IllegalArgumentException` - if the listener is null

  - ### getNotificationInfo

    public MBeanNotificationInfo[] getNotificationInfo()
    Returns information about the notifications this MBean can send.
    Specified by:
      `getNotificationInfo` in interface `NotificationBroadcaster`
    Returns:
      an array of MBeanNotificationInfo objects

  - ### removeNotificationListener

    public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException
    Removes a notification listener.
    Specified by:
      `removeNotificationListener` in interface `NotificationBroadcaster`
    Parameters:
      `listener` - the listener to remove
    Throws:
      `ListenerNotFoundException` - if the listener was not previously added

  - ### removeNotificationListener

    public void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws ListenerNotFoundException
    Removes a notification listener with the given filter and handback.
    Specified by:
      `removeNotificationListener` in interface `NotificationEmitter`
    Parameters:
      `listener` - the listener to remove
      `filter` - the notification filter
      `handback` - the handback object
    Throws:
      `ListenerNotFoundException` - if the listener was not previously added

  - ### getCompositeType

    protected static CompositeType getCompositeType()
    Returns the composite type used for JMX query statistics.
    Returns:
      the composite type, or `null` if initialization failed

  - ### reset

    public void reset(ConnectionPool parent, PooledConnection con)
    Resets this interceptor for a new connection, registering JMX if applicable.
    Overrides:
      `reset` in class `SlowQueryReport`
    Parameters:
      `parent` - the connection pool
      `con` - the pooled connection

  - ### poolClosed

    public void poolClosed(ConnectionPool pool)
    Called when the connection pool is closed; deregisters JMX.
    Overrides:
      `poolClosed` in class `SlowQueryReport`
    Parameters:
      `pool` - the connection pool being closed

  - ### poolStarted

    public void poolStarted(ConnectionPool pool)
    Called when the connection pool is started.
    Overrides:
      `poolStarted` in class `SlowQueryReport`
    Parameters:
      `pool` - the connection pool being started

  - ### reportFailedQuery

    protected String reportFailedQuery(String query, Object[] args, String name, long start, Throwable t)
    Reports a failed query and sends a JMX notification if logging is enabled.
    Overrides:
      `reportFailedQuery` in class `SlowQueryReport`
    Parameters:
      `query` - the SQL query
      `args` - the query arguments
      `name` - the connection pool name
      `start` - the start time in milliseconds
      `t` - the throwable that caused the failure
    Returns:
      the formatted query string

  - ### notifyJmx

    protected void notifyJmx(String query, String type)
    Sends a JMX notification for the given query and notification type.
    Parameters:
      `query` - the SQL query string
      `type` - the notification type (slow or failed query)

  - ### reportSlowQuery

    protected String reportSlowQuery(String query, Object[] args, String name, long start, long delta)
    Reports a slow query and sends a JMX notification if logging is enabled.
    Overrides:
      `reportSlowQuery` in class `SlowQueryReport`
    Parameters:
      `query` - the SQL query
      `args` - the query arguments
      `name` - the connection pool name
      `start` - the start time in milliseconds
      `delta` - the elapsed time in milliseconds
    Returns:
      the formatted query string

  - ### getPoolNames

    public String[] getPoolNames()
    JMX operation - return the names of all the pools
    Returns:
      - all the names of pools that we have stored data for

  - ### getPoolName

    public String getPoolName()
    JMX operation - return the name of the pool
    Returns:
      the name of the pool, unique within the JVM

  - ### 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.
    Parameters:
      `notifyPool` - true if notifications should be sent to the pool

  - ### resetStats

    public void resetStats()
    JMX operation - remove all stats for this connection pool

  - ### getSlowQueriesCD

    public CompositeData[] getSlowQueriesCD() throws OpenDataException
    JMX operation - returns all the queries we have collected.
    Specified by:
      `getSlowQueriesCD` in interface `SlowQueryReportJmxMBean`
    Returns:
      - the slow query report as composite data.
    Throws:
      `OpenDataException` - if the composite data cannot be created

  - ### deregisterJmx

    protected void deregisterJmx()
    Deregisters this interceptor from JMX.

  - ### getObjectName

    public ObjectName getObjectName(Class<?> clazz, String poolName) throws MalformedObjectNameException
    Returns the JMX ObjectName for the given class and pool name.
    Parameters:
      `clazz` - the interceptor class
      `poolName` - the name of the connection pool
    Returns:
      the JMX ObjectName
    Throws:
      `MalformedObjectNameException` - if the object name is malformed

  - ### registerJmx

    protected void registerJmx()
    Registers this interceptor with JMX.

  - ### setProperties

    public void setProperties(Map<String, PoolProperties.InterceptorProperty> properties)
    Sets the interceptor properties, including the notifyPool flag.
    Overrides:
      `setProperties` in class `SlowQueryReport`
    Parameters:
      `properties` - the interceptor properties