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

The following examples show how to use org.apache.qpid.proton.engine.Delivery#disposition() . 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: ProtonTransactionRefsOperation.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
public void rollbackRedelivery(Transaction txn, MessageReference ref, long timeBase, Map<QueueImpl, LinkedList<MessageReference>> queueMap) throws Exception {
   ProtonTransactionImpl tx = (ProtonTransactionImpl) txn;

   if (tx.getDeliveries().containsKey(ref)) {
      Delivery del = tx.getDeliveries().get(ref).getA();
      ServerConsumer consumer = (ServerConsumer) tx.getDeliveries().get(ref).getB().getBrokerConsumer();
      // Rollback normally if the delivery is not settled or a forced TX rollback is done (e.g. connection drop).
      if (del.remotelySettled() || !tx.isDischarged()) {
         super.rollbackRedelivery(tx, ref, timeBase, queueMap);
      } else {
         ref.incrementDeliveryCount();
         consumer.backToDelivering(ref);
         del.disposition(del.getLocalState() == null ? del.getDefaultDeliveryState() : del.getLocalState());
      }
   } else {
      super.rollbackRedelivery(tx, ref, timeBase, queueMap);
   }
}
 
Example 3
Source File: AmqpReceiver.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected void deliveryFailed(Delivery incoming, boolean expandCredit) {
   Modified disposition = new Modified();
   disposition.setUndeliverableHere(true);
   disposition.setDeliveryFailed(true);
   incoming.disposition(disposition);
   incoming.settle();
   if (expandCredit) {
      getEndpoint().flow(1);
   }
}
 
Example 4
Source File: AmqpSender.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private void doSend(AmqpMessage message, AsyncResult request, AmqpTransactionId txId) throws Exception {
   LOG.trace("Producer sending message: {}", message);

   Delivery delivery = null;
   if (presettle) {
      delivery = getEndpoint().delivery(EMPTY_BYTE_ARRAY, 0, 0);
   } else {
      byte[] tag = tagGenerator.getNextTag();
      delivery = getEndpoint().delivery(tag, 0, tag.length);
   }

   delivery.setContext(request);

   Binary amqpTxId = null;
   if (txId != null) {
      amqpTxId = txId.getRemoteTxId();
   } else if (session.isInTransaction()) {
      amqpTxId = session.getTransactionId().getRemoteTxId();
   }

   if (amqpTxId != null) {
      TransactionalState state = new TransactionalState();
      state.setTxnId(amqpTxId);
      delivery.disposition(state);
   }

   encodeAndSend(message.getWrappedMessage(), delivery);

   if (presettle) {
      delivery.settle();
      request.onSuccess();
   } else {
      pending.add(delivery);
      getEndpoint().advance();
   }
}
 
Example 5
Source File: AmqpConsumer.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
private void handleAccepted(JmsInboundMessageDispatch envelope, Delivery delivery) {
    LOG.debug("Accepted Ack of message: {}", envelope);
    if (!delivery.remotelySettled()) {
        if (session.isTransacted() && !getResourceInfo().isBrowser()) {

            if (session.isTransactionFailed()) {
                LOG.trace("Skipping ack of message {} in failed transaction.", envelope);
                return;
            }

            Binary txnId = session.getTransactionContext().getAmqpTransactionId();
            if (txnId != null) {
                delivery.disposition(session.getTransactionContext().getTxnAcceptState());
                delivery.settle();
                session.getTransactionContext().registerTxConsumer(this);
            }
        } else {
            delivery.disposition(Accepted.getInstance());
            delivery.settle();
        }
    } else {
        delivery.settle();
    }

    if (envelope.isDelivered()) {
        deliveredCount--;
    }
    dispatchedCount--;
}
 
Example 6
Source File: AmqpConsumer.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
private void handleDisposition(JmsInboundMessageDispatch envelope, Delivery delivery, DeliveryState outcome) {
    delivery.disposition(outcome);
    delivery.settle();
    if (envelope.isDelivered()) {
        deliveredCount--;
    }
    dispatchedCount--;
}
 
Example 7
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
/**
 * Verify that no Disposition frame is emitted by the Transport should a Delivery
 * have disposition applied after the Close frame was sent.
 */
