All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.microsoft.azure.servicebus.amqp.SendLinkHandler Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
/*
 * Copyright (c) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
 */
package com.microsoft.azure.servicebus.amqp;

import java.util.Locale;
import java.util.logging.Level;

import org.apache.qpid.proton.amqp.transport.ErrorCondition;
import org.apache.qpid.proton.engine.Delivery;
import org.apache.qpid.proton.engine.Event;
import org.apache.qpid.proton.engine.Link;
import org.apache.qpid.proton.engine.Sender;

public class SendLinkHandler extends BaseLinkHandler
{
	private final IAmqpSender msgSender;
	private final Object firstFlow;
	private boolean isFirstFlow;

	public SendLinkHandler(final IAmqpSender sender)
	{
		super(sender);

		this.msgSender = sender;
		this.firstFlow = new Object();
		this.isFirstFlow = true;
	}

	@Override
	public void onLinkRemoteOpen(Event event)
	{
		Link link = event.getLink();
		if (link != null && link instanceof Sender)
		{
			Sender sender = (Sender) link;
			if (link.getRemoteTarget() != null)
			{
				if(TRACE_LOGGER.isLoggable(Level.FINE))
				{
					TRACE_LOGGER.log(Level.FINE, String.format(Locale.US, "linkName[%s], remoteTarget[%s]", sender.getName(), link.getRemoteTarget()));
				}

				synchronized (this.firstFlow)
				{
					this.isFirstFlow = false;
					this.msgSender.onOpenComplete(null);
				}
			}
			else
			{
				if(TRACE_LOGGER.isLoggable(Level.FINE))
				{
					TRACE_LOGGER.log(Level.FINE,
							String.format(Locale.US, "linkName[%s], remoteTarget[null], remoteSource[null], action[waitingForError]", sender.getName()));
				}
			}
		}
	}

	@Override
	public void onDelivery(Event event)
	{		
		Delivery delivery = event.getDelivery();
		if(TRACE_LOGGER.isLoggable(Level.FINEST))
		{
			Sender sender = (Sender) delivery.getLink();
			TRACE_LOGGER.log(Level.FINEST, 
					"linkName[" + sender.getName() + 
					"], unsettled[" + sender.getUnsettled() + "], credit[" + sender.getCredit()+ "], deliveryState[" + delivery.getRemoteState() + 
					"], delivery.isBuffered[" + delivery.isBuffered() +"], delivery.id[" + new String(delivery.getTag()) + "]");
		}

		msgSender.onSendComplete(delivery);
	}

	@Override
	public void onLinkFlow(Event event)
	{
		if (this.isFirstFlow)
		{
			synchronized (this.firstFlow)
			{
				if (this.isFirstFlow)
				{
					this.msgSender.onOpenComplete(null);
					this.isFirstFlow = false;
				}
			}
		}

		this.msgSender.onFlow();

		if(TRACE_LOGGER.isLoggable(Level.FINEST))
		{
			Sender sender = event.getSender();
			TRACE_LOGGER.log(Level.FINEST, "linkName[" + sender.getName() + "], unsettled[" + sender.getUnsettled() + "], credit[" + sender.getCredit()+ "]");
		}
	}

	@Override
	public void onLinkRemoteClose(Event event)
	{
		Link link = event.getLink();
		ErrorCondition condition = link.getRemoteCondition();
		this.processOnClose(link, condition);
	}

	@Override
	public void onLinkRemoteDetach(Event event)
	{
		this.onLinkRemoteClose(event);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy