コード例 #1
0
  /**
   * 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);
    }
  }
コード例 #2
0
  /**
   * Listen to events coming from all grid nodes.
   *
   * @throws GridException If failed.
   */
  private static void remoteListen() throws GridException {
    Grid g = GridGain.grid();

    GridCache<Integer, String> cache = g.cache(CACHE_NAME);

    // Register remote event listeners on all nodes running cache.
    GridFuture<?> fut =
        g.forCache(CACHE_NAME)
            .events()
            .remoteListen(
                // This optional local callback is called for each event notification
                // that passed remote predicate filter.
                new GridBiPredicate<UUID, GridCacheEvent>() {
                  @Override
                  public boolean apply(UUID nodeId, GridCacheEvent evt) {
                    System.out.println();
                    System.out.println(
                        "Received event [evt="
                            + evt.name()
                            + ", key="
                            + evt.key()
                            + ", oldVal="
                            + evt.oldValue()
                            + ", newVal="
                            + evt.newValue());

                    return true; // Return true to continue listening.
                  }
                },
                // Remote filter which only accepts events for keys that are
                // greater or equal than 10 and if local node is primary for this key.
                new GridPredicate<GridCacheEvent>() {
                  /** Auto-inject grid instance. */
                  @GridInstanceResource private Grid g;

                  @Override
                  public boolean apply(GridCacheEvent evt) {
                    Integer key = evt.key();

                    return key >= 10
                        && g.cache(CACHE_NAME).affinity().isPrimary(g.localNode(), key);
                  }
                },
                // Types of events for which listeners are registered.
                EVT_CACHE_OBJECT_PUT,
                EVT_CACHE_OBJECT_READ,
                EVT_CACHE_OBJECT_REMOVED);

    // Wait until event listeners are subscribed on all nodes.
    fut.get();

    int keyCnt = 20;

    // Generate cache events.
    for (int i = 0; i < keyCnt; i++) cache.putx(i, Integer.toString(i));
  }
コード例 #3
0
  /**
   * Executes example.
   *
   * @param args Command line arguments, none required.
   * @throws GridException If example execution failed.
   */
  public static void main(String[] args) throws Exception {
    try (Grid grid = GridGain.start("examples/config/example-cache.xml")) {
      System.out.println();
      System.out.println(">>> Events API example started.");

      // Listen to events happening on local node.
      localListen();

      // Listen to events happening on all grid nodes.
      remoteListen();

      // Wait for a while while callback is notified about remaining puts.
      Thread.sleep(1000);
    }
  }
コード例 #4
0
  /**
   * 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 reducer example started.");

      Integer sum =
          g.compute()
              .apply(
                  new GridClosure<String, Integer>() {
                    @Override
                    public Integer apply(String word) {
                      System.out.println();
                      System.out.println(">>> Printing '" + word + "' on this node from grid job.");

                      // Return number of letters in the word.
                      return word.length();
                    }
                  },

                  // Job parameters. GridGain will create as many jobs as there are parameters.
                  Arrays.asList("Count characters using reducer".split(" ")),

                  // Reducer to process results as they come.
                  new GridReducer<Integer, Integer>() {
                    private AtomicInteger sum = new AtomicInteger();

                    // Callback for every job result.
                    @Override
                    public boolean collect(Integer len) {
                      sum.addAndGet(len);

                      // Return true to continue waiting until all results are received.
                      return true;
                    }

                    // Reduce all results into one.
                    @Override
                    public Integer reduce() {
                      return sum.get();
                    }
                  })
              .get();

      System.out.println();
      System.out.println(">>> Total number of characters in the phrase is '" + sum + "'.");
      System.out.println(">>> Check all nodes for output (this node is also part of the grid).");
    }
  }
コード例 #5
0
  /**
   * 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.");
    }
  }
コード例 #6
0
  /**
   * Listen to events that happen only on local node.
   *
   * @throws GridException If failed.
   */
  private static void localListen() throws GridException {
    Grid g = GridGain.grid();

    // Register event listener for all local task execution events.
    g.events()
        .localListen(
            new GridPredicate<GridEvent>() {
              @Override
              public boolean apply(GridEvent evt) {
                GridTaskEvent taskEvt = (GridTaskEvent) evt;

                System.out.println();
                System.out.println(
                    "Git event notification [evt="
                        + evt.name()
                        + ", taskName="
                        + taskEvt.taskName()
                        + ']');

                return true;
              }
            },
            EVTS_TASK_EXECUTION);

    // Generate task events.
    g.compute()
        .withName("example-event-task")
        .run(
            new GridRunnable() {
              @Override
              public void run() {
                System.out.println();
                System.out.println("Executing sample job.");
              }
            })
        .get();
  }
コード例 #7
0
 /**
  * Start up an empty node with specified cache configuration.
  *
  * @param args Command line arguments, none required.
  * @throws GridException If example execution failed.
  */
 public static void main(String[] args) throws GridException {
   GridGain.start("examples/config/example-ggfs.xml");
 }
コード例 #8
0
 /**
  * Start up an empty node with specified configuration.
  *
  * @param args Command line arguments, none required.
  * @throws org.gridgain.grid.GridException If example execution failed.
  */
 public static void main(String[] args) throws GridException {
   GridGain.start(configuration());
 }