Author: tv
Date: Wed Jan 8 11:16:37 2014
New Revision: 1556494
URL: http://svn.apache.org/r1556494
Log:
Add simple JMX monitoring feature by exposing the JCSAdminBean to JMX
Added:
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/JCSJMXBean.java
(with props)
commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/admin/TestJMX.java
(with props)
Modified:
commons/proper/jcs/trunk/src/changes/changes.xml
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/CacheElementInfo.java
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/CacheRegionInfo.java
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/JCSAdmin.jsp
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/JCSAdminBean.java
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java
commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/admin/AdminBeanUnitTest.java
Modified: commons/proper/jcs/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/changes/changes.xml?rev=1556494&r1=1556493&r2=1556494&view=diff
==============================================================================
--- commons/proper/jcs/trunk/src/changes/changes.xml (original)
+++ commons/proper/jcs/trunk/src/changes/changes.xml Wed Jan 8 11:16:37 2014
@@ -20,6 +20,9 @@
</properties>
<body>
<release version="2.0" date="unreleased" description="JDK 1.5
based major release">
+ <action dev="tv" type="add">
+ Add simple JMX monitoring feature by exposing the JCSAdminBean
to JMX
+ </action>
<action dev="tv" type="update" issue="JCS-109" due-to="Xiong LIU">
Improve performance of BlockDisk.write(Serializable)
</action>
Modified:
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/CacheElementInfo.java
URL:
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/CacheElementInfo.java?rev=1556494&r1=1556493&r2=1556494&view=diff
==============================================================================
---
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/CacheElementInfo.java
(original)
+++
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/CacheElementInfo.java
Wed Jan 8 11:16:37 2014
@@ -1,5 +1,7 @@
package org.apache.commons.jcs.admin;
+import java.beans.ConstructorProperties;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -25,21 +27,42 @@ package org.apache.commons.jcs.admin;
public class CacheElementInfo
{
/** element key */
- String key = null;
+ private String key = null;
/** is it eternal */
- boolean eternal = false;
+ private boolean eternal = false;
/** when it was created */
- String createTime = null;
+ private String createTime = null;
/** max life */
- long maxLifeSeconds = -1;
+ private long maxLifeSeconds = -1;
/** when it will expire */
- long expiresInSeconds = -1;
+ private long expiresInSeconds = -1;
/**
+ * Parameterized constructor
+ *
+ * @param key element key
+ * @param eternal is it eternal
+ * @param createTime when it was created
+ * @param maxLifeSeconds max life
+ * @param expiresInSeconds when it will expire
+ */
+ @ConstructorProperties({"key", "eternal", "createTime", "maxLifeSeconds",
"expiresInSeconds"})
+ public CacheElementInfo(String key, boolean eternal, String createTime,
+ long maxLifeSeconds, long expiresInSeconds)
+ {
+ super();
+ this.key = key;
+ this.eternal = eternal;
+ this.createTime = createTime;
+ this.maxLifeSeconds = maxLifeSeconds;
+ this.expiresInSeconds = expiresInSeconds;
+ }
+
+ /**
* @return a string representation of the key
*/
public String getKey()
Modified:
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/CacheRegionInfo.java
URL:
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/CacheRegionInfo.java?rev=1556494&r1=1556493&r2=1556494&view=diff
==============================================================================
---
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/CacheRegionInfo.java
(original)
+++
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/CacheRegionInfo.java
Wed Jan 8 11:16:37 2014
@@ -1,5 +1,7 @@
package org.apache.commons.jcs.admin;
+import java.beans.ConstructorProperties;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -19,51 +21,142 @@ package org.apache.commons.jcs.admin;
* under the License.
*/
-import org.apache.commons.jcs.engine.control.CompositeCache;
/**
* Stores info on a cache region for the template
*/
public class CacheRegionInfo
{
- /** The cache region we are getting info on. */
- CompositeCache<?,?> cache = null;
+ /** The name of the cache region */
+ private String cacheName;
- /** number of bytes counted so far, will be a total of all items. */
- long byteCount = 0;
+ /** The size of the cache region */
+ private int cacheSize;
- /**
- * @return the underlying region
- */
- public CompositeCache<?, ?> getCache()
- {
- return this.cache;
- }
+ /** The status of the cache region */
+ private String cacheStatus;
+
+ /** The statistics of the cache region */
+ private String cacheStatistics;
+
+ /** The number of memory hits in the cache region */
+ private int hitCountRam;
+
+ /** The number of auxiliary hits in the cache region */
+ private int hitCountAux;
+
+ /** The number of misses in the cache region because the items were not
found */
+ private int missCountNotFound;
+
+ /** The number of misses in the cache region because the items were
expired */
+ private int missCountExpired;
+
+ /** The number of bytes counted so far, will be a total of all items */
+ private long byteCount;
/**
- * @return total byte count
+ * Parameterized constructor
+ *
+ * @param cacheName The name of the cache region
+ * @param cacheSize The size of the cache region
+ * @param cacheStatus The status of the cache region
+ * @param cacheStatistics The statistics of the cache region
+ * @param hitCountRam The number of memory hits in the cache region
+ * @param hitCountAux The number of auxiliary hits in the cache region
+ * @param missCountNotFound The number of misses in the cache region
because the items were not found
+ * @param missCountExpired The number of misses in the cache region
because the items were expired
+ * @param byteCount The number of bytes counted so far, will be a total
of all items
+ */
+ @ConstructorProperties({"cacheName", "cacheSize", "cacheStatus",
"cacheStatistics",
+ "hitCountRam", "hitCountAux", "missCountNotFound", "missCountExpired",
"byteCount"})
+ public CacheRegionInfo(String cacheName, int cacheSize, String
cacheStatus,
+ String cacheStatistics, int hitCountRam, int
hitCountAux,
+ int missCountNotFound, int missCountExpired, long
byteCount)
+ {
+ super();
+ this.cacheName = cacheName;
+ this.cacheSize = cacheSize;
+ this.cacheStatus = cacheStatus;
+ this.cacheStatistics = cacheStatistics;
+ this.hitCountRam = hitCountRam;
+ this.hitCountAux = hitCountAux;
+ this.missCountNotFound = missCountNotFound;
+ this.missCountExpired = missCountExpired;
+ this.byteCount = byteCount;
+ }
+
+ /**
+ * @return the cacheName
+ */
+ public String getCacheName()
+ {
+ return this.cacheName;
+ }
+
+ /**
+ * @return the cacheSize
+ */
+ public int getCacheSize()
+ {
+ return this.cacheSize;
+ }
+
+ /**
+ * @return a status string
*/
- public long getByteCount()
+ public String getCacheStatus()
{
- return this.byteCount;
+ return this.cacheStatus;
}
/**
- * @return a status string
+ * Return the statistics for the region.
+ * <p>
+ * @return String
*/
- public String getStatus()
+ public String getCacheStatistics()
{
- return this.cache.getStatus().toString();
+ return this.cacheStatistics;
}
/**
- * Return the stats for the region.
- * <p>
- * @return String
+ * @return the hitCountRam
+ */
+ public int getHitCountRam()
+ {
+ return hitCountRam;
+ }
+
+ /**
+ * @return the hitCountAux
+ */
+ public int getHitCountAux()
+ {
+ return hitCountAux;
+ }
+
+ /**
+ * @return the missCountNotFound
+ */
+ public int getMissCountNotFound()
+ {
+ return missCountNotFound;
+ }
+
+ /**
+ * @return the missCountExpired
+ */
+ public int getMissCountExpired()
+ {
+ return missCountExpired;
+ }
+
+ /**
+ * @return total byte count
*/
- public String getStats()
+ public long getByteCount()
{
- return this.cache.getStats();
+ return this.byteCount;
}
/**
@@ -74,10 +167,10 @@ public class CacheRegionInfo
{
StringBuffer buf = new StringBuffer();
buf.append( "\nCacheRegionInfo " );
- if ( getCache() != null )
+ if ( cacheName != null )
{
- buf.append( "\n CacheName [" + getCache().getCacheName() + "]" );
- buf.append( "\n Status [" + getStatus() + "]" );
+ buf.append( "\n CacheName [" + cacheName + "]" );
+ buf.append( "\n Status [" + cacheStatus + "]" );
}
buf.append( "\n ByteCount [" + getByteCount() + "]" );
Modified:
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/JCSAdmin.jsp
URL:
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/JCSAdmin.jsp?rev=1556494&r1=1556493&r2=1556494&view=diff
==============================================================================
--- commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/JCSAdmin.jsp
(original)
+++ commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/JCSAdmin.jsp
Wed Jan 8 11:16:37 2014
@@ -201,8 +201,7 @@
<th> Till Expiration (s) </th>
</tr>
<%
- @SuppressWarnings("unchecked")
- List<CacheElementInfo> list = (List<CacheElementInfo>) context.get(
"elementInfoRecords" );
+ CacheElementInfo[] list = (CacheElementInfo[]) context.get(
"elementInfoRecords" );
for (CacheElementInfo element : list)
{
%>
@@ -252,12 +251,11 @@ which empties the entire cache.
Retrieve (key) <input type="text" name="key">
(region) <select name="cacheName">
<%
- @SuppressWarnings("unchecked")
- List<CacheRegionInfo> listSelect = (List<CacheRegionInfo>) context.get(
"cacheInfoRecords" );
+ CacheRegionInfo[] listSelect = (CacheRegionInfo[]) context.get(
"cacheInfoRecords" );
for (CacheRegionInfo record : listSelect)
{
%>
- <option
value="<%=record.getCache().getCacheName()%>"><%=record.getCache().getCacheName()%></option>
+ <option
value="<%=record.getCacheName()%>"><%=record.getCacheName()%></option>
<%
}
%>
@@ -279,24 +277,23 @@ which empties the entire cache.
</tr>
<%
- @SuppressWarnings("unchecked")
- List<CacheRegionInfo> list = (List<CacheRegionInfo>) context.get(
"cacheInfoRecords" );
+ CacheRegionInfo[] list = (CacheRegionInfo[]) context.get(
"cacheInfoRecords" );
for (CacheRegionInfo record : listSelect)
{
%>
<tr>
- <td> <%=record.getCache().getCacheName()%> </td>
- <td> <%=record.getCache().getSize()%> </td>
+ <td> <%=record.getCacheName()%> </td>
+ <td> <%=record.getCacheSize()%> </td>
<td> <%=record.getByteCount()%> </td>
- <td> <%=record.getStatus()%> </td>
- <td> <%=record.getCache().getHitCountRam()%> </td>
- <td> <%=record.getCache().getHitCountAux()%> </td>
- <td> <%=record.getCache().getMissCountNotFound()%> </td>
- <td> <%=record.getCache().getMissCountExpired()%> </td>
+ <td> <%=record.getCacheStatus()%> </td>
+ <td> <%=record.getHitCountRam()%> </td>
+ <td> <%=record.getHitCountAux()%> </td>
+ <td> <%=record.getMissCountNotFound()%> </td>
+ <td> <%=record.getMissCountExpired()%> </td>
<td>
- <a
href="JCSAdmin.jsp?action=regionSummary&cacheName=<%=record.getCache().getCacheName()%>">
Summary </a>
- | <a
href="JCSAdmin.jsp?action=detail&cacheName=<%=record.getCache().getCacheName()%>">
Detail </a>
- | <a href="javascript:decision('Clicking OK will remove all
the data from the region
[<%=record.getCache().getCacheName()%>]!','JCSAdmin.jsp?action=clearRegion&cacheName=<%=record.getCache().getCacheName()%>')">
Clear </a>
+ <a
href="JCSAdmin.jsp?action=regionSummary&cacheName=<%=record.getCacheName()%>">
Summary </a>
+ | <a
href="JCSAdmin.jsp?action=detail&cacheName=<%=record.getCacheName()%>"> Detail
</a>
+ | <a href="javascript:decision('Clicking OK will remove all
the data from the region
[<%=record.getCacheName()%>]!','JCSAdmin.jsp?action=clearRegion&cacheName=<%=record.getCacheName()%>')">
Clear </a>
</td>
</tr>
<%
Modified:
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/JCSAdminBean.java
URL:
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/JCSAdminBean.java?rev=1556494&r1=1556493&r2=1556494&view=diff
==============================================================================
---
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/JCSAdminBean.java
(original)
+++
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/JCSAdminBean.java
Wed Jan 8 11:16:37 2014
@@ -25,9 +25,7 @@ import java.io.Serializable;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.Date;
-import java.util.Iterator;
import java.util.LinkedList;
-import java.util.Map;
import java.util.Set;
import org.apache.commons.jcs.access.exception.CacheException;
@@ -39,7 +37,6 @@ import org.apache.commons.jcs.engine.beh
import org.apache.commons.jcs.engine.control.CompositeCache;
import org.apache.commons.jcs.engine.control.CompositeCacheManager;
import org.apache.commons.jcs.engine.memory.behavior.IMemoryCache;
-import org.apache.commons.jcs.engine.memory.util.MemoryElementDescriptor;
/**
* A servlet which provides HTTP access to JCS. Allows a summary of regions to
be viewed, and
@@ -47,7 +44,7 @@ import org.apache.commons.jcs.engine.mem
* items (any number of key arguments can be provided with action 'remove').
Should be initialized
* with a properties file that provides at least a classpath resource loader.
*/
-public class JCSAdminBean
+public class JCSAdminBean implements JCSJMXBean
{
/** The cache manager. */
private final CompositeCacheManager cacheHub;
@@ -69,13 +66,24 @@ public class JCSAdminBean
}
/**
+ * Parameterized constructor
+ *
+ * @param cacheHub the cache manager instance
+ */
+ public JCSAdminBean(CompositeCacheManager cacheHub)
+ {
+ super();
+ this.cacheHub = cacheHub;
+ }
+
+ /**
* Builds up info about each element in a region.
* <p>
* @param cacheName
- * @return List of CacheElementInfo objects
+ * @return Array of CacheElementInfo objects
* @throws Exception
*/
- public LinkedList<CacheElementInfo> buildElementInfo( String cacheName )
+ public CacheElementInfo[] buildElementInfo( String cacheName )
throws Exception
{
CompositeCache<Serializable, Serializable> cache = cacheHub.getCache(
cacheName );
@@ -109,21 +117,17 @@ public class JCSAdminBean
attributes = element.getElementAttributes();
- elementInfo = new CacheElementInfo();
-
- elementInfo.key = String.valueOf( key );
- elementInfo.eternal = attributes.getIsEternal();
- elementInfo.maxLifeSeconds = attributes.getMaxLifeSeconds();
-
- elementInfo.createTime = format.format( new Date(
attributes.getCreateTime() ) );
-
- elementInfo.expiresInSeconds = ( now - attributes.getCreateTime()
- ( attributes.getMaxLifeSeconds() * 1000 ) )
- / -1000;
+ elementInfo = new CacheElementInfo(
+ String.valueOf( key ),
+ attributes.getIsEternal(),
+ format.format(new Date(attributes.getCreateTime())),
+ attributes.getMaxLifeSeconds(),
+ (now - attributes.getCreateTime() -
(attributes.getMaxLifeSeconds() * 1000 )) / -1000);
records.add( elementInfo );
}
- return records;
+ return records.toArray(new CacheElementInfo[0]);
}
/**
@@ -134,7 +138,7 @@ public class JCSAdminBean
* @return list of CacheRegionInfo objects
* @throws Exception
*/
- public LinkedList<CacheRegionInfo> buildCacheInfo()
+ public CacheRegionInfo[] buildCacheInfo()
throws Exception
{
String[] cacheNames = cacheHub.getCacheNames();
@@ -150,18 +154,37 @@ public class JCSAdminBean
{
cache = cacheHub.getCache( cacheNames[i] );
- regionInfo = new CacheRegionInfo();
-
- regionInfo.cache = cache;
- regionInfo.byteCount = getByteCount( cache );
+ regionInfo = new CacheRegionInfo(
+ cache.getCacheName(),
+ cache.getSize(),
+ cache.getStatus().toString(),
+ cache.getStats(),
+ cache.getHitCountRam(),
+ cache.getHitCountAux(),
+ cache.getMissCountNotFound(),
+ cache.getMissCountExpired(),
+ getByteCount( cache ));
cacheInfo.add( regionInfo );
}
- return cacheInfo;
+ return cacheInfo.toArray(new CacheRegionInfo[0]);
}
- /**
+
+ /**
+ * Tries to estimate how much data is in a region. This is expensive. If
there are any non serializable objects in
+ * the region or an error occurs, suppresses exceptions and returns 0.
+ * <p/>
+ *
+ * @return int The size of the region in bytes.
+ */
+ public int getByteCount(String cacheName)
+ {
+ return getByteCount(cacheHub.getCache(cacheName));
+ }
+
+ /**
* Tries to estimate how much data is in a region. This is expensive. If
there are any non serializable objects in
* the region or an error occurs, suppresses exceptions and returns 0.
* <p/>
@@ -178,13 +201,23 @@ public class JCSAdminBean
long size = 0;
IMemoryCache<K, V> memCache = cache.getMemoryCache();
- Iterator<Map.Entry<K, MemoryElementDescriptor<K, V>>> iter =
memCache.getIterator();
- while (iter.hasNext())
+ for (K key : memCache.getKeySet())
{
- MemoryElementDescriptor<K, V> me = iter.next().getValue();
- ICacheElement<K, V> ice = me.ce;
-
- if (ice instanceof CacheElementSerialized)
+ ICacheElement<K, V> ice = null;
+ try
+ {
+ ice = memCache.get(key);
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException("IOException while trying to get a
cached element", e);
+ }
+
+ if (ice == null)
+ {
+ continue;
+ }
+ else if (ice instanceof CacheElementSerialized)
{
size = size + ((CacheElementSerialized<K, V>)
ice).getSerializedValue().length;
}
@@ -194,15 +227,38 @@ public class JCSAdminBean
//CountingOnlyOutputStream: Keeps track of the number of bytes
written to it, but doesn't write them anywhere.
CountingOnlyOutputStream counter = new
CountingOnlyOutputStream();
+ ObjectOutputStream out = null;
try
{
- ObjectOutputStream out = new ObjectOutputStream(counter);
+ out = new ObjectOutputStream(counter);
out.writeObject(element);
}
catch (IOException e)
{
throw new RuntimeException("IOException while trying to
measure the size of the cached element", e);
}
+ finally
+ {
+ try
+ {
+ if (out != null)
+ {
+ out.close();
+ }
+ }
+ catch (IOException e)
+ {
+ // ignore
+ }
+ try
+ {
+ counter.close();
+ }
+ catch (IOException e)
+ {
+ // ignore
+ }
+ }
// 4 bytes lost for the serialization header
size = size + counter.getCount() - 4;
Added:
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/JCSJMXBean.java
URL:
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/JCSJMXBean.java?rev=1556494&view=auto
==============================================================================
---
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/JCSJMXBean.java
(added)
+++
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/JCSJMXBean.java
Wed Jan 8 11:16:37 2014
@@ -0,0 +1,91 @@
+package org.apache.commons.jcs.admin;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.IOException;
+
+import javax.management.MXBean;
+
+/**
+ * A MXBean to expose the JCS statistics to JMX
+ */
+@MXBean
+public interface JCSJMXBean
+{
+ /**
+ * Builds up info about each element in a region.
+ * <p>
+ * @param cacheName
+ * @return Array of CacheElementInfo objects
+ * @throws Exception
+ */
+ CacheElementInfo[] buildElementInfo( String cacheName ) throws Exception;
+
+ /**
+ * Builds up data on every region.
+ * <p>
+ * @TODO we need a most light weight method that does not count bytes. The
byte counting can
+ * really swamp a server.
+ * @return Array of CacheRegionInfo objects
+ * @throws Exception
+ */
+ CacheRegionInfo[] buildCacheInfo() throws Exception;
+
+ /**
+ * Tries to estimate how much data is in a region. This is expensive. If
there are any non serializable objects in
+ * the region or an error occurs, suppresses exceptions and returns 0.
+ * <p/>
+ *
+ * @return int The size of the region in bytes.
+ */
+ int getByteCount(String cacheName);
+
+ /**
+ * Clears all regions in the cache.
+ * <p/>
+ * If this class is running within a remote cache server, clears all
regions via the <code>RemoteCacheServer</code>
+ * API, so that removes will be broadcast to client machines. Otherwise
clears all regions in the cache directly via
+ * the usual cache API.
+ */
+ void clearAllRegions() throws IOException;
+
+ /**
+ * Clears a particular cache region.
+ * <p/>
+ * If this class is running within a remote cache server, clears the
region via the <code>RemoteCacheServer</code>
+ * API, so that removes will be broadcast to client machines. Otherwise
clears the region directly via the usual
+ * cache API.
+ */
+ void clearRegion(String cacheName) throws IOException;
+
+ /**
+ * Removes a particular item from a particular region.
+ * <p/>
+ * If this class is running within a remote cache server, removes the item
via the <code>RemoteCacheServer</code>
+ * API, so that removes will be broadcast to client machines. Otherwise
clears the region directly via the usual
+ * cache API.
+ *
+ * @param cacheName
+ * @param key
+ *
+ * @throws IOException
+ */
+ void removeItem(String cacheName, String key) throws IOException;
+}
Propchange:
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/admin/JCSJMXBean.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java
URL:
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java?rev=1556494&r1=1556493&r2=1556494&view=diff
==============================================================================
---
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java
(original)
+++
commons/proper/jcs/trunk/src/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java
Wed Jan 8 11:16:37 2014
@@ -22,6 +22,7 @@ package org.apache.commons.jcs.engine.co
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
+import java.lang.management.ManagementFactory;
import java.security.AccessControlException;
import java.util.ArrayList;
import java.util.HashSet;
@@ -33,7 +34,11 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
import org.apache.commons.jcs.access.exception.CacheException;
+import org.apache.commons.jcs.admin.JCSAdminBean;
import org.apache.commons.jcs.auxiliary.AuxiliaryCacheAttributes;
import org.apache.commons.jcs.auxiliary.AuxiliaryCacheFactory;
import org.apache.commons.jcs.auxiliary.remote.behavior.IRemoteCacheConstants;
@@ -41,13 +46,13 @@ import org.apache.commons.jcs.engine.Cac
import org.apache.commons.jcs.engine.CompositeCacheAttributes;
import org.apache.commons.jcs.engine.ElementAttributes;
import org.apache.commons.jcs.engine.behavior.ICache;
+import org.apache.commons.jcs.engine.behavior.ICacheType.CacheType;
import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes;
import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
import org.apache.commons.jcs.engine.behavior.IElementAttributes;
import org.apache.commons.jcs.engine.behavior.IProvideScheduler;
import org.apache.commons.jcs.engine.behavior.IShutdownObservable;
import org.apache.commons.jcs.engine.behavior.IShutdownObserver;
-import org.apache.commons.jcs.engine.behavior.ICacheType.CacheType;
import org.apache.commons.jcs.engine.stats.CacheStats;
import org.apache.commons.jcs.engine.stats.behavior.ICacheStats;
import org.apache.commons.jcs.utils.threadpool.ThreadPoolManager;
@@ -73,6 +78,9 @@ public class CompositeCacheManager
/** The logger */
protected final static Log log = LogFactory.getLog(
CompositeCacheManager.class );
+ /** JMX object name */
+ private final static String JMX_OBJECT_NAME =
"org.apache.commons.jcs:type=JCSAdminBean";
+
/** Caches managed by this cache manager */
protected Hashtable<String, ICache<? extends Serializable, ? extends
Serializable>> caches =
new Hashtable<String, ICache<? extends Serializable, ? extends
Serializable>>();
@@ -128,6 +136,9 @@ public class CompositeCacheManager
/** Indicates whether configure has been called. */
private boolean isConfigured = false;
+ /** Indicates whether JMX bean has been registered. */
+ private boolean isJMXRegistered = false;
+
/**
* Gets the CacheHub instance. For backward compatibility, if this creates
the instance it will
* attempt to configure it with the default configuration. If you want to
configure from your
@@ -237,6 +248,23 @@ public class CompositeCacheManager
return t;
}
});
+
+ // Register JMX bean
+ if (!isJMXRegistered)
+ {
+ MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+ JCSAdminBean adminBean = new JCSAdminBean(this);
+ try
+ {
+ ObjectName jmxObjectName = new
ObjectName(JMX_OBJECT_NAME);
+ mbs.registerMBean(adminBean, jmxObjectName);
+ isJMXRegistered = true;
+ }
+ catch (Exception e)
+ {
+ log.warn( "Could not register JMX bean.", e );
+ }
+ }
}
/**
@@ -614,6 +642,23 @@ public class CompositeCacheManager
}
}
+ // Unregister JMX bean
+ if (isJMXRegistered)
+ {
+ MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+ try
+ {
+ ObjectName jmxObjectName = new
ObjectName(JMX_OBJECT_NAME);
+ mbs.unregisterMBean(jmxObjectName);
+ }
+ catch (Exception e)
+ {
+ log.warn( "Could not unregister JMX bean.", e );
+ }
+
+ isJMXRegistered = false;
+ }
+
// do the traditional shutdown of the regions.
String[] names = getCacheNames();
int len = names.length;
@@ -676,13 +721,7 @@ public class CompositeCacheManager
*/
public String[] getCacheNames()
{
- String[] list = new String[caches.size()];
- int i = 0;
- for (String key : caches.keySet())
- {
- list[i++] = key;
- }
- return list;
+ return caches.keySet().toArray(new String[caches.size()]);
}
/**
Modified:
commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/admin/AdminBeanUnitTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/admin/AdminBeanUnitTest.java?rev=1556494&r1=1556493&r2=1556494&view=diff
==============================================================================
---
commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/admin/AdminBeanUnitTest.java
(original)
+++
commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/admin/AdminBeanUnitTest.java
Wed Jan 8 11:16:37 2014
@@ -19,8 +19,6 @@ package org.apache.commons.jcs.admin;
* under the License.
*/
-import java.util.List;
-
import junit.framework.TestCase;
import org.apache.commons.jcs.JCS;
@@ -52,7 +50,7 @@ public class AdminBeanUnitTest
JCSAdminBean admin = new JCSAdminBean();
- List<CacheRegionInfo> regions = admin.buildCacheInfo();
+ CacheRegionInfo[] regions = admin.buildCacheInfo();
boolean foundRegion = false;
@@ -60,13 +58,13 @@ public class AdminBeanUnitTest
{
System.out.println( info );
- if ( info.getCache().getCacheName().equals( regionName ) )
+ if ( info.getCacheName().equals( regionName ) )
{
foundRegion = true;
assertTrue( "Byte count should be greater than 5.",
info.getByteCount() > 5 );
- assertNotNull( "Should have stats.", info.getStats() );
+ assertNotNull( "Should have stats.", info.getCacheStatistics()
);
}
}
@@ -92,11 +90,11 @@ public class AdminBeanUnitTest
JCSAdminBean admin = new JCSAdminBean();
- List<CacheElementInfo> elements = admin.buildElementInfo( regionName );
+ CacheElementInfo[] elements = admin.buildElementInfo( regionName );
- assertEquals( "Wrong number of elements in the region.", 1,
elements.size() );
+ assertEquals( "Wrong number of elements in the region.", 1,
elements.length );
- CacheElementInfo elementInfo = elements.get( 0 );
+ CacheElementInfo elementInfo = elements[0];
System.out.println( elementInfo );
assertEquals( "Wrong key.", key, elementInfo.getKey() );
}
@@ -121,18 +119,18 @@ public class AdminBeanUnitTest
String key = "myKey";
cache.put( key, "value" );
- List<CacheElementInfo> elements = admin.buildElementInfo( regionName );
+ CacheElementInfo[] elements = admin.buildElementInfo( regionName );
- assertEquals( "Wrong number of elements in the region.", 1,
elements.size() );
+ assertEquals( "Wrong number of elements in the region.", 1,
elements.length );
- CacheElementInfo elementInfo = elements.get( 0 );
+ CacheElementInfo elementInfo = elements[0];
assertEquals( "Wrong key.", key, elementInfo.getKey() );
admin.removeItem( regionName, key );
- List<CacheElementInfo> elements2 = admin.buildElementInfo( regionName
);
- assertEquals( "Wrong number of elements in the region after remove.",
0, elements2.size() );
+ CacheElementInfo[] elements2 = admin.buildElementInfo( regionName );
+ assertEquals( "Wrong number of elements in the region after remove.",
0, elements2.length );
}
/**
@@ -153,7 +151,7 @@ public class AdminBeanUnitTest
admin.clearAllRegions();
- List<CacheElementInfo> elements2 = admin.buildElementInfo( regionName
);
- assertEquals( "Wrong number of elements in the region after remove.",
0, elements2.size() );
+ CacheElementInfo[] elements2 = admin.buildElementInfo( regionName );
+ assertEquals( "Wrong number of elements in the region after remove.",
0, elements2.length );
}
}
Added:
commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/admin/TestJMX.java
URL:
http://svn.apache.org/viewvc/commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/admin/TestJMX.java?rev=1556494&view=auto
==============================================================================
--- commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/admin/TestJMX.java
(added)
+++ commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/admin/TestJMX.java
Wed Jan 8 11:16:37 2014
@@ -0,0 +1,38 @@
+package org.apache.commons.jcs.admin;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.commons.jcs.JCS;
+import org.apache.commons.jcs.access.CacheAccess;
+
+/**
+ * Helper class to test the JMX registration
+ */
+public class TestJMX
+{
+ public static void main(String[] args) throws Exception
+ {
+ CacheAccess<String, String> cache = JCS.getInstance("test");
+
+ cache.put("key", "value");
+ System.out.println("Waiting...");
+ Thread.sleep(Long.MAX_VALUE);
+ }
+}
Propchange:
commons/proper/jcs/trunk/src/test/org/apache/commons/jcs/admin/TestJMX.java
------------------------------------------------------------------------------
svn:mime-type = text/plain