Listening for Events
Prev | Chapter 5. Writing Monitor Nodes | Next

## Listening for Events

One of the things the Monitor class allows you to do is to listen for certain events that occur in the composition of the replication group. Your Monitor can be notified of these events by running an event listener using Monitor.startListener(). For example:

`Monitor.startListener()` takes a single argument, and that is an instance of MonitorChangeListener. `MonitorChangeListener` is an interface that you implement for the purpose of handling replication group events.

There are four events that the change listener can be notified of. Each of these are represented by a unique class:

1. GroupChangeEvent

   A new instance of this event is generated each time a node is added or removed from the replication group.

2. NewMasterEvent

   A new instance of this event is generated each time a new Master is elected.

3. JoinGroupEvent

   A new instance of this event is generated each time a node joins a group. The event is generated on a "best effort" basis. It may not be generated, for example, if the joining node was unable to communicate with the monitor due to a network problem. The application must be resilient in the face of such missing events.

4. LeaveGroupEvent

   A new instance of this event is generated each time a node leaves the group. The event is generated on a "best effort" basis. It may not be generated if the node leaving the group dies (for example, it was killed) before it has a chance to generate the event, or if the node was unable to communicate with the monitor due to a network problem. The application must be resilient in the face of such missing events.

For example, an implementation of the MonitorChangeListener interface might be:

    class MyChangeListener implements MonitorChangeListener {

        public void notify(NewMasterEvent newMasterEvent) {

         String newNodeName = newMasterEvent.getNodeName();

         InetSocketAddress newMasterAddr =
                newMasterEvent.getSocketAddress();
         String newMasterHostName = newMasterAddr.getHostName();
         int newMasterPort = newMasterAddr.getPort();

         // Do something with this information here.
        }

        public void notify(GroupChangeEvent groupChangeEvent) {
         ReplicationGroup repGroup = groupChangeEvent.getRepGroup();

         // Do something with the new ReplicationGroup composition here.
        }

        ...

    }

You can then start the Monitor listener as follows:

     // Initialize the monitor node config
     ReplicationConfig config =
            new ReplicationConfig("MyRepGroupName",
                                  "mon1",
                                  "monhost1.acme.com:7000");
     config.setNodeType(NodeType.MONITOR);
     config.setHelperHosts("node1.acme.com:5000,node2.acme.com:5000");

     Monitor monitor = new Monitor(config);

     // If the monitor has not been registered as a member of the
     // group, register it now. register() returns the current node
     // that is the master.
     ReplicationNode currentMaster = monitor.register();

     // Start up the listener, so that it can be used to track changes
     // in the master node, or group composition.
     monitor.startListener(new MyChangeListener());

Prev | Up | Next
Chapter 5. Writing Monitor Nodes | Home | Chapter 6. Replication Examples