Search in sources :

Example 1 with Sender

use of org.apache.qpid.proton.engine.Sender in project azure-service-bus-java by Azure.

the class CoreMessageSender method createSendLink.

private void createSendLink() {
    TRACE_LOGGER.info("Creating send link to '{}'", this.sendPath);
    final Connection connection = this.underlyingFactory.getConnection();
    final Session session = connection.session();
    session.setOutgoingWindow(Integer.MAX_VALUE);
    session.open();
    BaseHandler.setHandler(session, new SessionHandler(sendPath));
    final String sendLinkNamePrefix = StringUtil.getShortRandomString();
    final String sendLinkName = !StringUtil.isNullOrEmpty(connection.getRemoteContainer()) ? sendLinkNamePrefix.concat(TrackingUtil.TRACKING_ID_TOKEN_SEPARATOR).concat(connection.getRemoteContainer()) : sendLinkNamePrefix;
    final Sender sender = session.sender(sendLinkName);
    final Target target = new Target();
    target.setAddress(sendPath);
    sender.setTarget(target);
    final Source source = new Source();
    sender.setSource(source);
    SenderSettleMode settleMode = SenderSettleMode.UNSETTLED;
    TRACE_LOGGER.debug("Send link settle mode '{}'", settleMode);
    sender.setSenderSettleMode(settleMode);
    Map<Symbol, Object> linkProperties = new HashMap<>();
    // ServiceBus expects timeout to be of type unsignedint
    linkProperties.put(ClientConstants.LINK_TIMEOUT_PROPERTY, UnsignedInteger.valueOf(Util.adjustServerTimeout(this.underlyingFactory.getOperationTimeout()).toMillis()));
    sender.setProperties(linkProperties);
    SendLinkHandler handler = new SendLinkHandler(CoreMessageSender.this);
    BaseHandler.setHandler(sender, handler);
    sender.open();
    this.sendLink = sender;
}
Also used : SessionHandler(com.microsoft.azure.servicebus.amqp.SessionHandler) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Symbol(org.apache.qpid.proton.amqp.Symbol) Connection(org.apache.qpid.proton.engine.Connection) SendLinkHandler(com.microsoft.azure.servicebus.amqp.SendLinkHandler) SenderSettleMode(org.apache.qpid.proton.amqp.transport.SenderSettleMode) Source(org.apache.qpid.proton.amqp.messaging.Source) IAmqpSender(com.microsoft.azure.servicebus.amqp.IAmqpSender) Sender(org.apache.qpid.proton.engine.Sender) Target(org.apache.qpid.proton.amqp.messaging.Target) Session(org.apache.qpid.proton.engine.Session)

Example 2 with Sender

use of org.apache.qpid.proton.engine.Sender in project azure-service-bus-java by Azure.

the class CoreMessageSender method processSendWork.

// actual send on the SenderLink should happen only in this method & should run on Reactor Thread
private void processSendWork() {
    synchronized (this.pendingSendLock) {
        if (!this.isSendLoopRunning) {
            this.isSendLoopRunning = true;
        } else {
            return;
        }
    }
    TRACE_LOGGER.debug("Processing pending sends to '{}'. Available link credit '{}'", this.sendPath, this.linkCredit);
    try {
        if (!this.ensureLinkIsOpen().isDone()) {
            // Link recreation is pending
            return;
        }
        final Sender sendLinkCurrent = this.sendLink;
        while (sendLinkCurrent != null && sendLinkCurrent.getLocalState() == EndpointState.ACTIVE && sendLinkCurrent.getRemoteState() == EndpointState.ACTIVE && this.linkCredit > 0) {
            final WeightedDeliveryTag deliveryTag;
            final SendWorkItem<Void> sendData;
            synchronized (this.pendingSendLock) {
                deliveryTag = this.pendingSends.poll();
                if (deliveryTag == null) {
                    TRACE_LOGGER.debug("There are no pending sends to '{}'.", this.sendPath);
                    // Must be done inside this synchronized block
                    this.isSendLoopRunning = false;
                    break;
                } else {
                    sendData = this.pendingSendsData.get(deliveryTag.getDeliveryTag());
                    if (sendData == null) {
                        TRACE_LOGGER.warn("SendData not found for this delivery. path:{}, linkName:{}, deliveryTag:{}", this.sendPath, this.sendLink.getName(), deliveryTag);
                        continue;
                    }
                }
            }
            if (sendData.getWork() != null && sendData.getWork().isDone()) {
                // CoreSend could enqueue Sends into PendingSends Queue and can fail the SendCompletableFuture
                // (when It fails to schedule the ProcessSendWork on reactor Thread)
                this.pendingSendsData.remove(sendData);
                continue;
            }
            Delivery delivery = null;
            boolean linkAdvance = false;
            int sentMsgSize = 0;
            Exception sendException = null;
            try {
                delivery = sendLinkCurrent.delivery(deliveryTag.getDeliveryTag().getBytes());
                delivery.setMessageFormat(sendData.getMessageFormat());
                TRACE_LOGGER.debug("Sending message delivery '{}' to '{}'", deliveryTag.getDeliveryTag(), this.sendPath);
                sentMsgSize = sendLinkCurrent.send(sendData.getMessage(), 0, sendData.getEncodedMessageSize());
                assert sentMsgSize == sendData.getEncodedMessageSize() : "Contract of the ProtonJ library for Sender.Send API changed";
                linkAdvance = sendLinkCurrent.advance();
            } catch (Exception exception) {
                sendException = exception;
            }
            if (linkAdvance) {
                this.linkCredit--;
                sendData.setWaitingForAck();
            } else {
                TRACE_LOGGER.warn("Sendlink advance failed. path:{}, linkName:{}, deliveryTag:{}, sentMessageSize:{}, payloadActualSiz:{}", this.sendPath, this.sendLink.getName(), deliveryTag, sentMsgSize, sendData.getEncodedMessageSize());
                if (delivery != null) {
                    delivery.free();
                }
                Exception completionException = sendException != null ? new OperationCancelledException("Send operation failed. Please see cause for more details", sendException) : new OperationCancelledException(String.format(Locale.US, "Send operation failed while advancing delivery(tag: %s) on SendLink(path: %s).", this.sendPath, deliveryTag));
                AsyncUtil.completeFutureExceptionally(sendData.getWork(), completionException);
            }
        }
    } finally {
        synchronized (this.pendingSendLock) {
            if (this.isSendLoopRunning) {
                this.isSendLoopRunning = false;
            }
        }
    }
}
Also used : IAmqpSender(com.microsoft.azure.servicebus.amqp.IAmqpSender) Sender(org.apache.qpid.proton.engine.Sender) Delivery(org.apache.qpid.proton.engine.Delivery) IOException(java.io.IOException)

