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;
}
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;
}
}
}
}
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();
}
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());
}
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();
}
Aggregations