[AIX HP-UX Linux Solaris Windows]

Example: Implementing a side stream advisor

The following example demonstrates how a side stream advisor can be implemented. This sample illustrates suppressing the standard socket opened by the advisor base. Instead, this advisor opens a side stream Java socket to query a server. This procedure can be useful for servers that use a different port from normal client traffic to listen for an advisor query.

In this example, a server is listening on port 11999 and when queried returns a load value with a hexadecimal int "4". This sample runs in replace mode, that is, the last parameter of the advisor constructor is set to true and the advisor base code uses the returned load value rather than the elapsed time.

Note the call to supressBaseOpeningSocket() in the initialization routine. Suppressing the base socket when no data will be sent is not required. For example, you might want to open the socket to ensure that the advisor can contact the server. Examine the needs of your application carefully before making this choice.

package CustomAdvisors; 
import java.io.*; 
import java.net.*; 
import java.util.*; 
import java.util.Date; 
import com.ibm.internet.lb.advisors.*; 
import com.ibm.internet.lb.common.*; 
import com.ibm.internet.lb.server.SRV_ConfigServer; 

public class ADV_sidea extends ADV_Base implements ADV_MethodInterface { 
  static final String ADV_NAME = "sidea"; 
  static final int ADV_DEF_ADV_ON_PORT = 12345; 
  static final int ADV_DEF_INTERVAL = 7; 

  // create an array of bytes with the load request message 
  static final byte[] abHealth = {(byte)0x00, (byte)0x00, (byte)0x00,
                                  (byte)0x04}; 

  public ADV_sidea() { 
    super(ADV_NAME, "3.0.0.0-03.31.00", ADV_DEF_ADV_ON_PORT, 
          ADV_DEF_INTERVAL, "", 
          true);                     // replace mode parameter is true 
    super.setAdvisor( this ); 
  } 

//-------- 
// ADV_AdvisorInitialize 
  public void ADV_AdvisorInitialize() 
  { 
    suppressBaseOpeningSocket();        // tell base code not to open the 
                                        // standard socket 
    return; 
  } 

//-------- 
// getLoad 
  public int getLoad(int iConnectTime, ADV_Thread caller) { 
    int iRc; 
    int iLoad = ADV_HOST_INACCESSIBLE;    // -1 
    int iControlPort = 11999;             // port on which to communicate
                                          // with the server 
    String sServer = caller.getCurrentServerId(); // address of server to query 
    try { 
      socket soServer = new Socket(sServer, iControlPort);   // open socket to 
                                                             // server 
      DataInputStream disServer = new DataInputStream( 
                                                      soServer.getInputStream()); 
      DataOutputStream dosServer = new DataOutputStream( 
                                                      soServer.getOutputStream()); 
      int iRecvTimeout = 10000;           // set timeout (in milliseconds) 
                                          // for receiving data 
      soServer.setSoTimeout(iRecvTimeout); 
      dosServer.writeInt(4);              // send a message to the server 
      dosServer.flush(); 
      iLoad = disServer.readByte();       // receive the response from the server 

    } catch (exception e) { 
      system.out.println("Caught exception " + e); 
    } 
    return iLoad;                         // return the load reported from the server 
  } 
}



Related reference
Example: Sample advisor
Reference topic Reference topic    

Terms of Use | Feedback

Last updatedLast updated: Jun 20, 2011 4:04:37 PM EDT
File name: rprf_advexss.html