Exemplo 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);
  }
Exemplo n.º 2
0
  @Test
  public void testContains() {
    final IQueue q = client.getQueue(randomString());

    q.offer(1);
    assertTrue(q.contains(1));
    assertFalse(q.contains(2));
  }
Exemplo n.º 3
0
  @Test
  public void testRemove() throws IOException {
    final IQueue q = client.getQueue(randomString());

    q.offer(1);
    assertTrue(q.remove(1));
    assertFalse(q.remove(2));
  }
Exemplo n.º 4
0
  @Test
  public void testRemainingCapacity() throws IOException {
    final IQueue q = client.getQueue(randomString());

    assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
    q.offer("one");
    assertEquals(Integer.MAX_VALUE - 1, q.remainingCapacity());
  }
Exemplo n.º 5
0
 @Test
 public void testPeak() throws InterruptedException {
   final IQueue q = client.getQueue(randomString());
   q.offer(1);
   assertEquals(1, q.peek());
   assertEquals(1, q.peek());
   assertEquals(1, q.size());
 }
Exemplo n.º 6
0
  @Test
  public void testQueueWithSizeLimit() {
    final IQueue q = client.getQueue(queueForTestQueueWithSizeLimit);

    for (int i = 0; i < maxSizeForQueue; i++) {
      q.offer(i);
    }
    assertFalse(q.offer(maxSizeForQueue));
  }
Exemplo n.º 7
0
  @Test
  public void testSize() {
    final int maxItems = 143;
    final IQueue q = client.getQueue(randomString());

    for (int i = 0; i < maxItems; i++) {
      q.add(i);
    }
    assertEquals(maxItems, q.size());
  }
Exemplo n.º 8
0
  @Test
  public void testRetainEmptyList() throws IOException {
    final int maxItems = 131;
    final IQueue q = client.getQueue(randomString());

    for (int i = 0; i < maxItems; i++) {
      q.add(i);
    }

    List retain = new LinkedList();
    assertTrue(q.retainAll(retain));
    assertEquals(0, q.size());
  }
Exemplo n.º 9
0
  @Test
  public void testAddAll() throws IOException {
    final int maxItems = 13;
    final IQueue q = client.getQueue(randomString());

    Collection coll = new ArrayList(maxItems);

    for (int i = 0; i < maxItems; i++) {
      coll.add(i);
    }

    assertTrue(q.addAll(coll));
    assertEquals(coll.size(), q.size());
  }
Exemplo n.º 10
0
  @Test
  public void testToArray() {
    final int maxItems = 19;
    final IQueue q = client.getQueue(randomString());

    Object[] offered = new Object[maxItems];
    for (int i = 0; i < maxItems; i++) {
      q.offer(i);
      offered[i] = i;
    }

    Object[] result = q.toArray();
    assertEquals(offered, result);
  }
Exemplo n.º 11
0
  @Test
  public void testIterator() {
    final int maxItems = 18;
    final IQueue q = client.getQueue(randomString());

    for (int i = 0; i < maxItems; i++) {
      q.offer(i);
    }

    int i = 0;
    for (Object o : q) {
      assertEquals(i++, o);
    }
  }
Exemplo n.º 12
0
  @Test
  public void testRemoveList_whereNotFound() throws IOException {
    final int maxItems = 131;
    final IQueue q = client.getQueue(randomString());

    List removeList = new LinkedList();
    for (int i = 0; i < maxItems; i++) {
      q.add(i);
    }
    removeList.add(maxItems + 1);
    removeList.add(maxItems + 2);

    assertFalse(q.removeAll(removeList));
    assertEquals(maxItems, q.size());
  }
Exemplo n.º 13
0
  @Test
  public void testContainsAll() {
    final int maxItems = 11;
    final IQueue q = client.getQueue(randomString());

    List trueList = new ArrayList();
    List falseList = new ArrayList();
    for (int i = 0; i < maxItems; i++) {
      q.offer(i);
      trueList.add(i);
      falseList.add(i + 1);
    }
    assertTrue(q.containsAll(trueList));
    assertFalse(q.containsAll(falseList));
  }
