Example #1
0
 /** @see java.lang.Thread#run() */
 @Override
 public void run() {
   while (!stopRequested || (stopRequested && leaseQueue.size() > 0)) {
     Lease lease = null;
     try {
       lease = leaseQueue.poll(leaseCheckFrequency, TimeUnit.MILLISECONDS);
     } catch (InterruptedException e) {
       continue;
     } catch (ConcurrentModificationException e) {
       continue;
     } catch (Throwable e) {
       LOG.fatal("Unexpected exception killed leases thread", e);
       break;
     }
     if (lease == null) {
       continue;
     }
     // A lease expired.  Run the expired code before removing from queue
     // since its presence in queue is used to see if lease exists still.
     if (lease.getListener() == null) {
       LOG.error("lease listener is null for lease " + lease.getLeaseName());
     } else {
       lease.getListener().leaseExpired();
     }
     synchronized (leaseQueue) {
       leases.remove(lease.getLeaseName());
     }
   }
   close();
 }
 /** Stop processing the tasks */
 public synchronized void stop() {
   log.info("Stopping task manager. Task count " + taskHolderDelayQueue.size());
   for (TaskProcessor taskProcessor : taskProcessorQueue) {
     taskProcessor.deactivate();
   }
   taskProcessorQueue.clear();
 }
Example #3
0
 /** add succeeds */
 public void testAdd() {
   DelayQueue q = new DelayQueue();
   for (int i = 0; i < SIZE; ++i) {
     assertEquals(i, q.size());
     assertTrue(q.add(new PDelay(i)));
   }
 }
Example #4
0
 /** all elements successfully put are contained */
 public void testPut() {
   DelayQueue q = new DelayQueue();
   for (int i = 0; i < SIZE; ++i) {
     PDelay x = new PDelay(i);
     q.put(x);
     assertTrue(q.contains(x));
   }
   assertEquals(SIZE, q.size());
 }
  /** Start processing the tasks */
  public synchronized void start() {
    log.info("Starting task manager. Task count " + taskHolderDelayQueue.size());

    for (int i = 0; i < workerCount; i++) {
      TaskProcessor taskProcessor =
          new TaskProcessor(taskHolderDelayQueue, taskExceptionHandler, idleTaskDelayMillis);
      taskProcessorQueue.add(taskProcessor);
      taskExecutorPool.submit(taskProcessor);
    }
  }
Example #6
0
 /** drainTo(c, n) empties first min(n, size) elements of queue into c */
 public void testDrainToN() {
   for (int i = 0; i < SIZE + 2; ++i) {
     DelayQueue q = populatedQueue(SIZE);
     ArrayList l = new ArrayList();
     q.drainTo(l, i);
     int k = (i < SIZE) ? i : SIZE;
     assertEquals(SIZE - k, q.size());
     assertEquals(k, l.size());
   }
 }
Example #7
0
 /** Returns a new queue of given size containing consecutive PDelays 0 ... n. */
 private DelayQueue<PDelay> populatedQueue(int n) {
   DelayQueue<PDelay> q = new DelayQueue<PDelay>();
   assertTrue(q.isEmpty());
   for (int i = n - 1; i >= 0; i -= 2) assertTrue(q.offer(new PDelay(i)));
   for (int i = (n & 1); i < n; i += 2) assertTrue(q.offer(new PDelay(i)));
   assertFalse(q.isEmpty());
   assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
   assertEquals(n, q.size());
   return q;
 }
Example #8
0
 /** removeAll(c) removes only those elements of c and reports true if changed */
 public void testRemoveAll() {
   for (int i = 1; i < SIZE; ++i) {
     DelayQueue q = populatedQueue(SIZE);
     DelayQueue p = populatedQueue(i);
     assertTrue(q.removeAll(p));
     assertEquals(SIZE - i, q.size());
     for (int j = 0; j < i; ++j) {
       PDelay x = (PDelay) (p.remove());
       assertFalse(q.contains(x));
     }
   }
 }
