/**
   * @param it Iterator.
   * @param bound Value bound.
   * @throws Exception If failed.
   */
  private void testIteratorSerialization(Iterator<?> it, int bound) throws Exception {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();

    try (ObjectOutputStream out = new ObjectOutputStream(byteOut)) {
      out.writeObject(it);
    }

    byte[] bytes = byteOut.toByteArray();

    ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));

    Iterator<?> it0 = (Iterator<?>) in.readObject();

    int cnt = 0;

    while (it0.hasNext()) {
      Object obj = it0.next();

      if (obj instanceof GridCacheEntry) checkEntry((GridCacheEntry<String, Integer>) obj, bound);
      else if (obj instanceof String) checkKey((String) obj);
      else if (obj instanceof Integer) checkValue((Integer) obj, bound);
      else assert false : "Wrong type.";

      cnt++;
    }

    assert cnt == bound;
  }
  /** {@inheritDoc} */
  @Override
  public void loadCache(GridBiInClosure<K, V> c, @Nullable Object... args) throws GridException {
    ExecutorService exec =
        new ThreadPoolExecutor(
            threadsCnt,
            threadsCnt,
            0L,
            MILLISECONDS,
            new ArrayBlockingQueue<Runnable>(batchQueueSize),
            new BlockingRejectedExecutionHandler());

    Iterator<I> iter = inputIterator(args);

    Collection<I> buf = new ArrayList<>(batchSize);

    try {
      while (iter.hasNext()) {
        if (Thread.currentThread().isInterrupted()) {
          U.warn(log, "Working thread was interrupted while loading data.");

          break;
        }

        buf.add(iter.next());

        if (buf.size() == batchSize) {
          exec.submit(new Worker(c, buf, args));

          buf = new ArrayList<>(batchSize);
        }
      }

      if (!buf.isEmpty()) exec.submit(new Worker(c, buf, args));
    } catch (RejectedExecutionException ignored) {
      // Because of custom RejectedExecutionHandler.
      assert false : "RejectedExecutionException was thrown while it shouldn't.";
    } finally {
      exec.shutdown();

      try {
        exec.awaitTermination(Long.MAX_VALUE, MILLISECONDS);
      } catch (InterruptedException ignored) {
        U.warn(log, "Working thread was interrupted while waiting for put operations to complete.");

        Thread.currentThread().interrupt();
      }
    }
  }
Пример #3
0
    /** {@inheritDoc} */
    @Override
    public Class<?> deployClass() {
      if (cls == null) {
        Class<?> cls0 = null;

        if (depCls != null) cls0 = depCls;
        else {
          for (Iterator<Object> it = objs.iterator();
              (cls0 == null || U.isJdk(cls0)) && it.hasNext(); ) {
            Object o = it.next();

            if (o != null) cls0 = U.detectClass(o);
          }

          if (cls0 == null || U.isJdk(cls0)) cls0 = GridDataLoaderImpl.class;
        }

        assert cls0 != null : "Failed to detect deploy class [objs=" + objs + ']';

        cls = cls0;
      }

      return cls;
    }