Exemplo n.º 14
0
  @Test
  public void testDrain() {
    final int maxItems = 12;
    final IQueue q = client.getQueue(randomString());

    List offeredList = new LinkedList();
    for (int i = 0; i < maxItems; i++) {
      q.offer(i);
      offeredList.add(i);
    }
    List drainedList = new LinkedList();
    int totalDrained = q.drainTo(drainedList);

    assertEquals(maxItems, totalDrained);
    assertEquals(offeredList, drainedList);
  }
 @Test
 public void testHazelcastInstances() {
   assertNotNull(map1);
   assertNotNull(map2);
   assertNotNull(multiMap);
   assertNotNull(queue);
   assertNotNull(topic);
   assertNotNull(set);
   assertNotNull(list);
   assertNotNull(executorService);
   assertNotNull(idGenerator);
   assertNotNull(atomicLong);
   assertNotNull(atomicReference);
   assertNotNull(countDownLatch);
   assertNotNull(semaphore);
   assertNotNull(lock);
   assertEquals("map1", map1.getName());
   assertEquals("map2", map2.getName());
   assertEquals("testMultimap", multiMap.getName());
   assertEquals("testQ", queue.getName());
   assertEquals("testTopic", topic.getName());
   assertEquals("set", set.getName());
   assertEquals("list", list.getName());
   assertEquals("idGenerator", idGenerator.getName());
   assertEquals("atomicLong", atomicLong.getName());
   assertEquals("atomicReference", atomicReference.getName());
   assertEquals("countDownLatch", countDownLatch.getName());
   assertEquals("semaphore", semaphore.getName());
 }
    /*
     * (non-Javadoc)
     *
     * @see
     * org.mpilone.hazelcastmq.core.DefaultHazelcastMQConsumer.ReceiveStrategy
     * #receive(com.hazelcast.core.IQueue)
     */
    @Override
    public byte[] receive(IQueue<byte[]> queue) {
      if (closed || !active) {
        return null;
      }

      return queue.poll();
    }
