Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AMQP retry mechanism and example projects (#312)
* Add AMQP retry mechanism and example projects * Fix retry start index * Make a distinction between internally managed connection reference and not * Add better exception feedback on connection failure. * Make settings responsible for closing Session and Connection objects. * Update examples to show usage of both QueueNamed and Address settings * Add IsPackable tag in example projects csproj files
- Loading branch information
Showing
15 changed files
with
629 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/Amqp/Akka.Streams.Amqp.V1.Tests/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"profiles": { | ||
"Akka.Streams.Amqp.V1.Tests": { | ||
"commandName": "Project" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Akka.Serialization; | ||
using Amqp; | ||
using Amqp.Framing; | ||
using Amqp.Types; | ||
|
||
namespace Akka.Streams.Amqp.V1 | ||
{ | ||
public class AddressSinkSettings<T> : IAmqpSinkSettings<T> | ||
{ | ||
private readonly string _linkName; | ||
private readonly string _queueName; | ||
private readonly Serializer _serializer; | ||
private readonly Address _address; | ||
private readonly object _lock = new object(); | ||
|
||
private Connection _connection; | ||
private Session _session; | ||
|
||
public bool ManageConnection => true; | ||
|
||
public AddressSinkSettings( | ||
Address address, | ||
string linkName, | ||
string queueName, | ||
Serializer serializer) | ||
{ | ||
_address = address; | ||
_linkName = linkName; | ||
_queueName = queueName; | ||
_serializer = serializer; | ||
} | ||
|
||
public byte[] GetBytes(T obj) | ||
{ | ||
return _serializer.ToBinary(obj); | ||
} | ||
|
||
public void CloseConnection() | ||
{ | ||
_session?.Close(); | ||
_connection?.Close(); | ||
|
||
_session = null; | ||
_connection = null; | ||
} | ||
|
||
public async Task CloseConnectionAsync() | ||
{ | ||
if(_session != null) | ||
await _session.CloseAsync(); | ||
|
||
if(_connection != null) | ||
await _connection.CloseAsync(); | ||
} | ||
|
||
public SenderLink GetSenderLink() | ||
{ | ||
if (_connection == null || _connection.IsClosed) | ||
_connection = new Connection(_address); | ||
|
||
if (_session == null || _session.IsClosed) | ||
_session = new Session(_connection); | ||
|
||
return new SenderLink( | ||
_session, | ||
_linkName, | ||
new Target | ||
{ | ||
Address = _queueName, | ||
Capabilities = new[] { new Symbol("queue") } | ||
}, | ||
null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Akka.Serialization; | ||
using Amqp; | ||
using Amqp.Framing; | ||
using Amqp.Types; | ||
|
||
namespace Akka.Streams.Amqp.V1 | ||
{ | ||
public class AddressSourceSettings<T> : IAmqpSourceSettings<T> | ||
{ | ||
private readonly string _linkName; | ||
private readonly string _queueName; | ||
private readonly Serializer _serializer; | ||
|
||
public int Credit { get; } | ||
private readonly Address _address; | ||
private Connection _connection; | ||
private Session _session; | ||
|
||
public bool ManageConnection => true; | ||
|
||
public AddressSourceSettings( | ||
Address address, | ||
string linkName, | ||
string queueName, | ||
int credit, | ||
Serializer serializer) | ||
{ | ||
_address = address; | ||
_linkName = linkName; | ||
_queueName = queueName; | ||
Credit = credit; | ||
_serializer = serializer; | ||
} | ||
|
||
public T Convert(Message message) | ||
{ | ||
var bString = message.GetBody<byte[]>(); | ||
return _serializer.FromBinary<T>(bString); | ||
} | ||
|
||
public void CloseConnection() | ||
{ | ||
_session?.Close(); | ||
_connection?.Close(); | ||
} | ||
|
||
public async Task CloseConnectionAsync() | ||
{ | ||
if (_session != null) | ||
await _session.CloseAsync(); | ||
|
||
if (_connection != null) | ||
await _connection.CloseAsync(); | ||
} | ||
|
||
public ReceiverLink GetReceiverLink() | ||
{ | ||
if(_connection == null || _connection.IsClosed) | ||
_connection = new Connection(_address); | ||
|
||
if (_session == null || _session.IsClosed) | ||
_session = new Session(_connection); | ||
|
||
return new ReceiverLink( | ||
_session, | ||
_linkName, | ||
new Source | ||
{ | ||
Address = _queueName, | ||
Capabilities = new[] { new Symbol("queue") } | ||
}, | ||
null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.