Author: asmuts
Date: Wed May  3 08:20:49 2006
New Revision: 399331

URL: http://svn.apache.org/viewcvs?rev=399331&view=rev
Log:
Added a startup servlet for the remote cache.  This makes starting and stopping 
the remote cache much easier.

Added:
    
jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java
Modified:
    
jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/disk/jdbc/ShrinkerThread.java

Modified: 
jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/disk/jdbc/ShrinkerThread.java
URL: 
http://svn.apache.org/viewcvs/jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/disk/jdbc/ShrinkerThread.java?rev=399331&r1=399330&r2=399331&view=diff
==============================================================================
--- 
jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/disk/jdbc/ShrinkerThread.java
 (original)
+++ 
jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/disk/jdbc/ShrinkerThread.java
 Wed May  3 08:20:49 2006
@@ -9,7 +9,7 @@
 
 /**
  * Calls delete expired on the disk caches. The shrinker is run by a clock
- * daemon.
+ * daemon. The shrinker calls delete on each region. It pauses between calls.
  * 
  * @author Aaron Smuts
  * 
@@ -19,9 +19,20 @@
 {
     private final static Log log = LogFactory.getLog( ShrinkerThread.class );
 
+    /** A set of JDBCDiskCache objects to call deleteExpired on. */
     private Set shrinkSet = Collections.synchronizedSet( new HashSet() );
 
     /**
+     * How long should we wait between calls to deleteExpired when we are
+     * iterating through the list of regions. Delete can lock the table. We 
want
+     * to give clients a chance to get some work done.
+     */
+    private static final long DEFAULT_PAUSE_BETWEEN_REGION_CALLS_MILLIS = 5000;
+
+    private long pauseBetweenRegionCallsMillis = 
DEFAULT_PAUSE_BETWEEN_REGION_CALLS_MILLIS;
+
+    /**
+     * Does nothing special.
      * 
      * @param diskCache
      */
@@ -45,7 +56,7 @@
     }
 
     /**
-     * Calls deleteExpired on each item in the set.
+     * Calls deleteExpired on each item in the set. It pauses between each 
call.
      */
     public void run()
     {
@@ -72,7 +83,49 @@
                 {
                     log.info( "Deleted [" + deleted + "] expired for region [" 
+ cache.getCacheName() + "]" );
                 }
+
+                // don't pause after the last call to delete expired.
+                if ( i < caches.length - 1 )
+                {
+                    if ( log.isInfoEnabled() )
+                    {
+                        log.info( "Pausing for [" + 
this.getPauseBetweenRegionCallsMillis()
+                            + "] ms. before shinker the next region." );
+                    }
+
+                    try
+                    {
+                        Thread.sleep( this.getPauseBetweenRegionCallsMillis() 
);
+                    }
+                    catch ( InterruptedException e )
+                    {
+                        log.warn( "Interrupted while waiting to delete expired 
for the enxt region." );
+                    }
+                }
             }
         }
+    }
+
+    /**
+     * How long should we wait between calls to deleteExpired when we are
+     * iterating through the list of regions.
+     * 
+     * @param pauseBetweenRegionCallsMillis
+     *            The pauseBetweenRegionCallsMillis to set.
+     */
+    public void setPauseBetweenRegionCallsMillis( long 
pauseBetweenRegionCallsMillis )
+    {
+        this.pauseBetweenRegionCallsMillis = pauseBetweenRegionCallsMillis;
+    }
+
+    /**
+     * How long should we wait between calls to deleteExpired when we are
+     * iterating through the list of regions.
+     * 
+     * @return Returns the pauseBetweenRegionCallsMillis.
+     */
+    public long getPauseBetweenRegionCallsMillis()
+    {
+        return pauseBetweenRegionCallsMillis;
     }
 }

Added: 
jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java
URL: 
http://svn.apache.org/viewcvs/jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java?rev=399331&view=auto
==============================================================================
--- 
jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java
 (added)
+++ 
jakarta/jcs/trunk/src/java/org/apache/jcs/auxiliary/remote/server/RemoteCacheStartupServlet.java
 Wed May  3 08:20:49 2006
