예제 #1
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;
  }
  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);
    }
  }