JBoss Cache includes JMX MBeans to expose cache functionality and provide statistics that can be used to analyze cache operations. JBoss Cache can also broadcast cache events as MBean notifications for handling via JMX monitoring tools.
JBoss Cache provides an MBean that allows JMX access to a cache instance. This MBean is accessible from an MBean server through the service name specified in the cache instance's configuration. For example, the Tomcat clustering cache instance is accessible through the service named "jboss.cache:service=TomcatClusteringCache." This MBean can be used to perform most cache operations via JMX.
JBoss Cache also provides MBeans for each interceptor configured in the cache's interceptor stack. These MBeans are used to capture and expose statistics related to cache operations. They are hierarchically associated with the cache's primary MBean and have service names that reflect this relationship. For example, a replication interceptor MBean for the TomcatClusteringCache instance will be accessible through the service named "jboss.cache:service=TomcatClusteringCache,treecache-interceptor=ReplicationInterceptor."
JBoss Cache captures statistics in its interceptors and exposes the statistics through interceptor MBeans. Cache interceptor MBeans are enabled by default; these MBeans can be disabled for a specific cache instance through the UseInterceptorMbeans attribute. See the Configuration chapter for further details on configuration of this attribute.
Each interceptor's MBean provides an attribute that can be used to disable maintenance of statistics for that interceptor. Note that the majority of the statistics are provided by the CacheMgmtInterceptor MBean so this interceptor is the most significant in this regard. If you want to disable all statistics for performance reasons, you should utilize the UseInterceptorMbeans configuration setting as this will prevent the CacheMgmtInterceptor from being included in the cache's interceptor stack when the cache is started.
Each interceptor provides the following common operations and attributes.
The following table describes the statistics currently available for JBoss Cache.
Table 12.1. JBoss Cache Management Statistics
MBean Name | Attribute | Type | Description |
---|---|---|---|
ActivationInterceptor | Activations | long | Number of passivated nodes that have been activated. |
CacheLoaderInterceptor | CacheLoaderLoads | long | Number of nodes loaded through a cache loader. |
CacheLoaderInterceptor | CacheLoaderMisses | long | Number of unsuccessful attempts to load a node through a cache loader. |
CacheMgmtInterceptor | Hits | long | Number of successful attribute retrievals. |
CacheMgmtInterceptor | Misses | long | Number of unsuccessful attribute retrievals. |
CacheMgmtInterceptor | Stores | long | Number of attribute store operations. |
CacheMgmtInterceptor | Evictions | long | Number of node evictions. |
CacheMgmtInterceptor | NumberOfAttributes | int | Number of attributes currently cached. |
CacheMgmtInterceptor | NumberOfNodes | int | Number of nodes currently cached. |
CacheMgmtInterceptor | ElapsedTime | long | Number of seconds that the cache has been running. |
CacheMgmtInterceptor | TimeSinceReset | long | Number of seconds since the cache statistics have been reset. |
CacheMgmtInterceptor | AverageReadTime | long | Average time in milliseconds to retrieve a cache attribute, including unsuccessful attribute retrievals. |
CacheMgmtInterceptor | AverageWriteTime | long | Average time in milliseconds to write a cache attribute. |
CacheMgmtInterceptor | HitMissRatio | double | Ratio of hits to hits and misses. A hit is a get attribute operation that results in an object being returned to the client. The retrieval may be from a cache loader if the entry isn't in the local cache. |
CacheMgmtInterceptor | ReadWriteRatio | double | Ratio of read operations to write operations. This is the ratio of cache hits and misses to cache stores. |
CacheStoreInterceptor | CacheLoaderStores | long | Number of nodes written to the cache loader. |
InvalidationInterceptor | Invalidations | long | Number of cached nodes that have been invalidated. |
PassivationInterceptor | Passivations | long | Number of cached nodes that have been passivated. |
TxInterceptor | Prepares | long | Number of transaction prepare operations performed by this interceptor. |
TxInterceptor | Commits | long | Number of transaction commit operations performed by this interceptor. |
TxInterceptor | Rollbacks | long | Number of transaction rollbacks operations performed by this interceptor. |
JBoss Cache users can register a listener to receive cache events as described in the Eviction Policies chapter. Users can alternatively utilize the cache's management information infrastructure to receive these events via JMX notifications. Cache events are accessible as notifications by registering a NotificationListener for the CacheMgmtInterceptor MBean. This functionality is only available if cache statistics are enabled as described in the previous section.
The following table depicts the JMX notifications available for JBoss Cache as well as the cache events to which they correspond. These are the notifications that can be received through the CacheMgmtInterceptor MBean. Each notification represents a single event published by JBoss Cache and provides user data corresponding to the parameters of the event.
Table 12.2. JBoss Cache MBean Notifications
Notification Type | Notification Data | TreeCacheListener Event | |||
---|---|---|---|---|---|
org.jboss.cache.CacheStarted | String : cache service name | cacheStarted | |||
org.jboss.cache.CacheStopped | String : cache service name | cacheStopped | |||
org.jboss.cache.NodeCreated | String : fqn | NodeCreated | |||
org.jboss.cache.NodeEvicted | String : fqn | NodeEvicted | |||
org.jboss.cache.NodeLoaded | String : fqn | NodeLoaded | |||
org.jboss.cache.NodeModifed | String : fqn | NodeModifed | |||
org.jboss.cache.NodeRemoved | String : fqn | NodeRemoved | |||
org.jboss.cache.NodeVisited | String : fqn | NodeVisited | |||
org.jboss.cache.ViewChange | String : view | ViewChange | |||
org.jboss.cache.NodeActivate |
| NodeActivate | |||
org.jboss.cache.NodeEvict |
| NodeEvict | |||
org.jboss.cache.NodeModify |
| NodeModify | |||
org.jboss.cache.NodePassivate |
| NodePassivate | |||
org.jboss.cache.NodeRemove |
| NodeRemove |
The following is an example of how to programmatically receive cache notifications when running in a JBoss application server environment. In this example, the client uses a filter to specify which events are of interest.
MyListener listener = new MyListener(); NotificationFilterSupport filter = null; // get reference to MBean server Context ic = new InitialContext(); MBeanServerConnection server = (MBeanServerConnection)ic.lookup("jmx/invoker/RMIAdaptor"); // get reference to CacheMgmtInterceptor MBean String cache_service = "jboss.cache:service=TomcatClusteringCache"; String mgmt_service = cache_service + ",treecache-interceptor=CacheMgmtInterceptor"; ObjectName mgmt_name = new ObjectName(mgmt_service); // configure a filter to only receive node created and removed events filter = new NotificationFilterSupport(); filter.disableAllTypes(); filter.enableType(CacheMgmtInterceptor.NOTIF_NODE_CREATED); filter.enableType(CacheMgmtInterceptor.NOTIF_NODE_REMOVED); // register the listener with a filter // leave the filter null to receive all cache events server.addNotificationListener(mgmt_name, listener, filter, null); // ... // on completion of processing, unregister the listener server.removeNotificationListener(mgmt_name, listener, filter, null);
The following is the simple notification listener implementation used in the previous example.
private class MyListener implements NotificationListener, Serializable { public void handleNotification(Notification notification, Object handback) { String message = notification.getMessage(); String type = notification.getType(); Object userData = notification.getUserData(); System.out.println(type + ": "+message); if (userData == null) { System.out.println("notification data is null"); } else if (userData instanceof String) { System.out.println("notification data: "+(String)userData); } else if (userData instanceof Object[]) { Object[] ud = (Object[])userData; for (int i = 0; i > ud.length; i++) { System.out.println("notification data: "+ud[i].toString()); } } else { System.out.println("notification data class: " + userData.getClass().getName()); } } }
Note: the JBoss Cache management implementation only listens to cache events after a client registers to receive MBean notifications. As soon as no clients are registered for notifications, the MBean will remove itself as a cache listener.
JBoss Cache MBeans are readily accessed when running cache instances in an application server that provides an MBean server interface such as JBoss JMX Console. Refer to server documentation for instructions on how to access MBeans running in a server's MBean container.
JBoss Cache MBeans are also accessible when running in a non-server environment if the JVM is JDK 5.0 or later. When running a standalone cache in a JDK 5 environment, you can access the cache's MBeans as follows.
Note: The jconsole utility will automatically register as a listener for cache notifications when connected to a JVM running JBoss Cache instances.
The following figure shows cache interceptor MBeans in jconsole. Cache statistics are displayed for the CacheMgmt interceptor: