Exemplo n.º 1
0
  public void addSubscription(
      long consumerID,
      String subscriptionID,
      String clientID,
      String durableSubscriptionName,
      String destination,
      String selector,
      String ack)
      throws Exception {
    SimpleString queue = SimpleString.toSimpleString(destination);
    int receiveCredits = consumerCredits;
    if (ack.equals(Stomp.Headers.Subscribe.AckModeValues.AUTO)) {
      receiveCredits = -1;
    }

    if (destination.startsWith("jms.topic")) {
      // subscribes to a topic
      if (durableSubscriptionName != null) {
        if (clientID == null) {
          throw BUNDLE.missingClientID();
        }
        queue = SimpleString.toSimpleString(clientID + "." + durableSubscriptionName);
        QueueQueryResult query = session.executeQueueQuery(queue);
        if (!query.isExists()) {
          session.createQueue(
              SimpleString.toSimpleString(destination),
              queue,
              SimpleString.toSimpleString(selector),
              false,
              true);
        }
      } else {
        queue = UUIDGenerator.getInstance().generateSimpleStringUUID();
        session.createQueue(
            SimpleString.toSimpleString(destination),
            queue,
            SimpleString.toSimpleString(selector),
            true,
            false);
      }
      ((ServerSessionImpl) session)
          .createConsumer(consumerID, queue, null, false, false, receiveCredits);
    } else {
      ((ServerSessionImpl) session)
          .createConsumer(
              consumerID,
              queue,
              SimpleString.toSimpleString(selector),
              false,
              false,
              receiveCredits);
    }

    StompSubscription subscription = new StompSubscription(subscriptionID, ack);
    subscriptions.put(consumerID, subscription);

    session.start();
  }
Exemplo n.º 2
0
 public boolean unsubscribe(String id, String durableSubscriptionName) throws Exception {
   Iterator<Entry<Long, StompSubscription>> iterator = subscriptions.entrySet().iterator();
   while (iterator.hasNext()) {
     Map.Entry<Long, StompSubscription> entry = iterator.next();
     long consumerID = entry.getKey();
     StompSubscription sub = entry.getValue();
     if (id != null && id.equals(sub.getID())) {
       iterator.remove();
       session.closeConsumer(consumerID);
       SimpleString queueName;
       if (durableSubscriptionName != null && durableSubscriptionName.trim().length() != 0) {
         queueName = SimpleString.toSimpleString(id + "." + durableSubscriptionName);
       } else {
         queueName = SimpleString.toSimpleString(id);
       }
       QueueQueryResult query = session.executeQueueQuery(queueName);
       if (query.isExists()) {
         session.deleteQueue(queueName);
       }
       return true;
     }
   }
   return false;
 }