Example 3 with Sender

use of org.apache.qpid.proton.engine.Sender in project azure-service-bus-java by Azure.

the class RequestResponseLink method createInternalLinks.

private void createInternalLinks() {
    Map<Symbol, Object> commonLinkProperties = new HashMap<>();
    // ServiceBus expects timeout to be of type unsignedint
    commonLinkProperties.put(ClientConstants.LINK_TIMEOUT_PROPERTY, UnsignedInteger.valueOf(Util.adjustServerTimeout(this.underlyingFactory.getOperationTimeout()).toMillis()));
    // Create send link
    final Connection connection = this.underlyingFactory.getConnection();
    Session session = connection.session();
    session.setOutgoingWindow(Integer.MAX_VALUE);
    session.open();
    BaseHandler.setHandler(session, new SessionHandler(this.linkPath));
    String sendLinkNamePrefix = "RequestResponseLink-Sender".concat(TrackingUtil.TRACKING_ID_TOKEN_SEPARATOR).concat(StringUtil.getShortRandomString());
    String sendLinkName = !StringUtil.isNullOrEmpty(connection.getRemoteContainer()) ? sendLinkNamePrefix.concat(TrackingUtil.TRACKING_ID_TOKEN_SEPARATOR).concat(connection.getRemoteContainer()) : sendLinkNamePrefix;
    Sender sender = session.sender(sendLinkName);
    Target sednerTarget = new Target();
    sednerTarget.setAddress(this.linkPath);
    sender.setTarget(sednerTarget);
    Source senderSource = new Source();
    senderSource.setAddress(this.replyTo);
    sender.setSource(senderSource);
    sender.setSenderSettleMode(SenderSettleMode.SETTLED);
    sender.setProperties(commonLinkProperties);
    SendLinkHandler sendLinkHandler = new SendLinkHandler(this.amqpSender);
    BaseHandler.setHandler(sender, sendLinkHandler);
    this.amqpSender.setSendLink(sender);
    TRACE_LOGGER.debug("RequestReponseLink - opening send link to {}", this.linkPath);
    sender.open();
    // Create receive link
    session = connection.session();
    session.setOutgoingWindow(Integer.MAX_VALUE);
    session.open();
    BaseHandler.setHandler(session, new SessionHandler(this.linkPath));
    String receiveLinkNamePrefix = "RequestResponseLink-Receiver".concat(TrackingUtil.TRACKING_ID_TOKEN_SEPARATOR).concat(StringUtil.getShortRandomString());
    String receiveLinkName = !StringUtil.isNullOrEmpty(connection.getRemoteContainer()) ? receiveLinkNamePrefix.concat(TrackingUtil.TRACKING_ID_TOKEN_SEPARATOR).concat(connection.getRemoteContainer()) : receiveLinkNamePrefix;
    Receiver receiver = session.receiver(receiveLinkName);
    Source receiverSource = new Source();
    receiverSource.setAddress(this.linkPath);
    receiver.setSource(receiverSource);
    Target receiverTarget = new Target();
    receiverTarget.setAddress(this.replyTo);
    receiver.setTarget(receiverTarget);
    // Set settle modes
    receiver.setSenderSettleMode(SenderSettleMode.SETTLED);
    receiver.setReceiverSettleMode(ReceiverSettleMode.FIRST);
    receiver.setProperties(commonLinkProperties);
    final ReceiveLinkHandler receiveLinkHandler = new ReceiveLinkHandler(this.amqpReceiver);
    BaseHandler.setHandler(receiver, receiveLinkHandler);
    this.amqpReceiver.setReceiveLink(receiver);
    TRACE_LOGGER.debug("RequestReponseLink - opening receive link to {}", this.linkPath);
    receiver.open();
}
Also used : SessionHandler(com.microsoft.azure.servicebus.amqp.SessionHandler) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Symbol(org.apache.qpid.proton.amqp.Symbol) Connection(org.apache.qpid.proton.engine.Connection) Receiver(org.apache.qpid.proton.engine.Receiver) IAmqpReceiver(com.microsoft.azure.servicebus.amqp.IAmqpReceiver) SendLinkHandler(com.microsoft.azure.servicebus.amqp.SendLinkHandler) Source(org.apache.qpid.proton.amqp.messaging.Source) IAmqpSender(com.microsoft.azure.servicebus.amqp.IAmqpSender) Sender(org.apache.qpid.proton.engine.Sender) Target(org.apache.qpid.proton.amqp.messaging.Target) ReceiveLinkHandler(com.microsoft.azure.servicebus.amqp.ReceiveLinkHandler) Session(org.apache.qpid.proton.engine.Session)

