/** {@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; }
/** * 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); } }