Пример #1
0
  public void acknowledge(String messageID, String subscriptionID) throws Exception {
    long id = Long.parseLong(messageID);
    Pair<Long, Integer> pair = messagesToAck.remove(id);

    if (pair == null) {
      throw BUNDLE.failToAckMissingID(id);
    }

    long consumerID = pair.getA();
    int credits = pair.getB();

    StompSubscription sub = subscriptions.get(consumerID);

    if (subscriptionID != null) {
      if (!sub.getID().equals(subscriptionID)) {
        throw BUNDLE.subscriptionIDMismatch(subscriptionID, sub.getID());
      }
    }

    if (this.consumerCredits != -1) {
      session.receiveConsumerCredits(consumerID, credits);
    }

    if (sub.getAck().equals(Stomp.Headers.Subscribe.AckModeValues.CLIENT_INDIVIDUAL)) {
      session.individualAcknowledge(consumerID, id);
    } else {
      session.acknowledge(consumerID, id);
    }

    session.commit();
  }
Пример #2
0
 boolean containsSubscription(String subscriptionID) {
   Iterator<Entry<Long, StompSubscription>> iterator = subscriptions.entrySet().iterator();
   while (iterator.hasNext()) {
     Map.Entry<Long, StompSubscription> entry = iterator.next();
     StompSubscription sub = entry.getValue();
     if (sub.getID().equals(subscriptionID)) {
       return true;
     }
   }
   return false;
 }
Пример #3
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;
 }
Пример #4
0
  public int sendMessage(ServerMessage serverMessage, long consumerID, int deliveryCount) {
    LargeServerMessageImpl largeMessage = null;
    ServerMessage newServerMessage = serverMessage;
    try {
      StompSubscription subscription = subscriptions.get(consumerID);
      StompFrame frame = null;
      if (serverMessage.isLargeMessage()) {
        newServerMessage = serverMessage.copy();

        largeMessage = (LargeServerMessageImpl) serverMessage;
        BodyEncoder encoder = largeMessage.getBodyEncoder();
        encoder.open();
        int bodySize = (int) encoder.getLargeBodySize();

        // large message doesn't have a body.
        ((ServerMessageImpl) newServerMessage).createBody(bodySize);
        encoder.encode(newServerMessage.getBodyBuffer(), bodySize);
        encoder.close();
      }

      if (serverMessage.getBooleanProperty(Message.HDR_LARGE_COMPRESSED)) {
        // decompress
        HornetQBuffer qbuff = newServerMessage.getBodyBuffer();
        int bytesToRead = qbuff.writerIndex() - MessageImpl.BODY_OFFSET;
        Inflater inflater = new Inflater();
        inflater.setInput(qbuff.readBytes(bytesToRead).toByteBuffer().array());

        // get the real size of large message
        long sizeBody = newServerMessage.getLongProperty(Message.HDR_LARGE_BODY_SIZE);

        byte[] data = new byte[(int) sizeBody];
        inflater.inflate(data);
        inflater.end();
        qbuff.resetReaderIndex();
        qbuff.resetWriterIndex();
        qbuff.writeBytes(data);
      }

      frame = connection.createStompMessage(newServerMessage, subscription, deliveryCount);

      int length = frame.getEncodedSize();

      if (subscription.getAck().equals(Stomp.Headers.Subscribe.AckModeValues.AUTO)) {
        if (manager.send(connection, frame)) {
          // we ack and commit only if the send is successful
          session.acknowledge(consumerID, newServerMessage.getMessageID());
          session.commit();
        }
      } else {
        messagesToAck.put(
            newServerMessage.getMessageID(), new Pair<Long, Integer>(consumerID, length));
        // Must send AFTER adding to messagesToAck - or could get acked from client BEFORE it's been
        // added!
        manager.send(connection, frame);
      }

      return length;
    } catch (Exception e) {
      return 0;
    } finally {
      if (largeMessage != null) {
        largeMessage.releaseResources();
        largeMessage = null;
      }
    }
  }