Exemplo n.º 17
0
  @Test
  public void testOfferPoll() throws IOException, InterruptedException {

    final IQueue q = client.getQueue(queueForTestOfferPoll);

    for (int i = 0; i < 10; i++) {
      boolean result = q.offer("item");
      if (i < maxSizeForQueue) {
        assertTrue(result);
      } else {
        assertFalse(result);
      }
    }
    assertEquals(maxSizeForQueue, q.size());

    final Thread t1 =
        new Thread() {
          public void run() {
            try {
              Thread.sleep(2 * 1000);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            q.poll();
          }
        };
    t1.start();

    boolean result = q.offer("item", 5, TimeUnit.SECONDS);
    assertTrue(result);

    for (int i = 0; i < 10; i++) {
      Object o = q.poll();
      if (i < maxSizeForQueue) {
        assertNotNull(o);
      } else {
        assertNull(o);
      }
    }
    assertEquals(0, q.size());

    final Thread t2 =
        new Thread() {
          public void run() {
            try {
              Thread.sleep(2 * 1000);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            q.offer("item1");
          }
        };
    t2.start();

    Object o = q.poll(5, TimeUnit.SECONDS);
    assertEquals("item1", o);
    t1.join(10000);
    t2.join(10000);
  }
Exemplo n.º 18
0
  @Test
  public void testPartialDrain() {
    final int maxItems = 15;
    final int itemsToDrain = maxItems / 2;

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

    List expectedList = new LinkedList();
    for (int i = 0; i < maxItems; i++) {
      q.offer(i);
      if (i < itemsToDrain) {
        expectedList.add(i);
      }
    }
    List drainedList = new LinkedList();
    int totalDrained = q.drainTo(drainedList, itemsToDrain);

    assertEquals(itemsToDrain, totalDrained);
    assertEquals(expectedList, drainedList);
  }
  /**
   * Starts the consumer which will register a queue or topic listener with Hazelcast and enable
   * message consumption (push or pull). If the consumer is already active, this method does
   * nothing.
   */
  void start() {
    if (active) {
      return;
    }

    receiveLock.lock();
    try {
      // Start listening for events. We currently always listen for events even
      // if we don't have a message listener. If this has a performance impact
      // on Hazelcast we may want to only listen if there is a registered
      // message listener that we need to notify.
      IQueue<byte[]> queue = hazelcastMQContext.resolveQueue(destination);
      if (queue != null) {
        // Get the raw queue outside of any transactional context so we can add
        // an item listener.
        queue = config.getHazelcastInstance().getQueue(queue.getName());
        queueListener = new HzQueueListener(queue);
      }

      // If we are a consumer on a topic, immediately start listening for events
      // so we can buffer them for (a)synchronous consumption.
      ITopic<byte[]> topic = hazelcastMQContext.resolveTopic(destination);
      if (topic != null) {
        topicListener = new HzTopicListener(topic);
      }

      active = true;

      if (messageListener != null) {
        // We have a message listener, so tell the context to drain the dispatch
        // ready queues.
        hazelcastMQContext.onConsumerDispatchReady(id);
      } else {
        // Signal that any receive requests can continue.
        receiveCondition.signalAll();
      }
    } finally {
      receiveLock.unlock();
    }
  }
Exemplo n.º 20
0
 @Override
 public void run() {
   for (int i = 0; i < 100; i++) {
     msg = "Message " + a++;
     logger.info("Produce " + msg);
     queueMessages.put(msg);
     try {
       Thread.sleep(5);
     } catch (InterruptedException e) {
       e.printStackTrace();
     }
   }
 }
    /*
     * (non-Javadoc)
     *
     * @see
     * org.mpilone.hazelcastmq.core.DefaultHazelcastMQConsumer.ReceiveStrategy
     * #receive(com.hazelcast.core.IQueue)
     */
    @Override
    public byte[] receive(IQueue<byte[]> queue) {
      if (closed) {
        return null;
      }

      try {
        if (!active) {
          receiveCondition.await(500L, TimeUnit.MILLISECONDS);
        } else {
          return queue.poll(500L, TimeUnit.MILLISECONDS);
        }
      } catch (InterruptedException ex) {
        // Ignore for now
      }

      return null;
    }
    /*
     * (non-Javadoc)
     *
     * @see
     * com.hazelcast.core.MessageListener#onMessage(com.hazelcast.core.Message)
     */
    @Override
    public void onMessage(Message<byte[]> hzMsg) {
      // We always queue the message even if we have a message listener. We'll
      // immediately pull it out of the queue and dispatch in a separate thread.
      // This is important to prevent slow message handlers from blocking topic
      // distribution in Hazelcast.
      if (!queue.offer(hzMsg.getMessageObject())) {
        log.warn(
            format(
                "In-memory message buffer full for topic [%s]. "
                    + "Messages will be lost. Consider increaing the speed of "
                    + "the consumer or the message buffer.",
                msgTopic.getName()));
        return;
      }

      if (messageListener != null) {
        hazelcastMQContext.onConsumerDispatchReady(id);
      }
    }
    /*
     * (non-Javadoc)
     *
     * @see
     * org.mpilone.hazelcastmq.core.DefaultHazelcastMQConsumer.ReceiveStrategy
     * #receive(com.hazelcast.core.IQueue)
     */
    @Override
    public byte[] receive(IQueue<byte[]> queue) {
      if (closed) {
        return null;
      }

      long t = Math.min(timeout, 500L);
      timeout -= 500L;

      try {
        if (!active) {
          receiveCondition.await(t, TimeUnit.MILLISECONDS);
        } else {
          return queue.poll(t, TimeUnit.MILLISECONDS);
        }
      } catch (InterruptedException ex) {
        // Ignore for now
      }

      return null;
    }
 public void shutdown() {
   msgTopic.removeMessageListener(registrationId);
   queue.clear();
 }
 public void shutdown() {
   queue.removeItemListener(registrationId);
 }
Exemplo n.º 26
0
 @Test
 public void testEmptyPeak() throws InterruptedException {
   final IQueue q = client.getQueue(randomString());
   assertNull(q.peek());
 }
Exemplo n.º 27
0
 @Test
 public void testTake() throws InterruptedException {
   final IQueue q = client.getQueue(randomString());
   q.put(1);
   assertEquals(1, q.take());
 }
Exemplo n.º 28
0
 @Test
 public void testadd() {
   final IQueue q = client.getQueue(randomString());
   assertTrue(q.add(1));
   assertEquals(1, q.size());
 }
Exemplo n.º 29
0
 @Test(expected = UnsupportedOperationException.class)
 public void testGetLocalQueueStats() {
   final IQueue q = client.getQueue(randomString());
   q.getLocalQueueStats();
 }
Exemplo n.º 30
0
 @Test
 public void testNotIsEmpty() {
   final IQueue q = client.getQueue(randomString());
   q.offer(1);
   assertFalse(q.isEmpty());
 }