Example 4 with Sender

use of org.apache.qpid.proton.engine.Sender in project azure-service-bus-java by Azure.

the class SendLinkHandler method onLinkFlow.

@Override
public void onLinkFlow(Event event) {
    if (this.isFirstFlow) {
        synchronized (this.firstFlow) {
            if (this.isFirstFlow) {
                this.msgSender.onOpenComplete(null);
                this.isFirstFlow = false;
            }
        }
    }
    Sender sender = event.getSender();
    this.msgSender.onFlow(sender.getRemoteCredit());
    TRACE_LOGGER.debug("onLinkFlow: linkName:{}, unsettled:{}, credit:{}", sender.getName(), sender.getUnsettled(), sender.getCredit());
}
Also used : Sender(org.apache.qpid.proton.engine.Sender)

Example 5 with Sender

use of org.apache.qpid.proton.engine.Sender in project activemq-artemis by apache.

the class AmqpTransactionCoordinator method sendTxCommand.

// ----- Internal implementation ------------------------------------------//
private void sendTxCommand(Message message) throws IOException {
    int encodedSize = 0;
    byte[] buffer = OUTBOUND_BUFFER;
    while (true) {
        try {
            encodedSize = message.encode(buffer, 0, buffer.length);
            break;
        } catch (BufferOverflowException e) {
            buffer = new byte[buffer.length * 2];
        }
    }
    Sender sender = getEndpoint();
    sender.send(buffer, 0, encodedSize);
    sender.advance();
}
Also used : Sender(org.apache.qpid.proton.engine.Sender) BufferOverflowException(java.nio.BufferOverflowException)

Aggregations

Sender (org.apache.qpid.proton.engine.Sender)22 Test (org.junit.Test)10 AmqpClient (org.apache.activemq.transport.amqp.client.AmqpClient)7 AmqpConnection (org.apache.activemq.transport.amqp.client.AmqpConnection)7 AmqpSender (org.apache.activemq.transport.amqp.client.AmqpSender)7 AmqpSession (org.apache.activemq.transport.amqp.client.AmqpSession)7 AmqpValidator (org.apache.activemq.transport.amqp.client.AmqpValidator)7 Delivery (org.apache.qpid.proton.engine.Delivery)7 Source (org.apache.qpid.proton.amqp.messaging.Source)5 Connection (org.apache.qpid.proton.engine.Connection)5 Session (org.apache.qpid.proton.engine.Session)5 AmqpMessage (org.apache.activemq.transport.amqp.client.AmqpMessage)4 Symbol (org.apache.qpid.proton.amqp.Symbol)4 Target (org.apache.qpid.proton.amqp.messaging.Target)4 IAmqpSender (com.microsoft.azure.servicebus.amqp.IAmqpSender)3 ErrorCondition (org.apache.qpid.proton.amqp.transport.ErrorCondition)3 Receiver (org.apache.qpid.proton.engine.Receiver)3 SendLinkHandler (com.microsoft.azure.servicebus.amqp.SendLinkHandler)2 SessionHandler (com.microsoft.azure.servicebus.amqp.SessionHandler)2 HashMap (java.util.HashMap)2