FileDocCategorySizeDatePackage
Shm.javaAPI DocApache Tomcat 6.0.149903Fri Jul 20 04:20:34 BST 2007org.apache.jk.common

Shm

public class Shm extends JniHandler
Handle the shared memory objects.
author
Costin Manolache

Fields Summary
String
file
int
size
String
host
int
port
String
unixSocket
boolean
help
boolean
unregister
boolean
reset
String
dumpFile
Vector
groups
static final int
SHM_WRITE_SLOT
static final int
SHM_RESET
static final int
SHM_DUMP
private static org.apache.juli.logging.Log
log
Constructors Summary
public Shm()

    
      
    
Methods Summary
public voiddestroy()

        destroyJkComponent();
    
public voiddumpScoreboard(java.lang.String fname)

        if( apr==null ) return;
        MsgContext mCtx=createMsgContext();
        Msg msg=(Msg)mCtx.getMsg(0);
        C2BConverter c2b=mCtx.getConverter();
        msg.reset();

        msg.appendByte( SHM_DUMP );

        appendString( msg, fname, c2b);
        
        this.invoke( msg, mCtx );
    
public voidexecute()

        try {
            if( help ) return;
            initCli();
            init();

            if( reset ) {
                resetScoreboard();
            } else if( dumpFile!=null ) {
                dumpScoreboard(dumpFile);
            } else if( unregister ) {
                unRegisterTomcat( host, port );
            } else {
                registerTomcat( host, port, unixSocket );
            }
        } catch (Exception ex ) {
            log.error( "Error executing Shm", ex);
        }
    
public voidinit()

        super.initNative( "shm" );
        if( apr==null ) return;
        if( file==null ) {
            log.error("No shm file, disabling shared memory");
            apr=null;
            return;
        }

        // Set properties and call init.
        setNativeAttribute( "file", file );
        if( size > 0 )
            setNativeAttribute( "size", Integer.toString( size ) );
        
        initJkComponent();
    
public voidinitCli()
Local initialization - for standalone use


    
    //-------------------- Main - use the shm functions from ant or CLI ------

               
         
        WorkerEnv wEnv=new WorkerEnv();
        AprImpl apr=new AprImpl();
        wEnv.addHandler( "apr", apr );
        wEnv.addHandler( "shm", this );
        apr.init();
        if( ! apr.isLoaded() ) {
            log.error( "No native support. " +
                       "Make sure libapr.so and libjkjni.so are available in LD_LIBRARY_PATH");
            return;
        }
    
public intinvoke(org.apache.jk.core.Msg msg, org.apache.jk.core.MsgContext ep)

        if( apr==null ) return 0;
        log.debug("ChannelShm.invoke: "  + ep );
        super.nativeDispatch( msg, ep, JK_HANDLE_SHM_DISPATCH, 0 );
        return 0;
    
public static voidmain(java.lang.String[] args)

        try {
            Shm shm=new Shm();

            if( args.length == 0 ||
                ( "-?".equals(args[0]) ) ) {
                shm.setHelp( true );
                return;
            }

            IntrospectionUtils.processArgs( shm, args);
            shm.execute();
        } catch( Exception ex ) {
            ex.printStackTrace();
        }
    
public voidregisterTomcat(java.lang.String host, int port, java.lang.String unixDomain)
Register a tomcat instance XXX make it more flexible

        String instanceId=host+":" + port;

        String slotName="TOMCAT:" + instanceId;
        MsgContext mCtx=createMsgContext();
        Msg msg=(Msg)mCtx.getMsg(0);
        msg.reset();
        C2BConverter c2b=mCtx.getConverter();
        
        msg.appendByte( SHM_WRITE_SLOT );
        appendString( msg, slotName, c2b );

        int channelCnt=1;
        if( unixDomain != null ) channelCnt++;

        // number of groups. 0 means the default lb.
        msg.appendInt( groups.size() );
        for( int i=0; i<groups.size(); i++ ) {
            appendString( msg, (String)groups.elementAt( i ), c2b);
            appendString( msg, instanceId, c2b);
        }
        
        // number of channels for this instance
        msg.appendInt( channelCnt );
        
        // The body:
        appendString(msg, "channel.socket:" + host + ":" + port, c2b );
        msg.appendInt( 1 );
        appendString(msg, "tomcatId", c2b);
        appendString(msg, instanceId, c2b);

        if( unixDomain != null ) {
            appendString(msg, "channel.apr:" + unixDomain, c2b );
            msg.appendInt(1);
            appendString(msg, "tomcatId", c2b);
            appendString(msg, instanceId, c2b);
        }

        if (log.isDebugEnabled())
            log.debug("Register " + instanceId );
        this.invoke( msg, mCtx );
    
public voidresetScoreboard()

        if( apr==null ) return;
        MsgContext mCtx=createMsgContext();
        Msg msg=(Msg)mCtx.getMsg(0);
        msg.reset();

        msg.appendByte( SHM_RESET );
        
        this.invoke( msg, mCtx );
    
public voidsetDump(java.lang.String dumpFile)
Copy the scoreboard in a file for debugging Will also log a lot of information about what's in the scoreboard.

        this.dumpFile=dumpFile;
    
public voidsetFile(java.lang.String f)
Scoreboard location

        file=f;
    
public voidsetGroup(java.lang.String grp)
Mark this instance as belonging to a group

        groups.addElement( grp );
    
public voidsetHelp(boolean b)

        if (log.isDebugEnabled()) {
            log.debug("Usage: ");
            log.debug("  Shm [OPTIONS]");
            log.debug("");
            log.debug("  -file SHM_FILE");
            log.debug("  -group GROUP ( can be specified multiple times )");
            log.debug("  -host HOST");
            log.debug("  -port PORT");
            log.debug("  -unixSocket UNIX_FILE");
            //        log.debug("  -priority XXX");
            //        log.debug("  -lbFactor XXX");
        }
        help=true;
        return;
    
public voidsetHost(java.lang.String host)
Ajp13 host

        this.host=host;
    
public voidsetPort(int port)
Ajp13 port

        this.port=port;
    
public voidsetReset(boolean b)
Set this to get the scoreboard reset. The shm segment will be destroyed and a new one created, with the provided size. Requires "file" and "size".

        reset=true;
    
public voidsetSize(int size)
Size. Used only if the scoreboard is to be created.

        this.size=size;
    
public voidsetUnixSocket(java.lang.String unixSocket)
Unix socket where tomcat is listening. Use it only if tomcat is on the same host, of course

        this.unixSocket=unixSocket;
    
public voidsetUnregister(boolean unregister)
Set this option to mark the tomcat instance as 'down', so apache will no longer forward messages to it. Note that requests with a session will still try this host first. This can be used to implement gracefull shutdown. Host and port are still required, since they are used to identify tomcat.

        this.unregister=true;
    
public voidunRegisterTomcat(java.lang.String host, int port)

        String slotName="TOMCAT:" + host + ":" + port;
        MsgContext mCtx=createMsgContext();
        Msg msg=(Msg)mCtx.getMsg(0);
        msg.reset();
        C2BConverter c2b=mCtx.getConverter();
        
        msg.appendByte( SHM_WRITE_SLOT );
        appendString( msg, slotName, c2b );

        // number of channels for this instance
        msg.appendInt( 0 );
        msg.appendInt( 0 );
        
        if (log.isDebugEnabled())
            log.debug("UnRegister " + slotName );
        this.invoke( msg, mCtx );