コード例 #1
0
  private boolean subscribeSingleTopic(final Subscription newSubscription) {
    subscriptions.add(newSubscription.asClientTopicCouple());

    // scans retained messages to be published to the new subscription
    // TODO this is ugly, it does a linear scan on potential big dataset
    Collection<IMessagesStore.StoredMessage> messages =
        m_messagesStore.searchMatching(
            new IMatchingCondition() {
              public boolean match(String key) {
                return SubscriptionsStore.matchTopics(key, newSubscription.getTopicFilter());
              }
            });

    ClientSession targetSession = m_sessionsStore.sessionForClient(newSubscription.getClientId());
    verifyToActivate(newSubscription.getClientId(), targetSession);
    for (IMessagesStore.StoredMessage storedMsg : messages) {
      // fire the as retained the message
      LOG.debug("send publish message for topic {}", newSubscription.getTopicFilter());
      // forwardPublishQoS0(newSubscription.getClientId(), storedMsg.getTopic(), storedMsg.getQos(),
      // storedMsg.getPayload(), true);
      Integer packetID =
          storedMsg.getQos() == QOSType.MOST_ONE
              ? null
              : m_messagesStore.nextPacketID(newSubscription.getClientId());
      directSend(
          targetSession,
          storedMsg.getTopic(),
          storedMsg.getQos(),
          storedMsg.getPayload(),
          true,
          packetID);
    }

    // notify the Observables
    m_interceptor.notifyTopicSubscribed(newSubscription);
    return true;
  }