You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jcs-dev@jakarta.apache.org by as...@apache.org on 2005/01/06 02:26:38 UTC
cvs commit: jakarta-turbine-jcs/src/test/org/apache/jcs/servlet SessionExampleServlet.java
asmuts 2005/01/05 17:26:38
Modified: src/java/org/apache/jcs/auxiliary/remote RemoteCache.java
RemoteCacheAttributes.java RemoteCacheNoWait.java
RemoteCacheNoWaitFacade.java
src/test/org/apache/jcs/auxiliary/remote
RemoteCacheClientTest.java
src/test/org/apache/jcs/servlet SessionExampleServlet.java
Log:
Put in a solution for the RMI bug. Now, gets can be done asynchronously with a configurable timeout.
You can specify that your remote region should timeout gets.
see the cache.ccf in the src/conf for details
Revision Changes Path
1.12 +142 -2 jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/remote/RemoteCache.java
Index: RemoteCache.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/remote/RemoteCache.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- RemoteCache.java 12 Jun 2004 02:33:41 -0000 1.11
+++ RemoteCache.java 6 Jan 2005 01:26:38 -0000 1.12
@@ -20,7 +20,11 @@
import java.io.IOException;
import java.io.Serializable;
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
import java.util.Set;
import org.apache.jcs.access.exception.ObjectNotFoundException;
@@ -38,6 +42,15 @@
import org.apache.commons.logging.LogFactory;
import org.apache.jcs.engine.behavior.IZombie;
+import org.apache.jcs.engine.stats.StatElement;
+import org.apache.jcs.engine.stats.Stats;
+import org.apache.jcs.engine.stats.behavior.IStatElement;
+import org.apache.jcs.engine.stats.behavior.IStats;
+import org.apache.jcs.utils.threadpool.ThreadPoolManager;
+
+import EDU.oswego.cs.dl.util.concurrent.Callable;
+import EDU.oswego.cs.dl.util.concurrent.FutureResult;
+import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
/**
* Client proxy for an RMI remote cache.
@@ -54,6 +67,9 @@
IElementAttributes attr = null;
+ private PooledExecutor pool = null;
+ private boolean usePoolForGet = false;
+
/** Description of the Method */
public String toString()
{
@@ -79,6 +95,25 @@
log.debug( "Construct> cacheName=" + cattr.getCacheName() );
log.debug( "irca = " + irca.toString() );
}
+
+ // use a pool if it is greater than 0
+ if ( log.isDebugEnabled() )
+ {
+ log.debug( "GetTimeoutMillis() = " + irca.getGetTimeoutMillis() );
+ }
+ if ( irca.getGetTimeoutMillis() > 0 )
+ {
+ pool = ThreadPoolManager.getInstance().getPool( irca.getThreadPoolName() );
+ if ( log.isDebugEnabled() )
+ {
+ log.debug( "Thread Pool = " + pool );
+ }
+ if ( pool != null )
+ {
+ usePoolForGet = true;
+ }
+ }
+
/*
* TODO
* should be done by the remote cache, not the job of the hub manager
@@ -154,13 +189,22 @@
/**
* Synchronously get from the remote cache; if failed, replace the remote
* handle with a zombie.
+ *
+ * Use threadpool to timeout is a value is set for GetTimeoutMillis
*/
public ICacheElement get( Serializable key )
throws IOException
{
try
{
- return remote.get( cacheName, sanitized( key ) );
+ if ( usePoolForGet )
+ {
+ return getUsingPool( key );
+ }
+ else
+ {
+ return remote.get( cacheName, sanitized( key ) );
+ }
}
catch ( ObjectNotFoundException one )
{
@@ -176,6 +220,62 @@
}
}
+
+ /**
+ * This allows gets to timeout in case of remote server machine shutdown.
+ *
+ * @param key
+ * @return
+ * @throws IOException
+ */
+ public ICacheElement getUsingPool( final Serializable key )
+ throws IOException
+ {
+ int timeout = irca.getGetTimeoutMillis();
+
+ try
+ {
+ FutureResult future = new FutureResult();
+ Runnable command = future.setter( new Callable()
+ {
+ public Object call() throws IOException
+ {
+ try
+ {
+ return remote.get( cacheName, key );
+ }
+ catch (ObjectNotFoundException onf)
+ {
+ if ( log.isDebugEnabled() )
+ {
+ log.debug( "getusingPool, Didin't find object" );
+ }
+ return null;
+ }
+ }
+ } );
+
+ // execute using the pool
+ pool.execute( command );
+
+ // used timed get in order to timeout
+ future.timedGet( timeout );
+ }
+ catch (InterruptedException ex)
+ {
+ log.warn( "Get Request timed out after " + timeout );
+ throw new IOException( "Get Request timed out after " + timeout );
+ }
+ catch (InvocationTargetException ex)
+ {
+ // assume that this is an IOException thrown by the callable.
+ log.error( "Assuming an IO exception thrown in the backfground.", ex );
+ throw new IOException( "Get Request timed out after " + timeout );
+ }
+
+ return null;
+ }
+
public Set getGroupKeys(String groupName) throws java.rmi.RemoteException
{
return remote.getGroupKeys(cacheName, groupName);
@@ -303,9 +403,49 @@
*/
public String getStats()
{
- return "cacheName = " + cacheName;
+ return getStatistics().toString();
}
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.jcs.auxiliary.AuxiliaryCache#getStatistics()
+ */
+ public IStats getStatistics()
+ {
+ IStats stats = new Stats();
+ stats.setTypeName( "Remote Cache No Wait" );
+
+ ArrayList elems = new ArrayList();
+
+ IStatElement se = null;
+
+ // no data gathered here
+ se = new StatElement();
+ se.setName( "UsePoolForGet" );
+ se.setData( "" + usePoolForGet );
+ elems.add( se );
+
+ if ( pool != null )
+ {
+ se = new StatElement();
+ se.setName( "Pool Size" );
+ se.setData("" + pool.getPoolSize() );
+ elems.add(se);
+
+ se = new StatElement();
+ se.setName( "Maximum Pool Size" );
+ se.setData("" + pool.getMaximumPoolSize() );
+ elems.add(se);
+ }
+
+ // get an array and put them in the Stats object
+ IStatElement[] ses = (IStatElement[]) elems.toArray( new StatElement[0] );
+ stats.setStatElements( ses );
+
+ return stats;
+ }
/**
* Returns the current cache size.
*
1.6 +41 -0 jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/remote/RemoteCacheAttributes.java
Index: RemoteCacheAttributes.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/remote/RemoteCacheAttributes.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- RemoteCacheAttributes.java 12 Jun 2004 02:33:26 -0000 1.5
+++ RemoteCacheAttributes.java 6 Jan 2005 01:26:38 -0000 1.6
@@ -58,6 +58,11 @@
private boolean localClusterConsistency = false;
+ // default name is remote_cache_client
+ private String threadPoolName = "remote_cache_client";
+ // must be greater than 0 for a pool to be used.
+ private int getTimeoutMillis = -1;
+
/** Constructor for the RemoteCacheAttributes object */
public RemoteCacheAttributes() { }
@@ -420,6 +425,23 @@
this.localClusterConsistency = r;
}
+ /* (non-Javadoc)
+ * @see org.apache.jcs.auxiliary.remote.behavior.IRemoteCacheAttributes#getThreadPoolName()
+ */
+ public String getThreadPoolName()
+ {
+ return threadPoolName;
+ }
+
+
+ /* (non-Javadoc)
+ * @see org.apache.jcs.auxiliary.remote.behavior.IRemoteCacheAttributes#setThreadPoolName(java.lang.String)
+ */
+ public void setThreadPoolName( String name )
+ {
+ threadPoolName = name;
+ }
+
/** Description of the Method */
public String toString()
{
@@ -432,4 +454,23 @@
return buf.toString();
}
+
+ /* (non-Javadoc)
+ * @see org.apache.jcs.auxiliary.remote.behavior.IRemoteCacheAttributes#getGetTimeoutMillis()
+ */
+ public int getGetTimeoutMillis()
+ {
+ return getTimeoutMillis;
+ }
+
+
+ /* (non-Javadoc)
+ * @see org.apache.jcs.auxiliary.remote.behavior.IRemoteCacheAttributes#setGetTimeoutMillis(int)
+ */
+ public void setGetTimeoutMillis( int millis )
+ {
+ getTimeoutMillis = millis;
+ }
+
+
}
1.11 +45 -1 jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/remote/RemoteCacheNoWait.java
Index: RemoteCacheNoWait.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/remote/RemoteCacheNoWait.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- RemoteCacheNoWait.java 16 Jul 2004 01:27:52 -0000 1.10
+++ RemoteCacheNoWait.java 6 Jan 2005 01:26:38 -0000 1.11
@@ -20,6 +20,9 @@
import java.io.IOException;
import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
import java.util.Set;
import java.rmi.UnmarshalException;
@@ -33,6 +36,10 @@
import org.apache.jcs.engine.behavior.ICacheElement;
import org.apache.jcs.engine.behavior.ICacheEventQueue;
import org.apache.jcs.engine.behavior.ICacheType;
+import org.apache.jcs.engine.stats.StatElement;
+import org.apache.jcs.engine.stats.Stats;
+import org.apache.jcs.engine.stats.behavior.IStatElement;
+import org.apache.jcs.engine.stats.behavior.IStats;
/**
* Used to queue up update requests to the underlying cache. These requests will
@@ -241,6 +248,43 @@
*/
public String getStats()
{
- return "";
+ return getStatistics().toString();
}
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.jcs.auxiliary.AuxiliaryCache#getStatistics()
+ */
+ public IStats getStatistics()
+ {
+ IStats stats = new Stats();
+ stats.setTypeName( "Remote Cache No Wait" );
+
+ ArrayList elems = new ArrayList();
+
+ IStatElement se = null;
+
+ // no data gathered here
+
+ // get the stats from the cache queue too
+ // get as array, convert to list, add list to our outer list
+ IStats cStats = this.cache.getStatistics();
+ IStatElement[] cSEs = cStats.getStatElements();
+ List cL = Arrays.asList(cSEs);
+ elems.addAll( cL );
+
+ // get the stats from the event queue too
+ // get as array, convert to list, add list to our outer list
+ IStats eqStats = this.q.getStatistics();
+ IStatElement[] eqSEs = eqStats.getStatElements();
+ List eqL = Arrays.asList(eqSEs);
+ elems.addAll( eqL );
+
+ // get an array and put them in the Stats object
+ IStatElement[] ses = (IStatElement[]) elems.toArray( new StatElement[0] );
+ stats.setStatElements( ses );
+
+ return stats;
+ }
}
1.10 +48 -1 jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/remote/RemoteCacheNoWaitFacade.java
Index: RemoteCacheNoWaitFacade.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/remote/RemoteCacheNoWaitFacade.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- RemoteCacheNoWaitFacade.java 16 Jul 2004 01:27:52 -0000 1.9
+++ RemoteCacheNoWaitFacade.java 6 Jan 2005 01:26:38 -0000 1.10
@@ -20,6 +20,9 @@
import java.io.IOException;
import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
import java.util.Set;
import java.util.HashSet;
@@ -29,6 +32,10 @@
import org.apache.jcs.engine.CacheConstants;
import org.apache.jcs.engine.behavior.ICacheElement;
import org.apache.jcs.engine.behavior.ICacheType;
+import org.apache.jcs.engine.stats.StatElement;
+import org.apache.jcs.engine.stats.Stats;
+import org.apache.jcs.engine.stats.behavior.IStatElement;
+import org.apache.jcs.engine.stats.behavior.IStats;
/**
* Used to provide access to multiple services under nowait protection. factory
@@ -302,7 +309,47 @@
*/
public String getStats()
{
- return "";
+ return getStatistics().toString();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.jcs.auxiliary.AuxiliaryCache#getStatistics()
+ */
+ public IStats getStatistics()
+ {
+ IStats stats = new Stats();
+ stats.setTypeName( "Remote Cache No Wait Facade" );
+
+ ArrayList elems = new ArrayList();
+
+ IStatElement se = null;
+
+ if ( noWaits != null )
+ {
+ se = new StatElement();
+ se.setName( "Number of No Waits" );
+ se.setData( "" + noWaits.length );
+ elems.add( se );
+
+ for ( int i = 0; i < noWaits.length; i++ )
+ {
+ // get the stats from the super too
+ // get as array, convert to list, add list to our outer list
+ IStats sStats = noWaits[i].getStatistics();
+ IStatElement[] sSEs = sStats.getStatElements();
+ List sL = Arrays.asList( sSEs );
+ elems.addAll( sL );
+ }
+
+ }
+
+ // get an array and put them in the Stats object
+ IStatElement[] ses = (IStatElement[]) elems.toArray( new StatElement[0] );
+ stats.setStatElements( ses );
+
+ return stats;
}
}
1.4 +2 -9 jakarta-turbine-jcs/src/test/org/apache/jcs/auxiliary/remote/RemoteCacheClientTest.java
Index: RemoteCacheClientTest.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-jcs/src/test/org/apache/jcs/auxiliary/remote/RemoteCacheClientTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- RemoteCacheClientTest.java 9 Jun 2004 04:12:03 -0000 1.3
+++ RemoteCacheClientTest.java 6 Jan 2005 01:26:38 -0000 1.4
@@ -18,25 +18,18 @@
import java.io.IOException;
import java.io.Serializable;
-
import java.net.MalformedURLException;
-
import java.rmi.Naming;
import java.rmi.NotBoundException;
-import java.rmi.Remote;
import java.rmi.registry.Registry;
-
import java.rmi.server.ExportException;
import java.rmi.server.UnicastRemoteObject;
import org.apache.jcs.access.exception.ObjectExistsException;
import org.apache.jcs.access.exception.ObjectNotFoundException;
-
import org.apache.jcs.auxiliary.remote.behavior.IRemoteCacheConstants;
import org.apache.jcs.auxiliary.remote.behavior.IRemoteCacheListener;
-
import org.apache.jcs.engine.CacheElement;
-
import org.apache.jcs.engine.behavior.ICacheElement;
import org.apache.jcs.engine.behavior.ICacheObserver;
import org.apache.jcs.engine.behavior.ICacheService;
@@ -287,7 +280,7 @@
public void setListenerId( long id )
throws IOException
{
- this.listenerId = id;
+ listenerId = id;
p( "listenerId = " + id );
}
@@ -300,7 +293,7 @@
public long getListenerId()
throws IOException
{
- return this.listenerId;
+ return listenerId;
}
/** Helper for output, this is an user run test class */
1.3 +11 -7 jakarta-turbine-jcs/src/test/org/apache/jcs/servlet/SessionExampleServlet.java
Index: SessionExampleServlet.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-jcs/src/test/org/apache/jcs/servlet/SessionExampleServlet.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SessionExampleServlet.java 15 Apr 2004 19:24:19 -0000 1.2
+++ SessionExampleServlet.java 6 Jan 2005 01:26:38 -0000 1.3
@@ -17,17 +17,21 @@
* limitations under the License.
*/
-import java.io.*;
-import java.text.*;
-import java.util.*;
-import javax.servlet.*;
-import javax.servlet.http.*;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.Date;
+import java.util.Enumeration;
+import java.util.ResourceBundle;
-// JCS Session Implementation
-import org.apache.jcs.utils.servlet.session.HttpServletRequestFacade;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.apache.jcs.utils.servlet.session.HttpServletRequestFacade;
/**
* Session example using the cache for session replicaiton. Modifed from Tomcat
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-jcs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-jcs-dev-help@jakarta.apache.org