Java Code Examples for org.apache.qpid.proton.engine.Receiver#advance()

The following examples show how to use org.apache.qpid.proton.engine.Receiver#advance() . 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: Recv.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void onDelivery(Event event) {
    Receiver recv = (Receiver)event.getLink();
    Delivery delivery = recv.current();
    if (delivery.isReadable() && !delivery.isPartial()) {
        int size = delivery.pending();
        byte[] buffer = new byte[size];
        int read = recv.recv(buffer, 0, buffer.length);
        recv.advance();

        Message msg = Proton.message();
        msg.decode(buffer, 0, read);
        System.out.println(((AmqpValue)msg.getBody()).getValue());
    }
}
 
Example 2
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 3
Source File: ProtonReceiverImpl.java    From vertx-proton with Apache License 2.0 5 votes vote down vote up
private void handleAborted(Receiver receiver, Delivery delivery) {
  splitContent = null;

  receiver.advance();
  delivery.settle();

  if(!receiver.getDrain()) {
    flow(1, false);
  } else {
    processForDrainCompletion();
  }
}
 
Example 4
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
private Delivery verifyDeliveryRawPayload(Receiver receiver, String deliveryTag, byte[] payload)
{
    Delivery delivery = receiver.current();

    assertTrue(Arrays.equals(deliveryTag.getBytes(StandardCharsets.UTF_8), delivery.getTag()));

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

    byte[] received = new byte[delivery.pending()];
    int len = receiver.recv(received, 0, BUFFER_SIZE);

    assertEquals("unexpected length", len, received.length);

    assertArrayEquals("Received delivery payload not as expected", payload, received);

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

    return delivery;
}
 
Example 5
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeliveryIdTrackingHandlesAbortedDelivery() {
    MockTransportImpl transport = new MockTransportImpl();
    transport.setEmitFlowEventOnSend(false);
    Connection connection = Proton.connection();
    transport.bind(connection);

    connection.open();

    Session session = connection.session();
    session.open();

    String linkName1 = "myReceiver1";
    Receiver receiver1 = session.receiver(linkName1);
    receiver1.flow(5);
    receiver1.open();

    pumpMockTransport(transport);

    final UnsignedInteger r1handle = UnsignedInteger.ZERO;

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 4, transport.writes.size());

    // Give the necessary responses to open/begin/attach
    transport.handleFrame(new TransportFrame(0, new Open(), null));

    Begin begin = new Begin();
    begin.setRemoteChannel(UnsignedShort.valueOf((short) 0));
    begin.setNextOutgoingId(UnsignedInteger.ONE);
    begin.setIncomingWindow(UnsignedInteger.valueOf(1024));
    begin.setOutgoingWindow(UnsignedInteger.valueOf(1024));
    transport.handleFrame(new TransportFrame(0, begin, null));

    Attach attach1 = new Attach();
    attach1.setHandle(r1handle);
    attach1.setRole(Role.SENDER);
    attach1.setName(linkName1);
    attach1.setInitialDeliveryCount(UnsignedInteger.ZERO);
    transport.handleFrame(new TransportFrame(0, attach1, null));

    // Receive first transfer for a multi-frame delivery
    assertEquals("Unexpected queued count", 0, receiver1.getQueued());
    handlePartialTransfer(transport, r1handle, UnsignedInteger.ZERO, "tag1", new byte[]{ 1 }, true);
    assertEquals("Unexpected queued count", 1, receiver1.getQueued());
    // Receive second transfer for a multi-frame delivery, indicating it is aborted
    handlePartialTransfer(transport, r1handle, UnsignedInteger.ZERO, "tag1", new byte[]{ 2 }, true, true);
    assertEquals("Unexpected queued count", 1, receiver1.getQueued());

    // Receive first transfer for ANOTHER delivery, expect it not to fail, since the earlier delivery aborted
    handlePartialTransfer(transport, r1handle, UnsignedInteger.ONE, "tag2", new byte[]{ 3 }, false);
    assertEquals("Unexpected queued count", 2, receiver1.getQueued());

    receiver1.advance();
    verifyDeliveryRawPayload(receiver1, "tag2", new byte[] { 3 });
}
 
Example 6
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);
   }

}
 
Example 7
Source File: ProtonReceiverImpl.java    From vertx-proton with Apache License 2.0 4 votes vote down vote up
void onDelivery() {
  if (this.handler == null) {
    return;
  }

  Receiver receiver = getReceiver();
  Delivery delivery = receiver.current();

  if (delivery != null) {

    if(delivery.isAborted()) {
      handleAborted(receiver, delivery);
      return;
    }

    if (delivery.isPartial()) {
      handlePartial(receiver, delivery);

      // Delivery is not yet completely received,
      // return and allow further frames to arrive.
      return;
    }

    // Complete prior partial content if needed, or grab it all.
    ReadableBuffer data = receiver.recv();
    if(splitContent != null) {
      data = completePartial(data);
    }

    receiver.advance();

    MessageImpl msg = (MessageImpl) Proton.message();
    ProtonDeliveryImpl delImpl = new ProtonDeliveryImpl(delivery);
    try {
      msg.decode(data);
    } catch (Throwable t) {
      LOG.debug("Unable to decode message, undeliverable", t);

      handleDecodeFailure(receiver, delImpl);
      return;
    }

    handler.handle(delImpl, msg);

    if (autoAccept && delivery.getLocalState() == null) {
      accepted(delImpl, true);
    }

    if (prefetch > 0) {
      // Replenish credit if prefetch is configured.
      // TODO: batch credit replenish, optionally flush if exceeding a given threshold?
      flow(1, false);
    } else {
      processForDrainCompletion();
    }
  }
}
 
Example 8
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 3 votes vote down vote up
private Delivery verifyDelivery(Receiver receiver, String deliveryTag, String messageContent)
{
    Delivery delivery = receiver.current();

    assertTrue(Arrays.equals(deliveryTag.getBytes(StandardCharsets.UTF_8), delivery.getTag()));

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

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

    byte[] received = new byte[BUFFER_SIZE];
    int len = receiver.recv(received, 0, BUFFER_SIZE);

    assertTrue("given array was too small", len < BUFFER_SIZE);

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

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

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

    return delivery;
}