Dashboard > GridGain User Guide > Grid Discovery Example
Grid Discovery Example
Added by smith, last edited by smith on Oct 31, 2007  (view change)
Labels: 
(None)


Grid Discovery Example

This example demonstrates use of grid discovery methods of Grid Javadoc interface. GridDiscoveryListener Javadoc is defined to be triggered on any changes in grid topology and to display event information.

(will be available with 1.6.2 release)

Package:
org.gridgain.examples.discovery

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. Define GridDiscoveryListener Javadoc listener.

Every time changes in grid topology occur, a message is displayed in the local node console with event information.

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

4. Register Grid Discovery Listener.

grid.addDiscoveryListener(listener);

5. Register Grid Discovery Listener.

grid.addDiscoveryListener(listener);

6. Pause Execution.

Execution is paused by opening java option pane.

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
);

7. Remove Grid Discovery Listener.

grid.removeDiscoveryListener(listener);

Full Source Code

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

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

/**
 * Example of using grid discovery API methods. In this example we start up
 * one or more remote nodes and then verify that they have been discovered.
 */
public final class GridDiscoveryExample {
    /**
     * Ensure singleton.
     */
    private GridDiscoveryExample() {
        // No-op.
    }

    /**
     * Execute <tt>HelloWorld</tt> example on the grid.
     *
     * @param args Command line arguments, none required but if provided
     *      first one should point to the Spring 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 descovery 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