Example #9
0
 /** drainTo(c) empties queue into another collection c */
 public void testDrainTo() {
   DelayQueue q = new DelayQueue();
   PDelay[] elems = new PDelay[SIZE];
   for (int i = 0; i < SIZE; ++i) {
     elems[i] = new PDelay(i);
     q.add(elems[i]);
   }
   ArrayList l = new ArrayList();
   q.drainTo(l);
   assertEquals(0, q.size());
   for (int i = 0; i < SIZE; ++i) assertEquals(elems[i], l.get(i));
   q.add(elems[0]);
   q.add(elems[1]);
   assertFalse(q.isEmpty());
   assertTrue(q.contains(elems[0]));
   assertTrue(q.contains(elems[1]));
   l.clear();
   q.drainTo(l);
   assertEquals(0, q.size());
   assertEquals(2, l.size());
   for (int i = 0; i < 2; ++i) assertEquals(elems[i], l.get(i));
 }
Example #10
0
  /** retainAll(c) retains only those elements of c and reports true if changed */
  public void testRetainAll() {
    DelayQueue q = populatedQueue(SIZE);
    DelayQueue p = populatedQueue(SIZE);
    for (int i = 0; i < SIZE; ++i) {
      boolean changed = q.retainAll(p);
      if (i == 0) assertFalse(changed);
      else assertTrue(changed);

      assertTrue(q.containsAll(p));
      assertEquals(SIZE - i, q.size());
      p.remove();
    }
  }
Example #11
0
 /** clear removes all elements */
 public void testClear() {
   DelayQueue q = populatedQueue(SIZE);
   q.clear();
   assertTrue(q.isEmpty());
   assertEquals(0, q.size());
   assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
   PDelay x = new PDelay(1);
   q.add(x);
   assertFalse(q.isEmpty());
   assertTrue(q.contains(x));
   q.clear();
   assertTrue(q.isEmpty());
 }
Example #12
0
  /** @param args */
  public static void main(String[] args) throws Exception {

    /*
     * Delayed queue to store the events
     */
    DelayQueue<Event> queue = new DelayQueue<>();

    /*
     * An array to store the Thread objects that execute the tasks
     */
    Thread threads[] = new Thread[5];

    /*
     * Create the five tasks
     */
    for (int i = 0; i < threads.length; i++) {
      Task task = new Task(i + 1, queue);
      threads[i] = new Thread(task);
    }

    /*
     * Execute the five tasks
     */
    for (int i = 0; i < threads.length; i++) {
      threads[i].start();
    }

    /*
     * Wait for the finalization of the five tasks
     */
    for (int i = 0; i < threads.length; i++) {
      threads[i].join();
    }

    /*
     * Write the results to the console
     */
    do {
      int counter = 0;
      Event event;
      do {
        event = queue.poll();
        if (event != null) counter++;
      } while (event != null);
      System.out.printf("At %s you have read %d events\n", new Date(), counter);
      TimeUnit.MILLISECONDS.sleep(500);
    } while (queue.size() > 0);
  }
Example #13
0
  /** put doesn't block waiting for take */
  public void testPutWithTake() throws InterruptedException {
    final DelayQueue q = new DelayQueue();
    Thread t =
        newStartedThread(
            new CheckedRunnable() {
              public void realRun() {
                q.put(new PDelay(0));
                q.put(new PDelay(0));
                q.put(new PDelay(0));
                q.put(new PDelay(0));
              }
            });

    awaitTermination(t);
    assertEquals(4, q.size());
  }
Example #14
0
  /** drainTo empties queue */
  public void testDrainToWithActivePut() throws InterruptedException {
    final DelayQueue q = populatedQueue(SIZE);
    Thread t =
        new Thread(
            new CheckedRunnable() {
              public void realRun() {
                q.put(new PDelay(SIZE + 1));
              }
            });

    t.start();
    ArrayList l = new ArrayList();
    q.drainTo(l);
    assertTrue(l.size() >= SIZE);
    t.join();
    assertTrue(q.size() + l.size() >= SIZE);
  }