jakarta-jcs-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Smuts, Aaron" <Aaron.Sm...@travelocity.com>
Subject RE: Remote Cache
Date Wed, 03 May 2006 14:41:56 GMT
If you want to start the remote cache up in tomcat and deploy it as a
war, as I do, then you can use the servlet below.  I run the remote
cache inside of a tomcat base so I can administer it just like any other
web app.  (I run dozens of different services inside different tomcat
instances.)

----------------------------

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.auxiliary.remote.server.RemoteCacheServerFactory;
import org.apache.jcs.engine.control.CompositeCacheManager;
import org.apache.jcs.utils.props.PropertyLoader;

/**
 * This will be the JCS startup servlet. I just created it as a place
hodler for
 * now. I will probably move into JCS itself and then just reference it.
I want
 * to add health monitoring.
 * 
 * @author Aaron Smuts
 * 
 */
public class JCSRemoteCacheStartupServlet
    extends HttpServlet
{

    private static final long serialVersionUID = 1L;

    private final static Log log = LogFactory.getLog(
JCSRemoteCacheStartupServlet.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
            {
                // RemoteUtils.createRegistry( registryPort );

                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();
    }

}



----------------------------

> -----Original Message-----
> From: Alistair Forbes [mailto:forbes.al@googlemail.com]
> Sent: Wednesday, May 03, 2006 10:31 AM
> To: JCS Users List
> Subject: Re: Remote Cache
> 
> The script is in subversion:
> 
>
http://svn.apache.org/viewcvs.cgi/jakarta/jcs/trunk/src/scripts/startRem
ot
> eCache.sh?rev=224336&view=markup
> 
> The script does not do very much, so you could just as well roll your
own:
> 
> java $PROPS -cp $CLASSPATH
>
org.apache.jcs.auxiliary.remote.server.RemoteCacheServerFactory/remote.c
ac
> he.ccf
> > /dev/null 2>&1
> 
> Regards
> Al
> 
> On 5/3/06, adam.schwartz-lowe@thomson.com
<adam.schwartz-lowe@thomson.com>
> wrote:
> >
> > Where do I get ahold of startRemoteCache.sh?  I haven't been able to
> > find a download for jcs other than the jcs jar.
> >
> > thank you,
> > Adam
> >
> >

---------------------------------------------------------------------
To unsubscribe, e-mail: jcs-users-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jcs-users-help@jakarta.apache.org


Mime
View raw message