/**
   * Executes example.
   *
   * @param args Command line arguments, none required.
   * @throws GridException If example execution failed.
   */
  public static void main(String[] args) throws Exception {
    Timer timer = new Timer("priceBars");

    // Start grid.
    final Grid g = GridGain.start("examples/config/example-streamer.xml");

    System.out.println();
    System.out.println(">>> Streaming price bars example started.");

    try {
      TimerTask task = scheduleQuery(g, timer);

      streamData(g);

      // Force one more run to get final results.
      task.run();

      timer.cancel();

      // Reset all streamers on all nodes to make sure that
      // consecutive executions start from scratch.
      g.compute()
          .broadcast(
              new Runnable() {
                @Override
                public void run() {
                  if (!ExamplesUtils.hasStreamer(g, "priceBars"))
                    System.err.println(
                        "Default streamer not found (is example-streamer.xml "
                            + "configuration used on all nodes?)");
                  else {
                    GridStreamer streamer = g.streamer("priceBars");

                    System.out.println("Clearing bars from streamer.");

                    streamer.reset();
                  }
                }
              })
          .get();
    } finally {
      GridGain.stop(true);
    }
  }
  /**
   * Executes example.
   *
   * @param args Command line arguments, none required.
   * @throws GridException If example execution failed.
   */
  public static void main(String[] args) throws GridException {
    try (Grid g = GridGain.start("examples/config/example-compute.xml")) {
      System.out.println();
      System.out.println("Compute schedule example started.");

      // Schedule output message every minute.
      GridSchedulerFuture<?> fut =
          g.scheduler()
              .scheduleLocal(
                  new Callable<Integer>() {
                    private int invocations;

                    @Override
                    public Integer call() {
                      invocations++;

                      try {
                        g.compute()
                            .broadcast(
                                new GridRunnable() {
                                  @Override
                                  public void run() {
                                    System.out.println();
                                    System.out.println("Howdy! :) ");
                                  }
                                })
                            .get();
                      } catch (GridException e) {
                        throw new GridRuntimeException(e);
                      }

                      return invocations;
                    }
                  },
                  "{5, 3} * * * * *" // Cron expression.
                  );

      while (!fut.isDone()) System.out.println(">>> Invocation #: " + fut.get());

      System.out.println();
      System.out.println(">>> Schedule future is done and has been unscheduled.");
      System.out.println(">>> Check all nodes for hello message output.");
    }
  }
  /**
   * Streams random prices into the system.
   *
   * @param g Grid.
   * @throws GridException If failed.
   */
  private static void streamData(final Grid g) throws GridException {
    GridStreamer streamer = g.streamer("priceBars");

    for (int i = 0; i < CNT; i++) {
      for (int j = 0; j < INSTRUMENTS.length; j++) {
        // Use gaussian distribution to ensure that
        // numbers closer to 0 have higher probability.
        double price = round2(INITIAL_PRICES[j] + RAND.nextGaussian());

        Quote quote = new Quote(INSTRUMENTS[j], price);

        streamer.addEvent(quote);
      }
    }
  }