Skip to content

add events for connection recovery errors and connection success. #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions projects/client/RabbitMQ.Client/src/client/api/IConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ public interface IConnection : NetworkConnection, IDisposable
/// of those event handlers throws an exception, as well.
/// </remarks>
event EventHandler<CallbackExceptionEventArgs> CallbackException;
event EventHandler<EventArgs> RecoverySucceeded;
event EventHandler<ConnectionRecoveryErrorEventArgs> ConnectionRecoveryError;


event EventHandler<ConnectionBlockedEventArgs> ConnectionBlocked;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// This source code is dual-licensed under the Apache License, version
// 2.0, and the Mozilla Public License, version 1.1.
//
// The APL v2.0:
//
//---------------------------------------------------------------------------
// Copyright (c) 2007-2016 Pivotal Software, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//---------------------------------------------------------------------------
//
// The MPL v1.1:
//
//---------------------------------------------------------------------------
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License
// at http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
// the License for the specific language governing rights and
// limitations under the License.
//
// The Original Code is RabbitMQ.
//
// The Initial Developer of the Original Code is Pivotal Software, Inc.
// Copyright (c) 2007-2016 Pivotal Software, Inc. All rights reserved.
//---------------------------------------------------------------------------

using System;

namespace RabbitMQ.Client.Events
{
public sealed class ConnectionRecoveryErrorEventArgs : EventArgs
{
public ConnectionRecoveryErrorEventArgs(Exception ex)
{
Exception = ex;
}

public Exception Exception { get; private set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public class AutorecoveringConnection : IConnection, IRecoverable
private EventHandler<QueueNameChangedAfterRecoveryEventArgs> m_queueNameChange;
private EventHandler<EventArgs> m_recovery;

private EventHandler<ConnectionRecoveryErrorEventArgs> m_connectionRecoveryError;

public AutorecoveringConnection(ConnectionFactory factory, string clientProvidedName = null)
{
m_factory = factory;
Expand All @@ -121,6 +123,42 @@ private bool ManuallyClosed
}
}

public event EventHandler<EventArgs> RecoverySucceeded
{
add
{
lock (m_eventLock)
{
m_recovery += value;
}
}
remove
{
lock (m_eventLock)
{
m_recovery -= value;
}
}
}

public event EventHandler<ConnectionRecoveryErrorEventArgs> ConnectionRecoveryError
{
add
{
lock (m_eventLock)
{
m_connectionRecoveryError += value;
}
}
remove
{
lock (m_eventLock)
{
m_connectionRecoveryError -= value;
}
}
}

public event EventHandler<CallbackExceptionEventArgs> CallbackException
{
add
Expand Down Expand Up @@ -235,6 +273,7 @@ public event EventHandler<QueueNameChangedAfterRecoveryEventArgs> QueueNameChang
}
}

[Obsolete("Use RecoverySucceeded instead")]
public event EventHandler<EventArgs> Recovery
{
add
Expand Down Expand Up @@ -798,14 +837,33 @@ protected void RecoverConnectionDelegate()
m_delegate = new Connection(m_factory, false, fh, this.ClientProvidedName);
recovering = false;
}
catch (Exception)
catch (Exception e)
{
// Trigger recovery error event
var handler = m_connectionRecoveryError;
if (handler != null)
{
var args = new ConnectionRecoveryErrorEventArgs(e);
foreach (EventHandler<ConnectionRecoveryErrorEventArgs> h in handler.GetInvocationList())
{
try
{
h(this, args);
}
catch (Exception ex)
{
var a = new CallbackExceptionEventArgs(ex);
a.Detail["context"] = "OnConnectionRecoveryError";
m_delegate.OnCallbackException(a);
}
}
}
#if NETFX_CORE
System.Threading.Tasks.Task.Delay(m_factory.NetworkRecoveryInterval).Wait();
#else
Thread.Sleep(m_factory.NetworkRecoveryInterval);
#endif
// TODO: provide a way to handle these exceptions
// TODO: provide a way to handle these exceptions
}
}
}
Expand Down
39 changes: 38 additions & 1 deletion projects/client/RabbitMQ.Client/src/client/impl/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
using System.Net.Sockets;
#endif

using System.Reflection;
using System.Text;
using System.Threading;

Expand All @@ -72,6 +71,8 @@ public class Connection : IConnection

private ManualResetEvent m_appContinuation = new ManualResetEvent(false);
private EventHandler<CallbackExceptionEventArgs> m_callbackException;
private EventHandler<EventArgs> m_recoverySucceeded;
private EventHandler<ConnectionRecoveryErrorEventArgs> connectionRecoveryFailure;

private IDictionary<string, object> m_clientProperties;

Expand All @@ -81,6 +82,7 @@ public class Connection : IConnection
private EventHandler<ConnectionBlockedEventArgs> m_connectionBlocked;
private EventHandler<ShutdownEventArgs> m_connectionShutdown;
private EventHandler<EventArgs> m_connectionUnblocked;

private IConnectionFactory m_factory;
private IFrameHandler m_frameHandler;

Expand Down Expand Up @@ -130,6 +132,24 @@ public Connection(IConnectionFactory factory, bool insist, IFrameHandler frameHa

public Guid Id { get { return m_id; } }

public event EventHandler<EventArgs> RecoverySucceeded
{
add
{
lock (m_eventLock)
{
m_recoverySucceeded += value;
}
}
remove
{
lock (m_eventLock)
{
m_recoverySucceeded -= value;
}
}
}

public event EventHandler<CallbackExceptionEventArgs> CallbackException
{
add
Expand Down Expand Up @@ -211,6 +231,23 @@ public event EventHandler<EventArgs> ConnectionUnblocked
}
}

public event EventHandler<ConnectionRecoveryErrorEventArgs> ConnectionRecoveryError
{
add
{
lock (m_eventLock)
{
connectionRecoveryFailure += value;
}
}
remove
{
lock (m_eventLock)
{
connectionRecoveryFailure -= value;
}
}
}
public string ClientProvidedName { get; private set; }

public bool AutoClose
Expand Down
17 changes: 16 additions & 1 deletion projects/client/Unit/src/unit/TestConnectionRecovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void TestBasicConnectionRecoveryWithEndpointList()
{
using(var c = CreateAutorecoveringConnection(
new List<AmqpTcpEndpoint>
{
{
new AmqpTcpEndpoint("127.0.0.1"),
new AmqpTcpEndpoint("localhost")
}))
Expand All @@ -174,6 +174,21 @@ public void TestBasicConnectionRecoveryWithEndpointList()
}
}

[Test]
public void TestBasicConnectionRecoveryErrorEvent()
{
Assert.IsTrue(Conn.IsOpen);
using(var c = CreateAutorecoveringConnection())
{
var latch = new AutoResetEvent(false);
c.ConnectionRecoveryError += (o, _args) => latch.Set();
StopRabbitMQ();
latch.WaitOne(30000);
StartRabbitMQ();
WaitForRecovery(c);
}
}

[Test]
public void TestBasicConnectionRecoveryWithEndpointListAndUnreachableHosts()
{
Expand Down