Esempio n. 1
0
  @Test
  public void testListener() throws Exception {
    final int maxItems = 10;
    final CountDownLatch itemAddedLatch = new CountDownLatch(maxItems);
    final CountDownLatch itemRemovedLatch = new CountDownLatch(maxItems);

    final IQueue queue = client.getQueue(randomString());

    String id =
        queue.addItemListener(
            new ItemListener() {

              public void itemAdded(ItemEvent itemEvent) {
                itemAddedLatch.countDown();
              }

              public void itemRemoved(ItemEvent item) {
                itemRemovedLatch.countDown();
              }
            },
            true);

    new Thread() {
      public void run() {
        for (int i = 0; i < maxItems; i++) {
          queue.offer(i);
          queue.remove(i);
        }
      }
    }.start();

    assertTrue(itemAddedLatch.await(5, TimeUnit.SECONDS));
    assertTrue(itemRemovedLatch.await(5, TimeUnit.SECONDS));
    queue.removeItemListener(id);
  }