@@ -0,0 +1,187 @@
+package org.apache.jcs.auxiliary.remote.server;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.rmi.RemoteException;
+import java.rmi.registry.LocateRegistry;
+import java.util.Properties;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.jcs.engine.control.CompositeCacheManager;
+import org.apache.jcs.utils.props.PropertyLoader;
+
+/**
+ * This servlet can be used to startup the JCS remote cache. It is easy to
+ * deploy the remote server in a tomcat base. This give you an easy way to
+ * monitor its activity.
+ * <p>
+ * 
+ * <code>
+ *  <servlet>
+        <servlet-name>JCSRemoteCacheStartupServlet</servlet-name>
+        <servlet-class>
+            com.travelocity.lmd.jcs.JCSRemoteCacheStartupServlet
+        </servlet-class>
+        <load-on-startup>1</load-on-startup>
+    </servlet>
+    
+    
+    <servlet-mapping>
+        <servlet-name>JCSRemoteCacheStartupServlet</servlet-name>
+        <url-pattern>/jcs</url-pattern>
+    </servlet-mapping>
+ * </code>
+ * 
+ * 
+ * @author Aaron Smuts
+ * 
+ */
+public class RemoteCacheStartupServlet
+    extends HttpServlet
+{
+    private static final long serialVersionUID = 1L;
+
+    private final static Log log = LogFactory.getLog( 
RemoteCacheStartupServlet.class );
+
+    private static final int DEFAULT_REGISTRY_PORT = 1101;
+
+    private static final String DEFAULT_PROPS_FILE_NAME = "cache.ccf";
+
+    /**
+     * Starts the registry and then tries to bind to it.
+     * <p>
+     * Gets the port from a props file. Uses the local host name for the 
rgistry
+     * host. Tries to start the registry, ignoreing failure. Starts the server.
+     * 
+     */
+    public void init()
+        throws ServletException
+    {
+        super.init();
+        // TODO load from props file or get as init param or get from jndi, or
+        // all three
+        int registryPort = DEFAULT_REGISTRY_PORT;
+
+        try
+        {
+            Properties props = PropertyLoader.loadProperties( 
DEFAULT_PROPS_FILE_NAME );
+            if ( props != null )
+            {
+                String portS = props.getProperty( "registry.port", 
String.valueOf( DEFAULT_REGISTRY_PORT ) );
+
+                try
+                {
+                    registryPort = Integer.parseInt( portS );
+                }
+                catch ( NumberFormatException e )
+                {
+                    log.error( "Problem converting port to an int.", e );
+                }
+            }
+        }
+        catch ( Exception e )
+        {
+            log.error( "Problem loading props.", e );
+        }
+        catch ( Throwable t )
+        {
+            log.error( "Problem loading props.", t );
+        }
+
+        // we will always use the local machine for the registry
+        String registryHost;
+        try
+        {
+            registryHost = InetAddress.getLocalHost().getHostAddress();
+
+            if ( log.isDebugEnabled() )
+            {
+                log.debug( "registryHost = [" + registryHost + "]" );
+            }
+
+            if ( "localhost".equals( registryHost ) || "127.0.0.1".equals( 
registryHost ) )
+            {
+                log.warn( "The local address [" + registryHost
+                    + "] is INVALID.  Other machines must be able to use the 
address to reach this server." );
+            }
+
+            try
+            {
+                LocateRegistry.createRegistry( registryPort );
+            }
+            catch ( RemoteException e )
+            {
+                log.error( "Problem creating registry.  It may already be 
started. " + e.getMessage() );
+            }
+            catch ( Throwable t )
+            {
+                log.error( "Problem creating registry.", t );
+            }
+
+            try
+            {
+                RemoteCacheServerFactory.startup( registryHost, registryPort, 
"/" + DEFAULT_PROPS_FILE_NAME );
+            }
+            catch ( IOException e )
+            {
+                log.error( "Problem starting remote cache server.", e );
+            }
+
+            catch ( Throwable t )
+            {
+                log.error( "Problem starting remote cache server.", t );
+            }
+        }
+        catch ( UnknownHostException e )
+        {
+            log.error( "Could not get local address to use for the registry!", 
e );
+        }
+    }
+
+    /**
+     * It just dumps the stats.
+     */
+    protected void service( HttpServletRequest request, HttpServletResponse 
response )
+        throws ServletException, IOException
+    {
+
+        String stats = CompositeCacheManager.getInstance().getStats();
+        if ( log.isInfoEnabled() )
+        {
+            log.info( stats );
+        }
+
+        try
+        {
+            OutputStream os = response.getOutputStream();
+            os.write( stats.getBytes() );
+            os.close();
+        }
+        catch ( IOException e )
+        {
+            log.error( "Problem writing response.", e );
+        }
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.servlet.Servlet#destroy()
+     */
+    public void destroy()
+    {
+        super.destroy();
+
+        log.info( "Shutting down remote cache " );
+
+        CompositeCacheManager.getInstance().shutDown();
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to