Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8661697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:33:35+00:00 2026-06-12T16:33:35+00:00

Do you have any pointers how to determine when a subscription problem has occurred

  • 0

Do you have any pointers how to determine when a subscription problem has occurred so I can reconnect?

My service uses RabbitMQ.Client.MessagePatterns.Subscription for it’s subscription. After some time, my client silently stops receiving messages. I suspect network issues as I our VPN connection is not the most reliable.

I’ve read through the docs for awhile looking for a key to find out when this subscription might be broken due to a network issue without much luck. I’ve tried checking that the connection and channel are still open, but it always seems to report that it is still open.

The messages it does process work quite well and are acknowledged back to the queue so I don’t think it’s an issue with the “ack”.

I’m sure I must be just missing something simple, but I haven’t yet found it.

public void Run(string brokerUri, Action<byte[]> handler)
{
    log.Debug("Connecting to broker: {0}".Fill(brokerUri));
    ConnectionFactory factory = new ConnectionFactory { Uri = brokerUri };

    using (IConnection connection = factory.CreateConnection())
    {
        using (IModel channel = connection.CreateModel())
        {
            channel.QueueDeclare(queueName, true, false, false, null);

            using (Subscription subscription = new Subscription(channel, queueName, false))
            {
                while (!Cancelled)
                {
                    BasicDeliverEventArgs args;

                    if (!channel.IsOpen)
                    {
                        log.Error("The channel is no longer open, but we are still trying to process messages.");
                        throw new InvalidOperationException("Channel is closed.");
                    }
                    else if (!connection.IsOpen)
                    {
                        log.Error("The connection is no longer open, but we are still trying to process message.");
                        throw new InvalidOperationException("Connection is closed.");
                    }

                    bool gotMessage = subscription.Next(250, out args);

                    if (gotMessage)
                    {
                        log.Debug("Received message");
                        try
                        {
                            handler(args.Body);
                        }
                        catch (Exception e)
                        {
                            log.Debug("Exception caught while processing message. Will be bubbled up.", e);
                            throw;
                        }

                        log.Debug("Acknowledging message completion");
                        subscription.Ack(args);
                    }
                }
            }
        }
    }
}

UPDATE:

