@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;
  }
 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);
     }
   }
 }
  protected void sendStreamOfCommands(int[] sequenceNumbers, boolean expected, int expectedCount) {
    for (int i = 0; i < sequenceNumbers.length; i++) {
      int commandId = sequenceNumbers[i];

      ConsumerInfo info = new ConsumerInfo();
      info.setSelector("Cheese: " + commandId);
      info.setCommandId(commandId);

      transport.onCommand(info);
    }

    Queue<Object> exceptions = listener.getExceptions();
    Queue<Object> commands = listener.getCommands();
    if (expected) {
      if (!exceptions.isEmpty()) {
        Exception e = (Exception) exceptions.remove();
        e.printStackTrace();
        fail("Caught exception: " + e);
      }
      assertEquals("number of messages received", expectedCount, commands.size());

      assertEquals("Should have no buffered commands", 0, transport.getBufferedCommandCount());
    } else {
      assertTrue("Should have received an exception!", exceptions.size() > 0);
      Exception e = (Exception) exceptions.remove();
      LOG.info("Caught expected response: " + e);
    }
  }
Пример #4
0
  @Override
  protected DemandSubscription createDemandSubscription(ConsumerInfo info) throws IOException {
    boolean isForcedDurable =
        NetworkBridgeUtils.isForcedDurable(
            info, dynamicallyIncludedDestinations, staticallyIncludedDestinations);

    if (addToAlreadyInterestedConsumers(info, isForcedDurable)) {
      return null; // don't want this subscription added
    }
    // add our original id to ourselves
    info.addNetworkConsumerId(info.getConsumerId());
    ConsumerId forcedDurableId = isForcedDurable ? info.getConsumerId() : null;

    if (info.isDurable() || isForcedDurable) {
      // set the subscriber name to something reproducible
      info.setSubscriptionName(getSubscriberName(info.getDestination()));
      // and override the consumerId with something unique so that it won't
      // be removed if the durable subscriber (at the other end) goes away
      info.setConsumerId(
          new ConsumerId(localSessionInfo.getSessionId(), consumerIdGenerator.getNextSequenceId()));
    }
    info.setSelector(null);
    DemandSubscription demandSubscription = doCreateDemandSubscription(info);
    if (forcedDurableId != null) {
      demandSubscription.addForcedDurableConsumer(forcedDurableId);
      forcedDurableRemoteId.add(forcedDurableId);
    }
    return demandSubscription;
  }
  @Override
  public void browseFinished() {
    MessageDispatch md = new MessageDispatch();
    md.setConsumerId(info.getConsumerId());
    md.setMessage(null);
    md.setDestination(null);

    session.deliverMessage(md);
  }
 public AMQConsumer(
     AMQSession amqSession,
     org.apache.activemq.command.ActiveMQDestination d,
     ConsumerInfo info,
     ScheduledExecutorService scheduledPool) {
   this.session = amqSession;
   this.actualDest = d;
   this.info = info;
   this.scheduledPool = scheduledPool;
   this.prefetchSize = info.getPrefetchSize();
   this.windowAvailable = new AtomicInteger(prefetchSize);
   if (prefetchSize == 0) {
     messagePullHandler = new MessagePullHandler();
   }
 }
  @Override
  protected void populateObject(Object object) throws Exception {
    super.populateObject(object);
    ConsumerInfo info = (ConsumerInfo) object;

    info.setConsumerId(createConsumerId("ConsumerId:1"));
    info.setBrowser(true);
    info.setDestination(createActiveMQDestination("Destination:2"));
    info.setPrefetchSize(1);
    info.setMaximumPendingMessageLimit(2);
    info.setDispatchAsync(false);
    info.setSelector("Selector:3");
    info.setSubscriptionName("SubscriptionName:4");
    info.setNoLocal(true);
    info.setExclusive(false);
    info.setRetroactive(true);
    info.setPriority((byte) 1);
    {
      BrokerId value[] = new BrokerId[2];
      for (int i = 0; i < 2; i++) {
        value[i] = createBrokerId("BrokerPath:5");
      }
      info.setBrokerPath(value);
    }
    info.setAdditionalPredicate(createBooleanExpression("AdditionalPredicate:6"));
    info.setNetworkSubscription(false);
    info.setOptimizedAcknowledge(true);
    info.setNoRangeAcks(false);
    {
      ConsumerId value[] = new ConsumerId[2];
      for (int i = 0; i < 2; i++) {
        value[i] = createConsumerId("NetworkConsumerPath:7");
      }
      info.setNetworkConsumerPath(value);
    }
  }
  public void init() throws Exception {
    AMQServerSession coreSession = session.getCoreSession();

    SimpleString selector =
        info.getSelector() == null ? null : new SimpleString(info.getSelector());

    nativeId = session.getCoreServer().getStorageManager().generateID();

    SimpleString address = new SimpleString(this.actualDest.getPhysicalName());

    if (this.actualDest.isTopic()) {
      String physicalName = this.actualDest.getPhysicalName();
      if (physicalName.contains(".>")) {
        // wildcard
        physicalName = OpenWireUtil.convertWildcard(physicalName);
      }

      // on recreate we don't need to create queues
      address = new SimpleString("jms.topic." + physicalName);
      if (info.isDurable()) {
        subQueueName =
            new SimpleString(
                ActiveMQDestination.createQueueNameForDurableSubscription(
                    true, info.getClientId(), info.getSubscriptionName()));

        QueueQueryResult result = coreSession.executeQueueQuery(subQueueName);
        if (result.isExists()) {
          // Already exists
          if (result.getConsumerCount() > 0) {
            throw new IllegalStateException(
                "Cannot create a subscriber on the durable subscription since it already has subscriber(s)");
          }

          SimpleString oldFilterString = result.getFilterString();

          boolean selectorChanged =
              selector == null && oldFilterString != null
                  || oldFilterString == null && selector != null
                  || oldFilterString != null
                      && selector != null
                      && !oldFilterString.equals(selector);

          SimpleString oldTopicName = result.getAddress();

          boolean topicChanged = !oldTopicName.equals(address);

          if (selectorChanged || topicChanged) {
            // Delete the old durable sub
            coreSession.deleteQueue(subQueueName);

            // Create the new one
            coreSession.createQueue(address, subQueueName, selector, false, true);
          }

        } else {
          coreSession.createQueue(address, subQueueName, selector, false, true);
        }
      } else {
        subQueueName = new SimpleString(UUID.randomUUID().toString());

        coreSession.createQueue(address, subQueueName, selector, true, false);
      }

      coreSession.createConsumer(nativeId, subQueueName, null, info.isBrowser(), false, -1);
    } else {
      SimpleString queueName = new SimpleString("jms.queue." + this.actualDest.getPhysicalName());
      coreSession.createConsumer(nativeId, queueName, selector, info.isBrowser(), false, -1);
    }

    if (info.isBrowser()) {
      AMQServerConsumer coreConsumer = coreSession.getConsumer(nativeId);
      coreConsumer.setBrowserListener(this);
    }
  }
 public ConsumerId getId() {
   return info.getConsumerId();
 }
  /**
   * Un-marshal an object instance from the data input stream
   *
   * @param o the object to un-marshal
   * @param dataIn the data input stream to build the object from
   * @throws IOException
   */
  public void tightUnmarshal(
      OpenWireFormat wireFormat, Object o, DataInput dataIn, BooleanStream bs) throws IOException {
    super.tightUnmarshal(wireFormat, o, dataIn, bs);

    ConsumerInfo info = (ConsumerInfo) o;
    info.setConsumerId(
        (org.apache.activemq.command.ConsumerId) tightUnmarsalCachedObject(wireFormat, dataIn, bs));
    info.setBrowser(bs.readBoolean());
    info.setDestination(
        (org.apache.activemq.command.ActiveMQDestination)
            tightUnmarsalCachedObject(wireFormat, dataIn, bs));
    info.setPrefetchSize(dataIn.readInt());
    info.setMaximumPendingMessageLimit(dataIn.readInt());
    info.setDispatchAsync(bs.readBoolean());
    info.setSelector(tightUnmarshalString(dataIn, bs));
    info.setSubscriptionName(tightUnmarshalString(dataIn, bs));
    info.setNoLocal(bs.readBoolean());
    info.setExclusive(bs.readBoolean());
    info.setRetroactive(bs.readBoolean());
    info.setPriority(dataIn.readByte());

    if (bs.readBoolean()) {
      short size = dataIn.readShort();
      org.apache.activemq.command.BrokerId value[] = new org.apache.activemq.command.BrokerId[size];
      for (int i = 0; i < size; i++) {
        value[i] =
            (org.apache.activemq.command.BrokerId)
                tightUnmarsalNestedObject(wireFormat, dataIn, bs);
      }
      info.setBrokerPath(value);
    } else {
      info.setBrokerPath(null);
    }
    info.setAdditionalPredicate(
        (org.apache.activemq.filter.BooleanExpression)
            tightUnmarsalNestedObject(wireFormat, dataIn, bs));
    info.setNetworkSubscription(bs.readBoolean());
    info.setOptimizedAcknowledge(bs.readBoolean());
    info.setNoRangeAcks(bs.readBoolean());

    if (bs.readBoolean()) {
      short size = dataIn.readShort();
      org.apache.activemq.command.ConsumerId value[] =
          new org.apache.activemq.command.ConsumerId[size];
      for (int i = 0; i < size; i++) {
        value[i] =
            (org.apache.activemq.command.ConsumerId)
                tightUnmarsalNestedObject(wireFormat, dataIn, bs);
      }
      info.setNetworkConsumerPath(value);
    } else {
      info.setNetworkConsumerPath(null);
    }
  }
  /** Write the booleans that this object uses to a BooleanStream */
  public void looseMarshal(OpenWireFormat wireFormat, Object o, DataOutput dataOut)
      throws IOException {

    ConsumerInfo info = (ConsumerInfo) o;

    super.looseMarshal(wireFormat, o, dataOut);
    looseMarshalCachedObject(wireFormat, (DataStructure) info.getConsumerId(), dataOut);
    dataOut.writeBoolean(info.isBrowser());
    looseMarshalCachedObject(wireFormat, (DataStructure) info.getDestination(), dataOut);
    dataOut.writeInt(info.getPrefetchSize());
    dataOut.writeInt(info.getMaximumPendingMessageLimit());
    dataOut.writeBoolean(info.isDispatchAsync());
    looseMarshalString(info.getSelector(), dataOut);
    looseMarshalString(info.getSubscriptionName(), dataOut);
    dataOut.writeBoolean(info.isNoLocal());
    dataOut.writeBoolean(info.isExclusive());
    dataOut.writeBoolean(info.isRetroactive());
    dataOut.writeByte(info.getPriority());
    looseMarshalObjectArray(wireFormat, info.getBrokerPath(), dataOut);
    looseMarshalNestedObject(wireFormat, (DataStructure) info.getAdditionalPredicate(), dataOut);
    dataOut.writeBoolean(info.isNetworkSubscription());
    dataOut.writeBoolean(info.isOptimizedAcknowledge());
    dataOut.writeBoolean(info.isNoRangeAcks());
    looseMarshalObjectArray(wireFormat, info.getNetworkConsumerPath(), dataOut);
  }
  /**
   * Write a object instance to data output stream
   *
   * @param o the instance to be marshaled
   * @param dataOut the output stream
   * @throws IOException thrown if an error occurs
   */
  public void tightMarshal2(
      OpenWireFormat wireFormat, Object o, DataOutput dataOut, BooleanStream bs)
      throws IOException {
    super.tightMarshal2(wireFormat, o, dataOut, bs);

    ConsumerInfo info = (ConsumerInfo) o;
    tightMarshalCachedObject2(wireFormat, (DataStructure) info.getConsumerId(), dataOut, bs);
    bs.readBoolean();
    tightMarshalCachedObject2(wireFormat, (DataStructure) info.getDestination(), dataOut, bs);
    dataOut.writeInt(info.getPrefetchSize());
    dataOut.writeInt(info.getMaximumPendingMessageLimit());
    bs.readBoolean();
    tightMarshalString2(info.getSelector(), dataOut, bs);
    tightMarshalString2(info.getSubscriptionName(), dataOut, bs);
    bs.readBoolean();
    bs.readBoolean();
    bs.readBoolean();
    dataOut.writeByte(info.getPriority());
    tightMarshalObjectArray2(wireFormat, info.getBrokerPath(), dataOut, bs);
    tightMarshalNestedObject2(
        wireFormat, (DataStructure) info.getAdditionalPredicate(), dataOut, bs);
    bs.readBoolean();
    bs.readBoolean();
    bs.readBoolean();
    tightMarshalObjectArray2(wireFormat, info.getNetworkConsumerPath(), dataOut, bs);
  }
  /** Write the booleans that this object uses to a BooleanStream */
  public int tightMarshal1(OpenWireFormat wireFormat, Object o, BooleanStream bs)
      throws IOException {

    ConsumerInfo info = (ConsumerInfo) o;

    int rc = super.tightMarshal1(wireFormat, o, bs);
    rc += tightMarshalCachedObject1(wireFormat, (DataStructure) info.getConsumerId(), bs);
    bs.writeBoolean(info.isBrowser());
    rc += tightMarshalCachedObject1(wireFormat, (DataStructure) info.getDestination(), bs);
    bs.writeBoolean(info.isDispatchAsync());
    rc += tightMarshalString1(info.getSelector(), bs);
    rc += tightMarshalString1(info.getSubscriptionName(), bs);
    bs.writeBoolean(info.isNoLocal());
    bs.writeBoolean(info.isExclusive());
    bs.writeBoolean(info.isRetroactive());
    rc += tightMarshalObjectArray1(wireFormat, info.getBrokerPath(), bs);
    rc += tightMarshalNestedObject1(wireFormat, (DataStructure) info.getAdditionalPredicate(), bs);
    bs.writeBoolean(info.isNetworkSubscription());
    bs.writeBoolean(info.isOptimizedAcknowledge());
    bs.writeBoolean(info.isNoRangeAcks());
    rc += tightMarshalObjectArray1(wireFormat, info.getNetworkConsumerPath(), bs);

    return rc + 9;
  }