This solution uses the Java Servlet 2.3 ServletContextListener interface. The ServletContextListener interface can be used to listen and react to a variety of servlet events, including application lifecycle. In order to automate the addition and removal of an application from the appropriate BIG-IP pool, we'll be listening for two events: contextInitialized and contextDestroyed. In the former, we'll add the application to the appropriate pool and in the latter, we'll remove it automatically. This proactive approach to managing applications managed by BIG-IP LTM (Local Traffic Manager) ensures that requests are not caught in between a monitor's health check interval, which can result in either an error or a second connection as part of a retry event within an iRule. This improves performance by ensuring that only active applications receive requests, and reduces connection attempts that can improve the efficiency of high-volume applications.

This solution was built using Apache/Tomcat 4.1, Eclipse, and the Java EE SDK 5.

1. In a new or existing Web application project, add a web service client by loading the WSDL for LBLocalPool (https://<your BIG-IP>/iControl/iControlPortal.cgi?WSDL=LocalLB.Pool).You'll have to save the WSDL locally and then import it as a resource before you can use it to create a new Web service client.

2. Add a new Listener to your project that implements the ServletContextListener interface (application lifecycle).

3. In the contextInitialized method, create a connection to your BIG-IP and use the add_member operation of LBLocalPool to add the server to the appropriate pool

4. In the contextDestroyed method, use the remove_member operation of LBLocalPool to remove the server from the appropriate pool

Build and deploy!

The following sample code gives you the basics for accomplishing steps 3 and 4 above. Note that this is not a polished solution; in a real application you'll want to use context parameters (stored as external resources in web.xml) for values such as the pool name and connection parameters. A good example of how to do this can be found in this article [external link].

 

 

import javax.servlet.ServletContextEvent;

import javax.servlet.ServletContextListener;

import iControl.CommonIPPortDefinition;

import iControl.LocalLBPoolBindingStub;

import iControl.LocalLBPoolLocator;

 

/**

 * Application Lifecycle Listener implementation class HelloWorldListener

 *

 */

 

public class HelloWorldListener implements ServletContextListener {

    public LocalLBPoolBindingStub stub = null;

    public String[] poolNames = new String[1];

    public iControl.CommonIPPortDefinition[][] portdefs = new iControl.CommonIPPortDefinition[1][1];

    String fullUrl =

"https://<username>:<password>@<BIG-IP address>/iControl/iControlPortal.cgi";

   

    /**

     * Default constructor.

     */

    public HelloWorldListener() {

       

    }

 

    /**

     * @see ServletContextListener#contextInitialized(ServletContextEvent)

     */

    public void contextInitialized(ServletContextEvent arg0) {

        System.setProperty("javax.net.ssl.trustStore",

               System.getProperty("user.home") + "/.keystore");

 

        portdefs[0][0] = new iControl.CommonIPPortDefinition();

        portdefs[0][0].setPort(<port_on_this_server>);

        portdefs[0][0].setAddress("<ip address of this server>");

        poolNames[0] = "my_pool_name";

 

        XTrustProvider.install();

        try {

            stub = (iControl.LocalLBPoolBindingStub)new 

               iControl.LocalLBPoolLocator().getLocalLBPoolPort(new java.net.URL(fullUrl));

            stub.setTimeout(600);

            stub.add_member(poolNames, portdefs);

        } catch (Exception e) {

           // handle the exception here

        }      

    }

 

    /**

     * @see ServletContextListener#contextDestroyed(ServletContextEvent)

     */

    public void contextDestroyed(ServletContextEvent arg0) {

           try {

            stub = (iControl.LocalLBPoolBindingStub)new

                iControl.LocalLBPoolLocator().getLocalLBPoolPort(new java.net.URL(fullUrl) );

            stub.setTimeout(600);

            stub.remove_member(poolNames, portdefs);

        } catch (Exception e) {

           // handle the exception here

        }

 

    }

   

}