public static void Main()
        {
            /* Enable debug messages. */
            //Debug.Listeners.Add(new TextWriterTraceListener(System.Console.Out));

            String taskName = "org.gridgain.examples.task.CharCountTask";
            String taskArg  = "Hello Dot Net World";

            IGridClient client = CreateClient();

            try
            {
                // Show grid topology.
                X.WriteLine(">>> Client created, current grid topology: " + ToString(client.Compute().Nodes()));

                IGridClientCompute prj = client.Compute();

                // Execute test task that will count total number of nodes in grid.
                int wordCnt = prj.Execute <int>(taskName, taskArg);

                X.WriteLine(">>> Task result [args='" + taskArg + "', wordCnt=" + wordCnt + ']');
            }
            catch (GridClientException e)
            {
                Console.WriteLine("Unexpected grid client exception happens: {0}", e);
            }
            finally
            {
                GridClientFactory.StopAll();
            }
        }
示例#2
0
        public static void Main()
        {
            /* Enable debug messages. */
            //Debug.Listeners.Add(new TextWriterTraceListener(System.Console.Out));

            String taskName = "org.gridgain.examples.misc.client.api.ClientExampleTask";
            String taskArg  = ".NET client - ";

            IGridClient client = CreateClient();

            try {
                // Show grid topology.
                X.WriteLine(">>> Client created, current grid topology: " + ToString(client.Compute().Nodes()));

                // Random node ID.
                Guid randNodeId = client.Compute().Nodes()[0].Id;

                // Note that in this example we get a fixed projection for task call because we cannot guarantee that
                // other nodes contain ClientExampleTask in classpath.
                IGridClientCompute prj = client.Compute().Projection(delegate(IGridClientNode node) {
                    return(node.Id.Equals(randNodeId));
                });

                // Execute test task that will count total number of nodes in grid.
                int entryCnt = prj.Execute <int>(taskName, taskArg + "predicate projection");

                X.WriteLine(">>> Predicate projection : there are totally " + entryCnt + " nodes in the grid");

                // Same as above, using different projection API.
                IGridClientNode clntNode = prj.Node(randNodeId);

                prj = prj.Projection(clntNode);

                entryCnt = prj.Execute <int>(taskName, taskArg + "node projection");

                X.WriteLine(">>> GridClientNode projection : there are totally " + entryCnt + " nodes in the grid");

                // Use of collections is also possible.
                prj = prj.Projection(new IGridClientNode[] { clntNode });

                entryCnt = prj.Execute <int>(taskName, taskArg + "nodes collection projection");

                X.WriteLine(">>> Collection projection : there are totally " + entryCnt + " nodes in the grid");

                // Balancing - may be random or round-robin. Users can create
                // custom load balancers as well.
                IGridClientLoadBalancer balancer = new GridClientRandomBalancer();

                // Balancer may be added to predicate or collection examples.
                prj = client.Compute().Projection(delegate(IGridClientNode node) {
                    return(node.Id.Equals(randNodeId));
                }, balancer);

                entryCnt = prj.Execute <int>(taskName, taskArg + "predicate projection with balancer");

                X.WriteLine(">>> Predicate projection with balancer : there are totally " + entryCnt +
                            " nodes in the grid");

                // Now let's try round-robin load balancer.
                balancer = new GridClientRoundRobinBalancer();

                prj = prj.Projection(new IGridClientNode[] { clntNode }, balancer);

                entryCnt = prj.Execute <int>(taskName, taskArg + "node projection with balancer");

                X.WriteLine(">>> GridClientNode projection : there are totally " + entryCnt + " nodes in the grid");

                // Execution may be asynchronous.
                IGridClientFuture <int> fut = prj.ExecuteAsync <int>(taskName, taskArg + "asynchronous execution");

                X.WriteLine(">>> Execute async : there are totally " + fut.Result + " nodes in the grid");

                // GridClientCompute can be queried for nodes participating in it.
                ICollection <IGridClientNode> c = prj.Nodes(new Guid[] { randNodeId });

                X.WriteLine(">>> Nodes with Guid " + randNodeId + " : " + ToString(c));

                // Nodes may also be filtered with predicate. Here
                // we create projection which only contains local node.
                c = prj.Nodes(delegate(IGridClientNode node) {
                    return(node.Id.Equals(randNodeId));
                });

                X.WriteLine(">>> Nodes filtered with predicate : " + ToString(c));

                // Information about nodes may be refreshed explicitly.
                clntNode = prj.RefreshNode(randNodeId, true, true);

                X.WriteLine(">>> Refreshed node : " + clntNode);

                // As usual, there's also an asynchronous version.
                IGridClientFuture <IGridClientNode> futClntNode = prj.RefreshNodeAsync(randNodeId, false, false);

                X.WriteLine(">>> Refreshed node asynchronously : " + futClntNode.Result);

                // Nodes may also be refreshed by IP address.
                String clntAddr = "127.0.0.1";

                foreach (var addr in clntNode.AvailableAddresses(GridClientProtocol.Tcp))
                {
                    if (addr != null)
                    {
                        clntAddr = addr.Address.ToString();
                    }
                }

                // Force node metrics refresh (by default it happens periodically in the background).
                clntNode = prj.RefreshNode(clntAddr, true, true);

                X.WriteLine(">>> Refreshed node by IP : " + clntNode);

                // Asynchronous version.
                futClntNode = prj.RefreshNodeAsync(clntAddr, false, false);

                X.WriteLine(">>> Refreshed node by IP asynchronously : " + futClntNode.Result);

                // Topology as a whole may be refreshed, too.
                ICollection <IGridClientNode> top = prj.RefreshTopology(true, true);

                X.WriteLine(">>> Refreshed topology : " + ToString(top));

                // Asynchronous version.
                IGridClientFuture <IList <IGridClientNode> > topFut = prj.RefreshTopologyAsync(false, false);

                X.WriteLine(">>> Refreshed topology asynchronously : " + ToString(topFut.Result));
            }
            catch (GridClientException e) {
                Console.WriteLine("Unexpected grid client exception happens: {0}", e);
            }
            finally {
                GridClientFactory.StopAll();
            }
        }