Ejemplo n.º 1
0
  /**
   * Prints a phrase on the grid nodes running anonymous closure objects and calculating total
   * number of letters.
   *
   * @param phrase Phrase to print on of the grid nodes.
   * @throws GridException If failed.
   */
  private static void countLettersClosure(String phrase) throws GridException {
    X.println(">>> Starting countLettersClosure() example...");

    // Explicitly execute the collection of callable objects and receive a result.
    Collection<Integer> results =
        G.grid()
            .call(
                SPREAD,
                new GridClosure<String, Integer>() { // Create executable logic.
                  @Override
                  public Integer apply(String word) {
                    // Print out a given word, just so we can
                    // see which node is doing what.
                    X.println(">>> Executing word: " + word);

                    // Return the length of a given word, i.e. number of letters.
                    return word.length();
                  }
                },
                Arrays.asList(phrase.split(" "))); // Collection of arguments for closures.

    // Add up all results using convenience 'sum()' method.
    int letterCnt = F.sum(results);

    X.println(">>>");
    X.println(">>> Finished execution of counting letters with closure based on GridGain 3.0 API.");
    X.println(">>> You should see the phrase '" + phrase + "' printed out on the nodes.");
    X.println(">>> Total number of letters in the phrase is '" + letterCnt + "'.");
    X.println(">>> Check all nodes for output (this node is also part of the grid).");
    X.println(">>>");
  }
  /** {@inheritDoc} */
  @Override
  public Object onReceive(@Nullable Object obj) {
    if (obj instanceof byte[]) {
      X.println(">>> Byte array received over REST: " + Arrays.toString((byte[]) obj));

      BigInteger val = new BigInteger((byte[]) obj);

      X.println(">>> Unpacked a BigInteger from byte array received over REST: " + val);

      return val;
    } else return obj;
  }
  /** {@inheritDoc} */
  @Override
  public Object onSend(Object obj) {
    if (obj instanceof BigInteger) {
      X.println(">>> Creating byte array from BigInteger to send over REST: " + obj);

      byte[] bytes = ((BigInteger) obj).toByteArray();

      X.println(
          ">>> Created byte array from BigInteger to send over REST: " + Arrays.toString(bytes));

      return bytes;
    } else return obj;
  }
Ejemplo n.º 4
0
  /**
   * Calculates length of a given phrase on the grid.
   *
   * @param phrase Phrase to count the number of letters in.
   * @throws GridException If failed.
   */
  private static void countLettersReducer(String phrase) throws GridException {
    X.println(">>> Starting countLettersReducer() example...");

    Grid grid = G.grid();

    // Logger to use in your closure. Note that even though we assign it
    // to a local variable, GridGain still allows to use it from remotely
    // executed code.
    final GridLogger log = grid.log();

    // Execute Hello World task.
    int letterCnt =
        grid.reduce(
            BALANCE,
            new GridClosure<String, Integer>() { // Create executable logic.
              @Override
              public Integer apply(String word) {
                // Print out a given word, just so we can
                // see which node is doing what.
                log.info(">>> Calculating for word: " + word);

                // Return the length of a given word, i.e. number of letters.
                return word.length();
              }
            },
            Arrays.asList(phrase.split(" ")), // Collection of words.
            // Create custom reducer.
            // NOTE: Alternatively, you can use existing reducer: F.sumIntReducer()
            new GridReducer<Integer, Integer>() {
              private int sum;

              @Override
              public boolean collect(Integer res) {
                sum += res;

                return true; // True means continue collecting until last result.
              }

              @Override
              public Integer apply() {
                return sum;
              }
            });

    X.println(">>>");
    X.println(">>> Finished execution of counting letters with reducer based on GridGain 3.0 API.");
    X.println(">>> Total number of letters in the phrase is '" + letterCnt + "'.");
    X.println(">>> You should see individual words printed out on different nodes.");
    X.println(">>> Check all nodes for output (this node is also part of the grid).");
    X.println(">>>");
  }
Ejemplo n.º 5
0
  /**
   * Starts up grid and checks all provided values for prime.
   *
   * @param args Command line arguments, none required but if provided first one should point to the
   *     Spring XML configuration file. See {@code "examples/config/"} for configuration file
   *     examples.
   * @throws GridException If example execution failed.
   */
  public static void main(String[] args) throws GridException {
    // Starts grid.
    Grid grid = args.length == 0 ? G.start() : G.start(args[0]);

    // Values we want to check for prime.
    long[] checkVals = {32452841, 32452843, 32452847, 32452849, 236887699, 217645199};

    X.println(">>>");
    X.println(
        ">>> Starting to check the following numbers for primes: " + Arrays.toString(checkVals));

    try {
      long start = System.currentTimeMillis();

      for (long checkVal : checkVals) {
        // This method will be executed on the Grid as it is
        // annotated with @Gridify annotation.
        Long divisor = GridPrimeChecker.checkPrime(checkVal, 2, checkVal);

        // If divisor is null, then the number is prime.
        if (divisor == null) {
          X.println("Value '" + checkVal + "'is a prime number.");
        } else {
          X.println("Value '" + checkVal + "' is divisible by '" + divisor + '\'');
        }
      }

      long totalTime = System.currentTimeMillis() - start;

      X.println(">>> Total time to calculate all primes (milliseconds): " + totalTime);
      X.println(">>>");
    } finally {
      // Stops grid.
      G.stop(grid.name(), true);
    }
  }