me--
me--

Reputation: 2148

Apache NMS - how to determine if connection is up

I'm having a hell of a time with some Apache NMS issues. Part of this may be my own lack of understanding of the platform.

In essence, I have an NMS STOMP client that I use to send and receive AMQ messages via STOMP. The API looks a bit like this:

internal sealed class NMSStompClient : IDisposable
{
    public bool IsConnected { get; }

    public void Connect(Uri uri, string userId, string password, TimeSpan timeout);

    public void Disconnect();

    public void Send(IDestination destination, IDestination replyDestination, long sessionId, int correlationId, byte[] messageBytes, TimeSpan timeout);

    public IDisposable Subscribe(IDestination destination, Action<IMessage> messageHandler, Action<IMessage, Exception> errorHandler);
}

I'm attempting to get my integration tests to pass consistently, but they always fail when I increase the amount of time that AMQ is down for (passes at 10 seconds, fails at 60). After excruciating amounts of debugging and tracing, I've found that the problem appears to stem from my IsConnected implementation (or, at least, this is part of the problem):

public bool IsConnected
{
    // connection is Apache.NMS.IConnection
    // session is Apache.NMS.Stomp.Session
    get { return this.connection != null && this.connection.IsStarted && this.session != null && this.session.Connection == this.connection; }
}

I arrived at this implementation through trial and error. I simply could not find a simple way to determine whether the connection was "up". There is no property that I can find on either IConnection or Session that will tell me this.

I'm aware of the ConnectionInterruptedListener, ConnectionResumedListener, and ExceptionListener events, but the only one raised during my integration test is ExceptionListener. Moreover, I understand they aren't raised at all when using failover, which I am in production.

Can anyone help me to reliably determine whether the connection is up or not? Or perhaps clear up any misunderstanding I may have?

Upvotes: 2

Views: 4734

Answers (1)

Tim Bish
Tim Bish

Reputation: 18366

The Apache.NMS.Stomp client does indeed have a failover transport that will call the interrupted and resumed methods however when using failover you don't really need to care about those since the failover transport will deal with reconnecting things for you. When using a straight TCP connection you will only see the ExceptionListener invoked because from the point of view of the tcp transport, once its failed its failed, so an exception is appropriate.

You can make the general case assumption that when your client gets a callback on its ExceptionListener listener that the connection state is failed and do whatever reconnect you need to, although if you use the failover transport it will be handled for you, so no work needed on your part.

Upvotes: 4

Related Questions