public void addConsumerBrokerExchange(
     ConsumerId id, AMQSession amqSession, Map<ActiveMQDestination, AMQConsumer> consumerMap) {
   AMQConsumerBrokerExchange result = consumerExchanges.get(id);
   if (result == null) {
     if (consumerMap.size() == 1) {
       result =
           new AMQSingleConsumerBrokerExchange(amqSession, consumerMap.values().iterator().next());
     } else {
       result = new AMQCompositeConsumerBrokerExchange(amqSession, consumerMap);
     }
     synchronized (consumerExchanges) {
       result.setConnectionContext(context);
       SessionState ss = state.getSessionState(id.getParentId());
       if (ss != null) {
         ConsumerState cs = ss.getConsumerState(id);
         if (cs != null) {
           ConsumerInfo info = cs.getInfo();
           if (info != null) {
             if (info.getDestination() != null && info.getDestination().isPattern()) {
               result.setWildcard(true);
             }
           }
         }
       }
       consumerExchanges.put(id, result);
     }
   }
 }
  @Override
  public Response processRemoveConsumer(ConsumerId id, long lastDeliveredSequenceId)
      throws Exception {
    SessionId sessionId = id.getParentId();
    SessionState ss = state.getSessionState(sessionId);
    if (ss == null) {
      throw new IllegalStateException(
          "Cannot remove a consumer from a session that had not been registered: " + sessionId);
    }
    ConsumerState consumerState = ss.removeConsumer(id);
    if (consumerState == null) {
      throw new IllegalStateException(
          "Cannot remove a consumer that had not been registered: " + id);
    }
    ConsumerInfo info = consumerState.getInfo();
    info.setLastDeliveredSequenceId(lastDeliveredSequenceId);

    AMQConsumerBrokerExchange consumerBrokerExchange = consumerExchanges.get(id);

    consumerBrokerExchange.removeConsumer();

    removeConsumerBrokerExchange(id);

    return null;
  }
 @Override
 public Response processRemoveSession(SessionId id, long lastDeliveredSequenceId)
     throws Exception {
   SessionState session = state.getSessionState(id);
   if (session == null) {
     throw new IllegalStateException("Cannot remove session that had not been registered: " + id);
   }
   // Don't let new consumers or producers get added while we are closing
   // this down.
   session.shutdown();
   // Cascade the connection stop to the consumers and producers.
   for (ConsumerId consumerId : session.getConsumerIds()) {
     try {
       processRemoveConsumer(consumerId, lastDeliveredSequenceId);
     } catch (Throwable e) {
       // LOG.warn("Failed to remove consumer: {}", consumerId, e);
     }
   }
   for (ProducerId producerId : session.getProducerIds()) {
     try {
       processRemoveProducer(producerId);
     } catch (Throwable e) {
       // LOG.warn("Failed to remove producer: {}", producerId, e);
     }
   }
   state.removeSession(id);
   protocolManager.removeSession(context, session.getInfo());
   return null;
 }
 private AMQProducerBrokerExchange getProducerBrokerExchange(ProducerId id) throws IOException {
   AMQProducerBrokerExchange result = producerExchanges.get(id);
   if (result == null) {
     synchronized (producerExchanges) {
       result = new AMQProducerBrokerExchange();
       result.setConnectionContext(context);
       // todo implement reconnect https://issues.apache.org/jira/browse/ARTEMIS-194
       if (context.isReconnect()
           || (context.isNetworkConnection() && this.acceptorUsed.isAuditNetworkProducers())) {
         if (protocolManager.getPersistenceAdapter() != null) {
           result.setLastStoredSequenceId(
               protocolManager.getPersistenceAdapter().getLastProducerSequenceId(id));
         }
       }
       SessionState ss = state.getSessionState(id.getParentId());
       if (ss != null) {
         result.setProducerState(ss.getProducerState(id));
         ProducerState producerState = ss.getProducerState(id);
         if (producerState != null && producerState.getInfo() != null) {
           ProducerInfo info = producerState.getInfo();
           result.setMutable(info.getDestination() == null || info.getDestination().isComposite());
         }
       }
       producerExchanges.put(id, result);
     }
   }
   return result;
 }
 public int getProducerCount() {
   int result = 0;
   for (SessionId sessionId : state.getSessionIds()) {
     SessionState sessionState = state.getSessionState(sessionId);
     if (sessionState != null) {
       result += sessionState.getProducerIds().size();
     }
   }
   return result;
 }