Improve your Java Application Detecting Oracle RAC FAN Events


Grab a RAC environment and configure a service, ons and so on…

Create a java Project with your preferred tool and add the simplefan.jar library you can find in the jdbc distributions and create a java class like this:

package es.jmu.oracle.altamira.gk;
import java.util.Properties;
import oracle.simplefan.FanEventListener;
import oracle.simplefan.FanManager;
import oracle.simplefan.FanSubscription;
import oracle.simplefan.LoadAdvisoryEvent;
import oracle.simplefan.NodeDownEvent;
import oracle.simplefan.ServiceDownEvent;
public class FanListener {
    public FanListener() {
        super();
    }
    static int DUERMETE_MI_NINO = 1000;
    public static void main(String[] args) {
        //FanListener fanSample = new FanListener();
        
        /*
         * TEST DATA
         */
        String onsNodes = "192.168.56.110:6200,192.168.56.210:6200";
        String service = "tfanservice";
        
        
        Properties props = new Properties();
        props.setProperty("onsNodes", onsNodes);
        FanManager.getInstance().configure(props);
        props = new Properties();
        props.setProperty("serviceName", service);
        FanSubscription fsub = FanManager.getInstance().subscribe(props);
        System.out.println("Listening to FAN events coming from " + onsNodes + " for service " + service);
        fsub.addListener(new FanEventListener() {
            /*
             * Fired when service goes down
             */
            public void handleEvent(ServiceDownEvent arg0) {
                System.out.println("Service Down!");
                System.out.println("=============");
                System.out.println("Database          : " + arg0.getDatabaseUniqueName());
                System.out.println("Kind              : " + arg0.getKind());
                System.out.println("Service Name      : " + arg0.getServiceName());
                System.out.println("Reason            : " + arg0.getReason());
                System.out.println("ServiceMemberEvent: " + arg0.getServiceMemberEvent());
                System.out.println("Observed at       : " + arg0.getTimestamp().getTime() + "\n\n");
            }
            /*
             * Fired when a node goes down
             */
            public void handleEvent(NodeDownEvent arg0) {
                System.out.println("Node Down");
                System.out.println("=========");
                System.out.println("Incarnation: " + arg0.getIncarnation() + "\n\n");
            }
            /*
             * Fired when 
             */
            public void handleEvent(LoadAdvisoryEvent arg0) {
            System.out.println("Load Advisory event");
            System.out.println("==================="); 
            System.out.println("Database            : " + arg0.getDatabaseUniqueName());
            System.out.println("Instance            : " + arg0.getInstanceName());
            System.out.println("Service Name        : " + arg0.getServiceName());
            System.out.println("Service Quality     : " + arg0.getServiceQuality());
            System.out.println("Percent             : " + arg0.getPercent());
            System.out.println("Observed at         : " + arg0.getTimestamp().getTime() + "\n\n");
            } } );
                   
            while(true){
                try{Thread.sleep(DUERMETE_MI_NINO);}catch(Exception e){;}
           
            }
    }
}

Run the java class and be a bad boy with the RAC env (start/stop services|instances, generate load…) and see what happens…

Enjoy 😉

2 Comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.