Esempio n. 1
0
 @Override
 public void onLinkFlow(Event event) {
   Sender snd = (Sender) event.getLink();
   if (snd.getCredit() > 0 && snd.getLocalState() != EndpointState.CLOSED) {
     Message message = Proton.message();
     ++count;
     message.setBody(new AmqpValue("message-" + count));
     byte[] msgData = new byte[1024];
     int length;
     while (true) {
       try {
         length = message.encode(msgData, 0, msgData.length);
         break;
       } catch (BufferOverflowException e) {
         msgData = new byte[msgData.length * 2];
       }
     }
     byte[] tag = String.valueOf(count).getBytes();
     Delivery dlv = snd.delivery(tag);
     snd.send(msgData, 0, length);
     dlv.settle();
     snd.advance();
     if (count == numMsgs) {
       snd.close();
       snd.getSession().close();
       snd.getSession().getConnection().close();
       result = true;
     }
   }
 }
Esempio n. 2
0
    @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);

        ++count;
        String msgBody = ((AmqpValue) msg.getBody()).getValue().toString();
        String expected = "message-" + count;
        if (!expected.equals(msgBody)) {
          throw new RuntimeException(
              "Received message body '" + msgBody + "', expected: '" + expected + "'");
        }

        if (count == numMsgs) {
          recv.close();
          recv.getSession().close();
          recv.getSession().getConnection().close();
          acceptor.close();
        }
      }
    }
Esempio n. 3
0
 void setDispositionAndSettleIfRequired(IncommingMessage msg, DeliveryState state) {
   Delivery d = _unsettled.get(msg.getSequence());
   d.disposition(state);
   if (d.getLink().getReceiverSettleMode() == ReceiverSettleMode.FIRST) {
     d.settle();
     ((ReceiverImpl) d.getLink().getContext()).decrementUnsettledCount();
   }
 }
 @Override
 public Link getLink() {
   if (delivery.getLink() instanceof Sender) {
     return new UnmodifiableSender((Sender) delivery.getLink());
   } else if (delivery.getLink() instanceof Receiver) {
     return new UnmodifiableReceiver((Receiver) delivery.getLink());
   } else {
     throw new IllegalStateException("Delivery has unknown link type");
   }
 }
Esempio n. 5
0
  @Override
  protected void processDelivery(Delivery delivery) {
    final MessageDelivery md = (MessageDelivery) delivery.getContext();
    if (delivery.remotelySettled()) {
      if (delivery.getTag().length > 0) {
        checkinTag(delivery.getTag());
      }

      final DeliveryState state = delivery.getRemoteState();
      if (state == null || state instanceof Accepted) {
        if (!delivery.remotelySettled()) {
          delivery.disposition(new Accepted());
        }
      } else if (state instanceof Rejected) {
        // re-deliver /w incremented delivery counter.
        md.delivery = null;
        md.incrementDeliveryCount();
        outbound.addLast(md);
      } else if (state instanceof Released) {
        // re-deliver && don't increment the counter.
        md.delivery = null;
        outbound.addLast(md);
      } else if (state instanceof Modified) {
        Modified modified = (Modified) state;
        if (modified.getDeliveryFailed()) {
          // increment delivery counter..
          md.incrementDeliveryCount();
        }
      }
      delivery.settle();
    }
    md.fireWatches();
  }
Esempio n. 6
0
  protected void pumpDeliveries() {
    assertExecuting();
    try {
      while (true) {
        while (currentBuffer != null) {
          if (sender.getCredit() > 0) {
            int sent = sender.send(currentBuffer.data, currentBuffer.offset, currentBuffer.length);
            currentBuffer.moveHead(sent);
            if (currentBuffer.length == 0) {
              Delivery current = currentDelivery;
              MessageDelivery md = (MessageDelivery) current.getContext();
              currentBuffer = null;
              currentDelivery = null;
              if (qos == QoS.AT_MOST_ONCE) {
                current.settle();
              } else {
                sender.advance();
              }
              md.fireWatches();
            }
          } else {
            return;
          }
        }

        if (outbound.isEmpty()) {
          return;
        }

        final MessageDelivery md = outbound.removeFirst();
        outboundBufferSize -= md.initialSize;
        currentBuffer = md.encoded();
        if (qos == QoS.AT_MOST_ONCE) {
          currentDelivery = sender.delivery(EMPTY_BYTE_ARRAY, 0, 0);
        } else {
          final byte[] tag = nextTag();
          currentDelivery = sender.delivery(tag, 0, tag.length);
        }
        md.delivery = currentDelivery;
        currentDelivery.setContext(md);
      }
    } finally {
      fireWatches();
    }
  }
 @Override
 public DeliveryState getLocalState() {
   return delivery.getLocalState();
 }
 @Override
 public byte[] getTag() {
   return delivery.getTag();
 }
 @Override
 public boolean isBuffered() {
   return delivery.isBuffered();
 }
 @Override
 public Delivery getWorkNext() {
   return new UnmodifiableDelivery(delivery.getWorkNext());
 }
 @Override
 public boolean isSettled() {
   return delivery.isSettled();
 }
 @Override
 public Object getContext() {
   return delivery.getContext();
 }
 @Override
 public boolean isReadable() {
   return delivery.isReadable();
 }
 @Override
 public boolean isWritable() {
   return delivery.isWritable();
 }
 @Override
 public DeliveryState getRemoteState() {
   return delivery.getRemoteState();
 }
 @Override
 public int getMessageFormat() {
   return delivery.getMessageFormat();
 }
 @Override
 public boolean isUpdated() {
   return delivery.isUpdated();
 }
 @Override
 public boolean remotelySettled() {
   return delivery.remotelySettled();
 }
 @Override
 public boolean isPartial() {
   return delivery.isPartial();
 }
 @Override
 public Delivery next() {
   return new UnmodifiableDelivery(delivery.next());
 }
 @Override
 public int pending() {
   return delivery.pending();
 }