Esempio n. 1
0
 private int pollCompletedDownloads() {
   int tasks = 0;
   for (int i = 0; i < completedRequests.size(); i++) {
     parserService.submit(new FeedParserTask(completedRequests.poll()));
     tasks++;
   }
   return tasks;
 }
 public final T take() {
   if (!state) {
     return queue.poll();
   } else {
     try {
       return queue.take();
     } catch (InterruptedException e) {
       e.printStackTrace();
     }
     return null;
   }
 }
  public void run() {
    while (true) {

      try {
        // System.out.println(r+": Take(wait)");
        // String[] info = q.take();
        String blogID = q.poll(60, TimeUnit.SECONDS);
        if (blogID == null) {
          System.out.println("Poll.Timeout");
          continue;
        }

        // System.out.println(r+": Take(get) : "+blogID);

        if (blogID == NO_MORE_WORK) {
          break;
        }

        URL feedUrl = new URL("http://www.blogger.com/feeds/" + blogID + "/comments/default");
        Query myQuery = new Query(feedUrl);
        myQuery.setMaxResults(25);

        System.out.print(r + "+,");
        Feed resultFeed = myService.query(myQuery, Feed.class);

        for (Entry entry : resultFeed.getEntries()) {
          if (entry.getAuthors().get(0).getUri() != null) {
            String profileID = entry.getAuthors().get(0).getUri().replaceAll("[^\\d]", "");
            if (profileID.length() == 20) {
              try {
                myStm.executeUpdate(
                    "INSERT IGNORE INTO author SET profileID = '" + profileID + "'");
                // System.out.print(r+"+,");
              } catch (Exception e) {
              }
            }
          }
        }

      } catch (Exception e) {
        System.out.print(r + "ERR,");
      }
    }

    System.out.println("Bye(" + r + ")");
    try {
      myStm.close();
    } catch (Exception e) {
    }
  }
  @Test
  public void testIssue292() throws Exception {
    final BlockingQueue qResponse = new ArrayBlockingQueue(1);
    createSingleNodeExecutorService("testIssue292")
        .submit(
            new MemberCheck(),
            new ExecutionCallback<Member>() {
              public void onResponse(Member response) {
                qResponse.offer(response);
              }

              public void onFailure(Throwable t) {}
            });
    Object response = qResponse.poll(10, TimeUnit.SECONDS);
    assertNotNull(response);
    assertTrue(response instanceof Member);
  }
Esempio n. 5
0
    /*package*/ List<O> doJob(List<I> batch) {
      List<O> generatedBatch;
      assert lastBatchSent == false;

      if (batch == POISON_PILL) {
        lastBatch = true;
        synchronized (name) {
          pendingJobs--;
        }
        //                System.out.println(name + " - lastBatch");
        generatedBatch = Collections.emptyList();
      } else {

        EXECUTOR task = taskQueue.poll();

        boolean nextNodesAvailable = true;
        for (Node<O, ?, ?> node : nodes) {
          nextNodesAvailable &= node.isAvailable();
        }

        if (task == null) { // No available task
          resubmit(batch);
          generatedBatch = null;
        } else if (!nextNodesAvailable) { // Next nodes have to many batches.
          try {
            taskQueue.put(task);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          resubmit(batch);
          generatedBatch = null;
        } else { // Execute

          generatedBatch = execute(task, batch);
          //                    System.out.println(name + " - end job - " + generatedBatch.size());
          for (Node<O, ?, ?> node : nodes) {
            node.submit(generatedBatch);
          }

          try {
            taskQueue.put(task);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          synchronized (name) {
            pendingJobs--;
          }
          //                    System.out.println(name + " - pendingJobs " + pendingJobs);
        }
      }

      if (isFinished()) {
        if (!lastBatchSent) {
          for (Node<O, ?, ?> node : nodes) {
            node.submit(POISON_PILL);
          }
          lastBatchSent = true;
        }
        System.out.println("Node '" + name + "' is finished");
        synchronized (syncObject) {
          syncObject.notify();
        }
      } else {
        System.out.println("Node '" + name + "' pendingJobs " + pendingJobs);
      }
      return generatedBatch;
    }
Esempio n. 6
0
 public DocumentChange next(long timeout, TimeUnit unit) throws InterruptedException {
   assertRunningState();
   DocumentChange c = changes.poll(timeout, unit);
   checkIfInterrupted(c);
   return c;
 }
Esempio n. 7
0
 public DocumentChange poll() throws InterruptedException {
   assertRunningState();
   DocumentChange c = changes.poll();
   checkIfInterrupted(c);
   return c;
 }