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

The following examples show how to use org.apache.qpid.proton.engine.Receiver#recv() . 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: Server.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Override
public void onDelivery(Event evt) {
    Delivery dlv = evt.getDelivery();
    Link link = dlv.getLink();
    if (link instanceof Sender) {
        dlv.settle();
    } else {
        Receiver rcv = (Receiver) link;
        if (!dlv.isPartial()) {
            byte[] bytes = new byte[dlv.pending()];
            rcv.recv(bytes, 0, bytes.length);
            String address = router.getAddress(rcv);
            Message message = new Message(bytes);
            messages.put(address, message);
            dlv.disposition(Accepted.getInstance());
            dlv.settle();
            if (!quiet) {
                System.out.println(String.format("Got message(%s): %s", address, message));
            }
            send(address);
        }
    }
}
 
Example 2
Source File: Drain.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Override
public void onDelivery(Event evt) {
    Delivery dlv = evt.getDelivery();
    if (dlv.getLink() instanceof Receiver) {
        Receiver receiver = (Receiver) dlv.getLink();

        if (!dlv.isPartial()) {
            byte[] bytes = new byte[dlv.pending()];
            receiver.recv(bytes, 0, bytes.length);
            Message msg = new Message(bytes);

            if (!quiet) {
                System.out.println(String.format("Got message: %s", msg));
            }
            received++;
            dlv.settle();
        }

        if ((received >= count) || (!block && !receiver.draining())) {
            receiver.getSession().getConnection().close();
        }
    }
}
 
Example 3
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 4
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 5
Source File: ProtonServerReceiverContext.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private void initializeCurrentLargeMessage(Delivery delivery, Receiver receiver) throws Exception {
   long id = sessionSPI.getStorageManager().generateID();
   currentLargeMessage = new AMQPLargeMessage(id, delivery.getMessageFormat(), null, sessionSPI.getCoreMessageObjectPools(), sessionSPI.getStorageManager());

   ReadableBuffer dataBuffer = receiver.recv();
   currentLargeMessage.parseHeader(dataBuffer);

   sessionSPI.getStorageManager().largeMessageCreated(id, currentLargeMessage);
   currentLargeMessage.addBytes(dataBuffer);
}
 
Example 6
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 7
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 8
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 9
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 10
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;
}