I simulated a network failure by running the server in a virtual machine and I do get an exception (RabbitMQ.Client.Exceptions.OperationInterruptedException: The AMQP operation was interrupted) when I break the connection for long enough so perhaps it isn’t a network issue. Now I don’t know what it would be but it fails after just a couple hours of running.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:33:36+00:00Added an answer on June 12, 2026 at 4:33 pm

    EDIT: Since I’m sill getting upvotes on this, I should point out that the .NET RabbitMQ client now has this functionality built in: https://www.rabbitmq.com/dotnet-api-guide.html#connection-recovery

    Ideally, you should be able to use this and avoid manually implementing reconnection logic.


    I recently had to implement nearly the same thing. From what I can tell, most of the available information on RabbitMQ assumes that either your network is very reliable or that you run a RabbitMQ broker on the same machine as any client sending or receiving messages, allowing Rabbit to deal with any connection issues.

    It’s really not that hard to set up the Rabbit client to be robust against dropped connections, but there are a few idiosyncrasies that you need to deal with.

    The first thing you need to do turn on the heartbeat:

    ConnectionFactory factory = new ConnectionFactory() 
    {
      Uri = brokerUri,
      RequestedHeartbeat = 30,
    }; 
    

    Setting the “RequestedHeartbeat” to 30 will make the client check every 30 seconds if the connection is still alive. Without this turned on, the message subscriber will sit there happily waiting for another message to come in without a clue that its connection has gone bad.

    Turning the heartbeat on also makes the server check to see if the connection is still up, which can be very important. If a connection goes bad after a message has been picked up by the subscriber but before it’s been acknowledged, the server just assumes that the client is taking a long time, and the message gets “stuck” on the dead connection until it gets closed. With the heartbeat turned on, the server will recognize when the connection goes bad and close it, putting the message back in the queue so another subscriber can handle it. Without the heartbeat, I’ve had to go in manually and close the connection in the Rabbit management UI so that the stuck message can get passed to a subscriber.

    Second, you will need to handle OperationInterruptedException. As you noticed, this is usually the exception the Rabbit client will throw when it notices the connection has been interrupted. If IModel.QueueDeclare() is called when the connection has been interrupted, this is the exception you will get. Handle this exception by disposing of your subscription, channel, and connection and creating new ones.

    Finally, you will have to handle what your consumer does when trying to consume messages from a closed connection. Unfortunately, each different way of consuming messages from a queue in the Rabbit client seems to react differently. QueueingBasicConsumer throws EndOfStreamException if you call QueueingBasicConsumer.Queue.Dequeue on a closed connection. EventingBasicConsumer does nothing, since it’s just waiting for a message. From what I can tell from trying it, the Subscription class you’re using seems to return true from a call to Subscription.Next, but the value of args is null. Once again, handle this by disposing of your connection, channel, and subscription and recreating them.

    The value of connection.IsOpen will be updated to False when the connection fails with the heartbeat on, so you can check that if you would like. However, since the heartbeat runs on a separate thread, you will still need to handle the case where the connection is open when you check it, but closes before subscription.Next() is called.

    One final thing to watch out for is IConnection.Dispose(). This call will throw a EndOfStreamException if you call dispose after the connection has been closed. This seems like a bug to me, and I don’t like not calling dispose on an IDisposable object, so I call it and swallow the exception.

    Putting that all together in a quick and dirty example:

    public bool Cancelled { get; set; }
    
    IConnection _connection = null;
    IModel _channel = null;
    Subscription _subscription = null;
    
    public void Run(string brokerUri, string queueName, Action<byte[]> handler)
    {
        ConnectionFactory factory = new ConnectionFactory() 
        {
            Uri = brokerUri,
            RequestedHeartbeat = 30,
        };
    
        while (!Cancelled)
        {               
            try
            {
                if(_subscription == null)
                {
                    try
                    {
                        _connection = factory.CreateConnection();
                    }
                    catch(BrokerUnreachableException)
                    {
                        //You probably want to log the error and cancel after N tries, 
                        //otherwise start the loop over to try to connect again after a second or so.
                        continue;
                    }
    
                    _channel = _connection.CreateModel();
                    _channel.QueueDeclare(queueName, true, false, false, null);
                    _subscription = new Subscription(_channel, queueName, false);
                }
    
                BasicDeliverEventArgs args;
                bool gotMessage = _subscription.Next(250, out args);
                if (gotMessage)
                {
                    if(args == null)
                    {
                        //This means the connection is closed.
                        DisposeAllConnectionObjects();
                        continue;
                    }
    
                    handler(args.Body);
                    _subscription.Ack(args);
                }
            }
            catch(OperationInterruptedException ex)
            {
                DisposeAllConnectionObjects();
            }
        }
        DisposeAllConnectionObjects();
    }
    
    private void DisposeAllConnectionObjects()
    {
        if(_subscription != null)
        {
            //IDisposable is implemented explicitly for some reason.
            ((IDisposable)_subscription).Dispose();
            _subscription = null;
        }
    
        if(_channel != null)
        {
            _channel.Dispose();
            _channel = null;
        }
    
        if(_connection != null)
        {
            try
            {
                _connection.Dispose();
            }
            catch(EndOfStreamException) 
            {
            }
            _connection = null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone give me any pointers. I have a text file that contains dates
Does anyone have any pointers to good resources concerning mesh networks? Maybe I'm not
Does anyone have any good samples, resources, pointers etc. for C# Silverlight with RIA
Anyone have any ideas what I can do? Please reserve commenting about using 1.1
Using the sizeof operator, I can determine the size of any type – but
I have some code that uses ctypes to try to determine if the file
Does anyone have any idea how to change the thickness of the text pointer
If I have a character pointer that contains NULL bytes is there any built
Anyone have any idea why my jQuery carousel is working fine at the bottom
Anyone have any experience yet getting Radiant CMS extensions to actually make it onto

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.