@Test
public void testDispositionAfterCloseSent()
{
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();
    transport.bind(connection);

    connection.open();

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

    String linkName = "myReceiver";
    Receiver receiver = session.receiver(linkName);
    receiver.flow(5);
    receiver.open();

    pumpMockTransport(transport);

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

    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);
    assertTrue("Unexpected frame type", transport.writes.get(1) instanceof Begin);
    assertTrue("Unexpected frame type", transport.writes.get(2) instanceof Attach);
    assertTrue("Unexpected frame type", transport.writes.get(3) instanceof Flow);

    Delivery delivery = receiver.current();
    assertNull("Should not yet have a delivery", delivery);

    // Send the necessary responses to open/begin/attach as well as a transfer
    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 attach = new Attach();
    attach.setHandle(UnsignedInteger.ZERO);
    attach.setRole(Role.SENDER);
    attach.setName(linkName);
    attach.setInitialDeliveryCount(UnsignedInteger.ZERO);
    transport.handleFrame(new TransportFrame(0, attach, null));

    String deliveryTag = "tag1";
    String messageContent = "content1";
    handleTransfer(transport, 1, deliveryTag, messageContent);

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

    delivery = verifyDelivery(receiver, deliveryTag, messageContent);
    assertNotNull("Should now have a delivery", delivery);

    // Cause the Close frame to be sent
    connection.close();
    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 5, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(4) instanceof Close);

    delivery.disposition(Released.getInstance());
    delivery.settle();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 5, transport.writes.size());
}
 
Example 8
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
/**
 * Verify that no Disposition frame is emitted by the Transport should a Delivery
 * have disposition applied after the delivery has been settled previously.
 */
@Test
public void testNoDispositionUpdatesAfterSettlementProceessedSender()
{
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();
    transport.bind(connection);

    connection.open();

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

    String linkName = "myReceiver";
    Receiver receiver = session.receiver(linkName);
    receiver.flow(5);
    receiver.open();

    pumpMockTransport(transport);

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

    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);
    assertTrue("Unexpected frame type", transport.writes.get(1) instanceof Begin);
    assertTrue("Unexpected frame type", transport.writes.get(2) instanceof Attach);
    assertTrue("Unexpected frame type", transport.writes.get(3) instanceof Flow);

    Delivery delivery = receiver.current();
    assertNull("Should not yet have a delivery", delivery);

    // Send the necessary responses to open/begin/attach as well as a transfer
    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 attach = new Attach();
    attach.setHandle(UnsignedInteger.ZERO);
    attach.setRole(Role.SENDER);
    attach.setName(linkName);
    attach.setInitialDeliveryCount(UnsignedInteger.ZERO);
    transport.handleFrame(new TransportFrame(0, attach, null));

    String deliveryTag = "tag1";
    String messageContent = "content1";
    handleTransfer(transport, 1, deliveryTag, messageContent);

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

    delivery = verifyDelivery(receiver, deliveryTag, messageContent);
    assertNotNull("Should now have a delivery", delivery);

    delivery.disposition(Accepted.getInstance());
    delivery.settle();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 5, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(4) instanceof Disposition);

    // Should not produce any new frames being written
    delivery.disposition(Accepted.getInstance());

    connection.close();
    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 6, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(5) instanceof Close);
}
 
Example 9
Source File: TransportImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
/**
 * Verify that no Disposition frame is emitted by the Transport should a Delivery
 * have disposition applied after the delivery has been settled previously.
 */
@Test
public void testNoDispositionUpdatesAfterSettlementProceessedReceiver()
{
    MockTransportImpl transport = new MockTransportImpl();
    Connection connection = Proton.connection();
    transport.bind(connection);

    connection.open();

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

    String linkName = "myReceiver";
    Sender sender = session.sender(linkName);
    sender.open();

    pumpMockTransport(transport);

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

    assertTrue("Unexpected frame type", transport.writes.get(0) instanceof Open);
    assertTrue("Unexpected frame type", transport.writes.get(1) instanceof Begin);
    assertTrue("Unexpected frame type", transport.writes.get(2) instanceof Attach);

    // Send the necessary responses to open/begin/attach as well as a transfer
    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 attach = new Attach();
    attach.setHandle(UnsignedInteger.ZERO);
    attach.setRole(Role.RECEIVER);
    attach.setName(linkName);
    attach.setInitialDeliveryCount(UnsignedInteger.ZERO);
    transport.handleFrame(new TransportFrame(0, attach, null));

    int credit = 1;
    Flow flow = new Flow();
    flow.setHandle(UnsignedInteger.ZERO);
    flow.setDeliveryCount(UnsignedInteger.ZERO);
    flow.setNextIncomingId(UnsignedInteger.ONE);
    flow.setNextOutgoingId(UnsignedInteger.ZERO);
    flow.setIncomingWindow(UnsignedInteger.valueOf(1024));
    flow.setOutgoingWindow(UnsignedInteger.valueOf(1024));
    flow.setDrain(true);
    flow.setLinkCredit(UnsignedInteger.valueOf(credit));
    transport.handleFrame(new TransportFrame(0, flow, null));

    Delivery delivery = sendMessage(sender, "tag1", "content1");

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 4, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(3) instanceof Transfer);

    delivery.disposition(Accepted.getInstance());
    delivery.settle();

    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 5, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(4) instanceof Disposition);

    // Should not produce any new frames being written
    delivery.disposition(Accepted.getInstance());

    connection.close();
    pumpMockTransport(transport);

    assertEquals("Unexpected frames written: " + getFrameTypesWritten(transport), 6, transport.writes.size());
    assertTrue("Unexpected frame type", transport.writes.get(5) instanceof Close);
}
 
