Java Code Examples for org.apache.qpid.proton.engine.Delivery#available()

The following examples show how to use org.apache.qpid.proton.engine.Delivery#available() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeferredSettlementTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
private Delivery receiveMessageFromServer(String deliveryTag, int count)
{
    Delivery delivery = getClient().getConnection().getWorkHead();
    Receiver clientReceiver = getClient().getReceiver();

    assertTrue(Arrays.equals(deliveryTag.getBytes(StandardCharsets.UTF_8), delivery.getTag()));
    assertEquals("The received delivery should be on our receiver",
            clientReceiver, delivery.getLink());

    assertNull(delivery.getLocalState());
    assertNull(delivery.getRemoteState());

    assertFalse(delivery.isPartial());
    assertTrue(delivery.isReadable());

    int size = delivery.available();
    byte[] received = new byte[size];

    int len = clientReceiver.recv(received, 0, size);

    assertEquals("Should have received " + size + " bytes", size, len);
    assertEquals("Should be no bytes left", 0, delivery.available());

    Message m = Proton.message();
    m.decode(received, 0, len);

    Object messageBody = ((AmqpValue)m.getBody()).getValue();
    assertEquals("Unexpected message content", count, messageBody);

    boolean receiverAdvanced = clientReceiver.advance();
    assertTrue("receiver has not advanced", receiverAdvanced);

    delivery.setContext(count);

    return delivery;
}
 
Example 2
Source File: ProtonReceiverImpl.java    From vertx-proton with Apache License 2.0 5 votes vote down vote up
private void handlePartial(final Receiver receiver, final Delivery delivery) {
  if (sessionIncomingCapacity <= 0 || maxFrameSize <= 0 || session.getIncomingBytes() < windowFullThreshhold) {
    // No window, or there is still capacity, so do nothing.
  } else {
    // The session window could be effectively full, we need to
    // read part of the delivery content to ensure there is
    // room made for receiving more of the delivery.
    if(delivery.available() > 0) {
      ReadableBuffer buff = receiver.recv();

      if(splitContent == null && buff instanceof CompositeReadableBuffer) {
        // Its a composite and there is no prior partial content, use it.
        splitContent = (CompositeReadableBuffer) buff;
      } else {
        int remaining = buff.remaining();
        if(remaining > 0) {
          if (splitContent == null) {
            splitContent = new CompositeReadableBuffer();
          }

          byte[] chunk = new byte[remaining];
          buff.get(chunk);

          splitContent.append(chunk);
        }
      }
    }
  }
}
 
Example 3
Source File: ProtonServerReceiverContext.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Override
public void onMessage(Delivery delivery) throws ActiveMQAMQPException {
   connection.requireInHandler();
   Receiver receiver = ((Receiver) delivery.getLink());

   if (receiver.current() != delivery) {
      return;
   }

   try {
      if (delivery.isAborted()) {
         clearLargeMessage();

         // Aborting implicitly remotely settles, so advance
         // receiver to the next delivery and settle locally.
         receiver.advance();
         delivery.settle();

         // Replenish the credit if not doing a drain
         if (!receiver.getDrain()) {
            receiver.flow(1);
         }

         return;
      } else if (delivery.isPartial()) {
         if (sessionSPI.getStorageManager() instanceof NullStorageManager) {
            // if we are dealing with the NullStorageManager we should just make it a regular message anyways
            return;
         }

         if (currentLargeMessage == null) {
            // minLargeMessageSize < 0 means no large message treatment, make it disabled
            if (minLargeMessageSize > 0 && delivery.available() >= minLargeMessageSize) {
               initializeCurrentLargeMessage(delivery, receiver);
            }
         } else {
            currentLargeMessage.addBytes(receiver.recv());
         }

         return;
      }

      AMQPMessage message;

      // this is treating the case where the frameSize > minLargeMessage and the message is still large enough
      if (!(sessionSPI.getStorageManager() instanceof NullStorageManager) && currentLargeMessage == null && minLargeMessageSize > 0 && delivery.available() >= minLargeMessageSize) {
         initializeCurrentLargeMessage(delivery, receiver);
      }

      if (currentLargeMessage != null) {
         currentLargeMessage.addBytes(receiver.recv());
         receiver.advance();
         currentLargeMessage.finishParse();
         message = currentLargeMessage;
         currentLargeMessage = null;
      } else {
         ReadableBuffer data = receiver.recv();
         receiver.advance();
         message = sessionSPI.createStandardMessage(delivery, data);
      }

      Transaction tx = null;
      if (delivery.getRemoteState() instanceof TransactionalState) {
         TransactionalState txState = (TransactionalState) delivery.getRemoteState();
         tx = this.sessionSPI.getTransaction(txState.getTxnId(), false);
      }

      actualDelivery(message, delivery, receiver, tx);
   } catch (Exception e) {
      throw new ActiveMQAMQPInternalErrorException(e.getMessage(), e);
   }

}