public void testFlowControlProperties() throws Exception {
    assertTrue(_queueMBean.getCapacity() == 0);
    assertTrue(_queueMBean.getFlowResumeCapacity() == 0);
    assertFalse(_queueMBean.isFlowOverfull());

    // capacity currently 0, try setting FlowResumeCapacity above this
    try {
      _queueMBean.setFlowResumeCapacity(1L);
      fail("Should have failed to allow setting FlowResumeCapacity above Capacity");
    } catch (IllegalArgumentException ex) {
      // expected exception
      assertTrue(_queueMBean.getFlowResumeCapacity() == 0);
    }

    // add a message to the queue
    sendMessages(1, true);

    // (FlowResume)Capacity currently 0, set both to 2
    _queueMBean.setCapacity(2L);
    assertTrue(_queueMBean.getCapacity() == 2L);
    _queueMBean.setFlowResumeCapacity(2L);
    assertTrue(_queueMBean.getFlowResumeCapacity() == 2L);

    // Try setting Capacity below FlowResumeCapacity
    try {
      _queueMBean.setCapacity(1L);
      fail("Should have failed to allow setting Capacity below FlowResumeCapacity");
    } catch (IllegalArgumentException ex) {
      // expected exception
      assertTrue(_queueMBean.getCapacity() == 2);
    }

    // create a channel and use it to exercise the capacity check mechanism
    AMQChannel channel = new AMQChannel(getSession(), 1, getMessageStore());
    getQueue().checkCapacity(channel);

    assertTrue(_queueMBean.isFlowOverfull());
    assertTrue(channel.getBlocking());

    // set FlowResumeCapacity to MESSAGE_SIZE and check queue is now underfull and channel unblocked
    _queueMBean.setCapacity(MESSAGE_SIZE); // must increase capacity too
    _queueMBean.setFlowResumeCapacity(MESSAGE_SIZE);

    assertFalse(_queueMBean.isFlowOverfull());
    assertFalse(channel.getBlocking());
  }
 /** {@inheritDoc} */
 @Override
 public void forcefullyDisconnect() throws AndesException {
   try {
     channel.mgmtClose();
   } catch (AMQException e) {
     throw new AndesException(e);
   }
 }
  public void methodReceived(AMQStateManager stateManager, BasicRecoverBody body, int channelId)
      throws AMQException {
    AMQProtocolSession session = stateManager.getProtocolSession();

    _logger.debug("Recover received on protocol session " + session + " and channel " + channelId);
    AMQChannel channel = session.getChannel(channelId);

    if (channel == null) {
      throw body.getChannelNotFoundException(channelId);
    }

    channel.resend(body.getRequeue());

    // Qpid 0-8 hacks a synchronous -ok onto recover.
    // In Qpid 0-9 we create a separate sync-recover, sync-recover-ok pair to be "more" compliant
    if (session.getProtocolVersion().equals(ProtocolVersion.v8_0)) {
      MethodRegistry_8_0 methodRegistry = (MethodRegistry_8_0) session.getMethodRegistry();
      AMQMethodBody recoverOk = methodRegistry.createBasicRecoverOkBody();
      session.writeFrame(recoverOk.generateFrame(channelId));
    }
  }
  public void testConsumerCount() throws AMQException {

    assertTrue(getQueue().getActiveConsumerCount() == 0);
    assertTrue(_queueMBean.getActiveConsumerCount() == 0);

    InternalTestProtocolSession protocolSession = new InternalTestProtocolSession(getVirtualHost());

    AMQChannel channel = new AMQChannel(protocolSession, 1, getMessageStore());
    protocolSession.addChannel(channel);

    Subscription subscription =
        SUBSCRIPTION_FACTORY.createSubscription(
            channel.getChannelId(),
            protocolSession,
            new AMQShortString("test"),
            false,
            null,
            false,
            channel.getCreditManager());

    getQueue().registerSubscription(subscription, false);
    assertEquals(1, (int) _queueMBean.getActiveConsumerCount());

    SubscriptionFactory subscriptionFactory = SUBSCRIPTION_FACTORY;
    Subscription s1 =
        subscriptionFactory.createSubscription(
            channel.getChannelId(),
            protocolSession,
            new AMQShortString("S1"),
            false,
            null,
            true,
            channel.getCreditManager());

    Subscription s2 =
        subscriptionFactory.createSubscription(
            channel.getChannelId(),
            protocolSession,
            new AMQShortString("S2"),
            false,
            null,
            true,
            channel.getCreditManager());
    getQueue().registerSubscription(s1, false);
    getQueue().registerSubscription(s2, false);
    assertTrue(_queueMBean.getActiveConsumerCount() == 3);
    assertTrue(_queueMBean.getConsumerCount() == 3);

    s1.close();
    assertEquals(2, (int) _queueMBean.getActiveConsumerCount());
    assertTrue(_queueMBean.getConsumerCount() == 3);
  }
  public void methodReceived(AMQStateManager stateManager, QueueDeclareBody body, int channelId)
      throws AMQException {
    final AMQProtocolSession protocolConnection = stateManager.getProtocolSession();
    final AMQSessionModel session = protocolConnection.getChannel(channelId);
    VirtualHost virtualHost = protocolConnection.getVirtualHost();
    ExchangeRegistry exchangeRegistry = virtualHost.getExchangeRegistry();
    QueueRegistry queueRegistry = virtualHost.getQueueRegistry();
    DurableConfigurationStore store = virtualHost.getDurableConfigurationStore();

    final AMQShortString queueName;

    // if we aren't given a queue name, we create one which we return to the client
    if ((body.getQueue() == null) || (body.getQueue().length() == 0)) {
      queueName = createName();
    } else {
      queueName = body.getQueue().intern();
    }

    AMQQueue queue;

    // TODO: do we need to check that the queue already exists with exactly the same
    // "configuration"?

    synchronized (queueRegistry) {
      queue = queueRegistry.getQueue(queueName);

      AMQSessionModel owningSession = null;

      if (queue != null) {
        owningSession = queue.getExclusiveOwningSession();
      }

      if (queue == null) {
        if (body.getPassive()) {
          String msg = "Queue: " + queueName + " not found on VirtualHost(" + virtualHost + ").";
          throw body.getChannelException(AMQConstant.NOT_FOUND, msg);
        } else {
          queue = createQueue(queueName, body, virtualHost, protocolConnection);
          queue.setAuthorizationHolder(protocolConnection);
          if (queue.isDurable() && !queue.isAutoDelete()) {
            store.createQueue(queue, body.getArguments());

            // Tell Andes kernel to create queue
            QpidAndesBridge.createQueue(queue);
          }
          if (body.getAutoDelete()) {
            queue.setDeleteOnNoConsumers(true);
          }
          queueRegistry.registerQueue(queue);
          if (body.getExclusive()) {
            queue.setExclusiveOwningSession(protocolConnection.getChannel(channelId));
            queue.setAuthorizationHolder(protocolConnection);

            if (!body.getDurable()) {
              final AMQQueue q = queue;
              final AMQProtocolSession.Task sessionCloseTask =
                  new AMQProtocolSession.Task() {
                    public void doTask(AMQProtocolSession session) throws AMQException {
                      q.setExclusiveOwningSession(null);
                    }
                  };
              protocolConnection.addSessionCloseTask(sessionCloseTask);
              queue.addQueueDeleteTask(
                  new AMQQueue.Task() {
                    public void doTask(AMQQueue queue) throws AMQException {
                      protocolConnection.removeSessionCloseTask(sessionCloseTask);
                    }
                  });
            }
          }
          if (autoRegister) {
            Exchange defaultExchange = exchangeRegistry.getDefaultExchange();

            virtualHost
                .getBindingFactory()
                .addBinding(
                    String.valueOf(queueName), queue, defaultExchange, Collections.EMPTY_MAP);
            _logger.info(
                "Queue "
                    + queueName
                    + " bound to default exchange("
                    + defaultExchange.getNameShortString()
                    + ")");
          }
        }
      } else if (queue.isExclusive()
          && !queue.isDurable()
          && (owningSession == null || owningSession.getConnectionModel() != protocolConnection)) {
        throw body.getConnectionException(
            AMQConstant.NOT_ALLOWED,
            "Queue "
                + queue.getNameShortString()
                + " is exclusive, but not created on this Connection.");
      } else if (!body.getPassive() && ((queue.isExclusive()) != body.getExclusive())) {

        throw body.getChannelException(
            AMQConstant.ALREADY_EXISTS,
            "Cannot re-declare queue '"
                + queue.getNameShortString()
                + "' with different exclusivity (was: "
                + queue.isExclusive()
                + " requested "
                + body.getExclusive()
                + ")");
      } else if (!body.getPassive()
          && body.getExclusive()
          && !(queue.isDurable()
              ? String.valueOf(queue.getOwner()).equals(session.getClientID())
              : (owningSession == null
                  || owningSession.getConnectionModel() == protocolConnection))) {
        throw body.getChannelException(
            AMQConstant.ALREADY_EXISTS,
            "Cannot declare queue('"
                + queueName
                + "'), "
                + "as exclusive queue with same name "
                + "declared on another client ID('"
                + queue.getOwner()
                + "') your clientID('"
                + session.getClientID()
                + "')");

      } else if (!body.getPassive() && queue.isAutoDelete() != body.getAutoDelete()) {
        throw body.getChannelException(
            AMQConstant.ALREADY_EXISTS,
            "Cannot re-declare queue '"
                + queue.getNameShortString()
                + "' with different auto-delete (was: "
                + queue.isAutoDelete()
                + " requested "
                + body.getAutoDelete()
                + ")");
      } else if (!body.getPassive() && queue.isDurable() != body.getDurable()) {
        throw body.getChannelException(
            AMQConstant.ALREADY_EXISTS,
            "Cannot re-declare queue '"
                + queue.getNameShortString()
                + "' with different durability (was: "
                + queue.isDurable()
                + " requested "
                + body.getDurable()
                + ")");
      }

      AMQChannel channel = protocolConnection.getChannel(channelId);

      if (channel == null) {
        throw body.getChannelNotFoundException(channelId);
      }

      // set this as the default queue on the channel:
      channel.setDefaultQueue(queue);
    }

    if (!body.getNowait()) {
      MethodRegistry methodRegistry = protocolConnection.getMethodRegistry();
      QueueDeclareOkBody responseBody =
          methodRegistry.createQueueDeclareOkBody(
              queueName, queue.getMessageCount(), queue.getConsumerCount());
      protocolConnection.writeFrame(responseBody.generateFrame(channelId));

      _logger.info("Queue " + queueName + " declared successfully");
    }
  }
 @Override
 public UUID getChannelID() {
   return channel.getId();
 }