Browse Source

Fix RabbitMQ connection was closed unexpectedly. (#863)

* Fix RabbitMQ connection was closed unexpectedly.

* Ensure that the RabbitMQ connection is unique.
master
Null 3 years ago
committed by GitHub
parent
commit
a650299a2b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 5 deletions
  1. +9
    -4
      src/DotNetCore.CAP.RabbitMQ/IConnectionChannelPool.Default.cs
  2. +2
    -1
      src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClient.cs

+ 9
- 4
src/DotNetCore.CAP.RabbitMQ/IConnectionChannelPool.Default.cs View File

@@ -67,13 +67,17 @@ namespace DotNetCore.CAP.RabbitMQ


public IConnection GetConnection() public IConnection GetConnection()
{ {
if (_connection != null && _connection.IsOpen)
lock (SLock)
{ {
if (_connection != null && _connection.IsOpen)
{
return _connection;
}

_connection?.Dispose();
_connection = _connectionActivator();
return _connection; return _connection;
} }

_connection = _connectionActivator();
return _connection;
} }


public void Dispose() public void Dispose()
@@ -84,6 +88,7 @@ namespace DotNetCore.CAP.RabbitMQ
{ {
context.Dispose(); context.Dispose();
} }
_connection?.Dispose();
} }


private static Func<IConnection> CreateConnection(RabbitMQOptions options) private static Func<IConnection> CreateConnection(RabbitMQOptions options)


+ 2
- 1
src/DotNetCore.CAP.RabbitMQ/RabbitMQConsumerClient.cs View File

@@ -92,7 +92,8 @@ namespace DotNetCore.CAP.RabbitMQ
public void Dispose() public void Dispose()
{ {
_channel?.Dispose(); _channel?.Dispose();
_connection?.Dispose();
//The connection should not be closed here, because the connection is still in use elsewhere.
//_connection?.Dispose();
} }


public void Connect() public void Connect()


Loading…
Cancel
Save