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

com.microsoft.azure.servicebus.amqp.BaseLinkHandler 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.logging.Level;
import java.util.logging.Logger;

import org.apache.qpid.proton.amqp.transport.*;
import org.apache.qpid.proton.engine.*;

import com.microsoft.azure.servicebus.ClientConstants;

public class BaseLinkHandler extends BaseHandler {
    protected static final Logger TRACE_LOGGER = Logger.getLogger(ClientConstants.SERVICEBUS_CLIENT_TRACE);

    private final IAmqpLink underlyingEntity;

    public BaseLinkHandler(final IAmqpLink amqpLink) {
        this.underlyingEntity = amqpLink;
    }

    @Override
    public void onLinkLocalClose(Event event) {
        Link link = event.getLink();
        if (link != null) {
            if (TRACE_LOGGER.isLoggable(Level.FINE)) {
                TRACE_LOGGER.log(Level.FINE, String.format("linkName[%s]", link.getName()));
            }
        }

        closeSession(link);
    }

    @Override
    public void onLinkRemoteClose(Event event) {
        final Link link = event.getLink();

        if (link.getLocalState() != EndpointState.CLOSED) {
            link.close();
        }

        if (link != null) {
            ErrorCondition condition = link.getRemoteCondition();
            this.processOnClose(link, condition);
        }

        closeSession(link);
    }

    @Override
    public void onLinkRemoteDetach(Event event) {
        final Link link = event.getLink();

        if (link.getLocalState() != EndpointState.CLOSED) {
            link.close();
        }

        if (link != null) {
            this.processOnClose(link, link.getRemoteCondition());
        }

        closeSession(link);
    }

    public void processOnClose(Link link, ErrorCondition condition) {
        if (condition != null) {
            if (TRACE_LOGGER.isLoggable(Level.FINE)) {
                TRACE_LOGGER.log(Level.FINE, "linkName[" + link.getName() +
                        (condition != null ? "], ErrorCondition[" + condition.getCondition() + ", " + condition.getDescription() + "]" : "], condition[null]"));
            }
        }

        this.underlyingEntity.onClose(condition);
    }

    public void processOnClose(Link link, Exception exception) {
        this.underlyingEntity.onError(exception);
    }

    private void closeSession(Link link) {
        if (link.getSession() != null && link.getSession().getLocalState() != EndpointState.CLOSED)
            link.getSession().close();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy