Dashboard > GridGain User Guide > Table Of Contents > Examples Gallery > Grid Discovery Events Processing
Grid Discovery Events Processing
Added by ghost, last edited by morpheus on Feb 12, 2008  (view change)
Labels: 
(None)


Grid Discovery Events Processing Example.

Package:
org.gridgain.examples.discovery

This example demonstrates usage of grid discovery events. During startup new node communicates with remote ones to set up a connection and exchange attributes. This is called discovery. Example starts one node and waits for the others. It shows dialog with one OK button and prints all discovery events to the console. When dialog button pressed it stops grid and finishes execution.

There is one class implemented for this example:

Running Grid Node

This example will need one remote node to be running. Note that you don't need another machine for it - you can start remote node on the same machine you are running example on.

To start a remote node open the terminal window on Linux/Mac OS X or Command Prompt on Windows, change directory to ${GRIDGAIN_HOME}/bin and run the gridgain.{sh|bat} script. It takes 2-3 seconds for grid node to start and if everything worked fine you should see starting log ending with successful start acknowledgment.

GridDiscoveryExample.java

1. Import GridGain classes.

import org.gridgain.grid.*;

2. Add Grid Start and Stop.

GridFactory.start();
        
try {
    ...
}
finally {
    GridFactory.stop(true);
}

finally clause allows for graceful grid shutdown in case of the exceptions.

3. Set up discovery listener.

grid.addDiscoveryListener(listener);

Full Source Code

GridDiscoveryExample.java
package org.gridgain.examples.discovery;

import java.util.*;
import javax.swing.*;
import org.gridgain.grid.*;

public final class GridDiscoveryExample {
    /**
     * Ensure singleton.
     */
    private GridDiscoveryExample() {
        // No-op.
    }

    /**
     * Executes discovery example.
     *
     * @param args Command line arguments, none required but if provided
     *      first one should point to the Spring XML configuration file. See
     *      <tt>"examples/config/"</tt> for configuration file examples.
     * @throws GridException If example execution failed.
     */
    public static void main(String[] args) throws GridException {
        if (args.length == 0) {
            GridFactory.start();
        }
        else {
            GridFactory.start(args[0]);
        }

        try {
            Grid grid = GridFactory.getGrid();

            // Define discovery listener.
            GridDiscoveryListener listener = new  GridDiscoveryListener() {
                /**
                 * {@inheritDoc}
                 */
                public void onDiscovery(GridDiscoveryEventType type, GridNode node) {
                    System.out.println(">>> Received discovery event [type=" + type + ", originatingNode=" + node + ']');
                }
            };

            // Register discovery listener on the grid.
            grid.addDiscoveryListener(listener);
            // Wait until Ok is pressed.
            JOptionPane.showMessageDialog(
                null,
                new JComponent[] {
                    new JLabel("GridGain started."),
                    new JLabel(
                        "<html>" +
                            "Start up other grid nodes by executing <br>" +
                            "one or more 'gridgain.bat' or 'gridgain.sh' scripts<br>" +
                            "and click OK." +
                        "</html>")
                },
                "GridGain Discovery Example",
                JOptionPane.INFORMATION_MESSAGE
            );

            // Get local grid node.
            GridNode localNode = grid.getLocalNode();

            // Get remote grid nodes.
            Collection<GridNode> remoteNodes = grid.getRemoteNodes();

            // Get local and remote grid nodes in one collection.
            Collection<GridNode> allNodes = grid.getAllNodes();

            assert allNodes.contains(localNode) == true;
            assert allNodes.containsAll(remoteNodes) == true;

            System.out.println(">>> Local node: " + localNode);

            // Remote nodes.
            for (GridNode node : remoteNodes) {
                System.out.println(">>> Remote node: " + node);
            }

            // Unregister discovery listener
            grid.removeDiscoveryListener(listener);

            System.out.println(">>>");
            System.out.println(">>> Finished executing Grid Discovery Example.");
            System.out.println(">>> Check local node output.");
            System.out.println(">>>");
        }
        finally {
            GridFactory.stop(true);
        }
    }
}

Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.10 Build:#528 Nov 29, 2006) - Bug/feature request - Contact Administrators