Example #1
0
  /*
   This test sends some messages to the queue with subscribers needing message to be acknowledged.
   The messages will not be acknowledged and will be required twice. Why we are checking this is because
   the bug reported said that the queueDepth keeps increasing when messages are requeued.
   // TODO - queue depth now includes unacknowledged messages so does not go down when messages are delivered

   The QueueDepth should decrease when messages are delivered from the queue (QPID-408)
  */
  public void testQueueDepthAlertWithSubscribers() throws Exception {
    AMQChannel channel = new AMQChannel(getSession(), 2, getMessageStore());
    getSession().addChannel(channel);

    // Create queue
    setQueue(getNewQueue());
    Subscription subscription =
        SUBSCRIPTION_FACTORY.createSubscription(
            channel.getChannelId(),
            getSession(),
            new AMQShortString("consumer_tag"),
            true,
            null,
            false,
            channel.getCreditManager());

    getQueue().registerSubscription(subscription, false);

    _queueMBean = (AMQQueueMBean) getQueue().getManagedObject();
    _queueMBean.setMaximumMessageCount(9999l); // Set a high value, because this is not being tested
    _queueMBean.setMaximumQueueDepth(MAX_QUEUE_DEPTH);

    // Send messages(no of message to be little more than what can cause a Queue_Depth alert)
    int messageCount = Math.round(MAX_QUEUE_DEPTH / MAX_MESSAGE_SIZE) + 10;
    long totalSize = (messageCount * MAX_MESSAGE_SIZE);
    sendMessages(channel, messageCount, MAX_MESSAGE_SIZE);

    // Check queueDepth. There should be no messages on the queue and as the subscriber is listening
    // so there should be no Queue_Deoth alert raised
    assertEquals(new Long(totalSize), new Long(_queueMBean.getQueueDepth()));
    Notification lastNotification = _queueMBean.getLastNotification();
    //        assertNull(lastNotification);

    // Kill the subscriber and check for the queue depth values.
    // Messages are unacknowledged, so those should get requeued. All messages should be on the
    // Queue
    getQueue().unregisterSubscription(subscription);
    channel.requeue();

    assertEquals(new Long(totalSize), new Long(_queueMBean.getQueueDepth()));

    lastNotification = _queueMBean.getLastNotification();
    assertNotNull(lastNotification);
    String notificationMsg = lastNotification.getMessage();
    assertTrue(notificationMsg.startsWith(NotificationCheck.QUEUE_DEPTH_ALERT.name()));

    // Connect a consumer again and check QueueDepth values. The queue should get emptied.
    // Messages will get delivered but still are unacknowledged.
    Subscription subscription2 =
        SUBSCRIPTION_FACTORY.createSubscription(
            channel.getChannelId(),
            getSession(),
            new AMQShortString("consumer_tag"),
            true,
            null,
            false,
            channel.getCreditManager());

    getQueue().registerSubscription(subscription2, false);

    while (getQueue().getUndeliveredMessageCount() != 0) {
      Thread.sleep(100);
    }
    //        assertEquals(new Long(0), new Long(_queueMBean.getQueueDepth()));

    // Kill the subscriber again. Now those messages should get requeued again. Check if the queue
    // depth
    // value is correct.
    getQueue().unregisterSubscription(subscription2);
    channel.requeue();

    assertEquals(new Long(totalSize), new Long(_queueMBean.getQueueDepth()));
    getSession().closeSession();

    // Check the clear queue
    _queueMBean.clearQueue();
    assertEquals(new Long(0), new Long(_queueMBean.getQueueDepth()));
  }