com.microsoft.azure.servicebus.amqp.SessionHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of azure-servicebus Show documentation
Show all versions of azure-servicebus Show documentation
Java library for Azure Service Bus. Please note, a newer package com.azure:azure-messaging-servicebus for Azure Service Bus is available as of December 2020. While this package will continue to receive critical bug fixes, we strongly encourage you to upgrade. Read the migration guide at https://aka.ms/azsdk/java/migrate/sb for more details.
/*
* 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 org.apache.qpid.proton.engine.BaseHandler;
import org.apache.qpid.proton.engine.EndpointState;
import org.apache.qpid.proton.engine.Event;
import org.apache.qpid.proton.engine.Session;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
public class SessionHandler extends BaseHandler
{
protected static final Logger TRACE_LOGGER = LoggerFactory.getLogger(SessionHandler.class);
private final String name;
public SessionHandler(final String name)
{
this.name = name;
}
@Override
public void onSessionRemoteOpen(Event e)
{
TRACE_LOGGER.debug("onSessionRemoteOpen - entityName: {}, sessionIncCapacity: {}, sessionOutgoingWindow: {}", this.name, e.getSession().getIncomingCapacity(), e.getSession().getOutgoingWindow());
Session session = e.getSession();
if (session != null && session.getLocalState() == EndpointState.UNINITIALIZED)
{
session.open();
}
}
@Override
public void onSessionLocalClose(Event e)
{
TRACE_LOGGER.debug("onSessionLocalClose - entityName: {}, condition: {}", this.name, e.getSession().getCondition() == null ? "none" : e.getSession().getCondition().toString());
Session session = e.getSession();
if (session != null)
{
checkAndFreeSession(session);
}
}
@Override
public void onSessionRemoteClose(Event e)
{
TRACE_LOGGER.debug("onSessionRemoteClose - entityName: {}, condition: {}", this.name, e.getSession().getCondition() == null ? "none" : e.getSession().getCondition().toString());
Session session = e.getSession();
if (session != null)
{
if(session.getLocalState() != EndpointState.CLOSED)
{
session.close();
}
checkAndFreeSession(session);
}
}
@Override
public void onSessionFinal(Event e)
{
TRACE_LOGGER.debug("onSessionFinal - entityName: {}", this.name);
Session session = e.getSession();
if(session != null)
{
session.attachments().clear();
}
}
private static void checkAndFreeSession(Session session)
{
if(session.getLocalState() == EndpointState.CLOSED && session.getRemoteState() == EndpointState.CLOSED)
{
session.free();
}
}
}