Getting Started with Terracotta Toolkit – Part 2 (Cluster Events)

In the last post we saw Terracotta toolkit Queue. In this Part, lets explore Cluster Events. Cluster Events are propagated across the Terracotta Cluster, whenever specific event happens inside the Cluster. In this post we shall explore what are the different types of Events available.

Cluster Events

As of 3.3.0, following are the four Events that are available

  • Node Joined
  • Node Left
  • Operations Enabled
  • Operations Disabled

So what do we need to do, in order to receive these events? Just two simple steps

  • Implement ClusterListener
  • Register the Listener with toolkit

Pre-requisite

  • Download Terracotta 3.3.0 from Download page
  • Include following terracotta-toolkit-1.0-runtime-1.0.0.jar into your classpath for using Toolkit. The jar is present inside Terracotta_Install_dir/common folder.
  • Implementing ClusterListener

    Here is the code

    class MyClusterListener implements ClusterListener {
    
            public void nodeJoined(ClusterEvent clusterEvent) {
                try {
                    System.out.println("Event Type = "+clusterEvent.getType());
                    System.out.println("Node Joined "+clusterEvent.getNode().getId()
                            + ", IP="+clusterEvent.getNode().getAddress());
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
            }
    
            public void nodeLeft(ClusterEvent clusterEvent) {
                try {
                    System.out.println("Node Left "+clusterEvent.getNode().getId()
                            + ", IP="+clusterEvent.getNode().getAddress());
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
            }
    
            public void operationsEnabled(ClusterEvent clusterEvent) {
                System.out.println("Operations Enabled");
            }
    
            public void operationsDisabled(ClusterEvent clusterEvent) {
                System.out.println("Operations Disabled");
            }
        }
    

    As you can see, the implementation is pretty simple. Now its time to register the listener

    ClusterInfo clusterInfo = clusterToolkit.getClusterInfo();
    clusterInfo.addClusterListener(new MyClusterListener());
    

    Get the complete code here

    Time to see the implementation in Action

    • Start Terracotta Server
    • Start the Listener
    • Start/Stop Terracotta Clients and see the events being received

    There can be some innovative things that can be done inside the ClusterListener. For ex, you can send SNMP Traps to your management system for nodeJoined or nodeLeft events.

    Stay tuned for more on Terracotta Toolkit...

    Looking for more details, you find them here

  • Terracotta Documentation
  • Terracotta Toolkit Javadoc
  • One thought on “Getting Started with Terracotta Toolkit – Part 2 (Cluster Events)

    Leave a Reply

    Your email address will not be published. Required fields are marked *

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