Exemplo n.º 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()));
  }
Exemplo n.º 2
0
  public void methodReceived(AMQStateManager stateManager, ExchangeDeclareBody body, int channelId)
      throws AMQException {
    AMQProtocolSession session = stateManager.getProtocolSession();
    VirtualHost virtualHost = session.getVirtualHost();
    ExchangeRegistry exchangeRegistry = virtualHost.getExchangeRegistry();
    ExchangeFactory exchangeFactory = virtualHost.getExchangeFactory();
    final AMQChannel channel = session.getChannel(channelId);
    if (channel == null) {
      throw body.getChannelNotFoundException(channelId);
    }

    final AMQShortString exchangeName = body.getExchange();
    if (_logger.isDebugEnabled()) {
      _logger.debug(
          "Request to declare exchange of type " + body.getType() + " with name " + exchangeName);
    }

    synchronized (exchangeRegistry) {
      Exchange exchange = exchangeRegistry.getExchange(exchangeName);

      if (exchange == null) {
        if (body.getPassive() && ((body.getType() == null) || body.getType().length() == 0)) {
          throw body.getChannelException(
              AMQConstant.NOT_FOUND, "Unknown exchange: " + exchangeName);
        } else if (exchangeName.startsWith("amq.")) {
          throw body.getConnectionException(
              AMQConstant.NOT_ALLOWED,
              "Attempt to declare exchange: "
                  + exchangeName
                  + " which begins with reserved prefix 'amq.'.");
        } else if (exchangeName.startsWith("qpid.")) {
          throw body.getConnectionException(
              AMQConstant.NOT_ALLOWED,
              "Attempt to declare exchange: "
                  + exchangeName
                  + " which begins with reserved prefix 'qpid.'.");
        } else {
          try {
            exchange =
                exchangeFactory.createExchange(
                    exchangeName == null ? null : exchangeName.intern(),
                    body.getType() == null ? null : body.getType().intern(),
                    body.getDurable(),
                    body.getAutoDelete(),
                    body.getTicket());
            exchangeRegistry.registerExchange(exchange);

            if (exchange.isDurable()) {
              virtualHost.getMessageStore().createExchange(exchange);
            }
          } catch (AMQUnknownExchangeType e) {
            throw body.getConnectionException(
                AMQConstant.COMMAND_INVALID, "Unknown exchange: " + exchangeName, e);
          }
        }
      } else if (!exchange.getTypeShortString().equals(body.getType())
          && !((body.getType() == null || body.getType().length() == 0) && body.getPassive())) {

        throw new AMQConnectionException(
            AMQConstant.NOT_ALLOWED,
            "Attempt to redeclare exchange: "
                + exchangeName
                + " of type "
                + exchange.getTypeShortString()
                + " to "
                + body.getType()
                + ".",
            body.getClazz(),
            body.getMethod(),
            body.getMajor(),
            body.getMinor(),
            null);
      }
    }
    if (!body.getNowait()) {
      MethodRegistry methodRegistry = session.getMethodRegistry();
      AMQMethodBody responseBody = methodRegistry.createExchangeDeclareOkBody();
      channel.sync();
      session.writeFrame(responseBody.generateFrame(channelId));
    }
  }