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(); }
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; } } }