Example 10
Source File: AmqpFixedProducer.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
private void doSend(JmsOutboundMessageDispatch envelope, InFlightSend send) throws ProviderException {
    LOG.trace("Producer sending message: {}", envelope);

    boolean presettle = envelope.isPresettle() || isPresettle();
    Delivery delivery = null;

    if (presettle) {
        delivery = getEndpoint().delivery(EMPTY_BYTE_ARRAY, 0, 0);
    } else {
        byte[] tag = tagGenerator.getNextTag();
        delivery = getEndpoint().delivery(tag, 0, tag.length);
    }

    if (session.isTransacted()) {
        AmqpTransactionContext context = session.getTransactionContext();
        delivery.disposition(context.getTxnEnrolledState());
        context.registerTxProducer(this);
    }

    // Write the already encoded AMQP message into the Sender
    ByteBuf encoded = (ByteBuf) envelope.getPayload();
    getEndpoint().sendNoCopy(new AmqpReadableBuffer(encoded.duplicate()));

    AmqpProvider provider = getParent().getProvider();

    if (!presettle && getSendTimeout() != JmsConnectionInfo.INFINITE && send.requestTimeout == null) {
        send.requestTimeout = getParent().getProvider().scheduleRequestTimeout(send, getSendTimeout(), send);
    }

    if (presettle) {
        delivery.settle();
    } else {
        sent.put(envelope.getMessageId(), send);
        getEndpoint().advance();
    }

    send.setDelivery(delivery);
    delivery.setContext(send);

    // Put it on the wire and let it fail if the connection is broken, if it does
    // get written then continue on to determine when we should complete it.
    if (provider.pumpToProtonTransport(send, false)) {
        // For presettled messages we can just mark as successful and we are done, but
        // for any other message we still track it until the remote settles.  If the send
        // was tagged as asynchronous we must mark the original request as complete but
        // we still need to wait for the disposition before we can consider the send as
        // having been successful.
        if (presettle) {
            send.onSuccess();
        } else if (envelope.isSendAsync()) {
            send.getOriginalRequest().onSuccess();
        }

        try {
            provider.getTransport().flush();
        } catch (Throwable ex) {
            throw ProviderExceptionSupport.createOrPassthroughFatal(ex);
        }
    }
}
 
Example 11
Source File: AmqpConsumer.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
private boolean processDelivery(Delivery incoming) throws Exception {
    JmsMessage message = null;
    try {
        message = AmqpCodec.decodeMessage(this, getEndpoint().recv()).asJmsMessage();
    } catch (Exception e) {
        LOG.warn("Error on transform: {}", e.getMessage());
        // TODO - We could signal provider error but not sure we want to fail
        //        the connection just because we can't convert the message.
        //        In the future once the JMS mapping is complete we should be
        //        able to convert everything to some message even if its just
        //        a bytes messages as a fall back.
        incoming.disposition(MODIFIED_FAILED_UNDELIVERABLE);
        incoming.settle();
        // TODO: this flows credit, which we might not want, e.g if
        // a drain was issued to stop the link.
        sendFlowIfNeeded();
        return false;
    }

    try {
        // Let the message do any final processing before sending it onto a consumer.
        // We could defer this to a later stage such as the JmsConnection or even in
        // the JmsMessageConsumer dispatch method if we needed to.
        message.onDispatch();

        JmsInboundMessageDispatch envelope = new JmsInboundMessageDispatch(getNextIncomingSequenceNumber());
        envelope.setMessage(message);
        envelope.setConsumerId(getResourceInfo().getId());
        envelope.setConsumerInfo(getResourceInfo());
        // Store link to delivery in the hint for use in acknowledge requests.
        envelope.setProviderHint(incoming);
        envelope.setMessageId(message.getFacade().getProviderMessageIdObject());

        // Store reference to envelope in delivery context for recovery
        incoming.setContext(envelope);

        deliver(envelope);

        return true;
    } finally {
        getEndpoint().advance();
    }
}