forked from ardalis/pluralsight-ddd-fundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic RabbitMQ comms from ClinicManagement to FrontDesk working
- Loading branch information
Showing
20 changed files
with
290 additions
and
109 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
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
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
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
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
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
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
2 changes: 1 addition & 1 deletion
2
ClinicManagement/src/ClinicManagement.Blazor/wwwroot/appsettings.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
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
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
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
55 changes: 55 additions & 0 deletions
55
ClinicManagement/src/ClinicManagement.Infrastructure/Messaging/RabbitMessagePublisher.cs
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,55 @@ | ||
using System; | ||
using System.Text; | ||
using Ardalis.GuardClauses; | ||
using ClinicManagement.Core.Interfaces; | ||
using PluralsightDdd.SharedKernel.Interfaces; | ||
using Microsoft.Extensions.ObjectPool; | ||
using RabbitMQ.Client; | ||
using System.Text.Json; | ||
using PluralsightDdd.SharedKernel; | ||
|
||
namespace ClinicManagement.Infrastructure.Messaging | ||
{ | ||
public class RabbitMessagePublisher : IMessagePublisher | ||
{ | ||
private readonly DefaultObjectPool<IModel> _objectPool; | ||
|
||
public RabbitMessagePublisher(IPooledObjectPolicy<IModel> objectPolicy) | ||
{ | ||
_objectPool = new DefaultObjectPool<IModel>(objectPolicy, Environment.ProcessorCount * 2); | ||
} | ||
|
||
public void Publish(IApplicationEvent applicationEvent) | ||
{ | ||
Guard.Against.Null(applicationEvent, nameof(applicationEvent)); | ||
|
||
var channel = _objectPool.Get(); | ||
|
||
object message = (object)applicationEvent; | ||
try | ||
{ | ||
string exchangeName = MessagingConstants.Exchanges.FRONTDESK_CLINICMANAGEMENT_EXCHANGE; | ||
channel.ExchangeDeclare(exchangeName, "direct", true, false, null); | ||
|
||
var sendBytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(message)); | ||
|
||
var properties = channel.CreateBasicProperties(); | ||
properties.Persistent = true; | ||
|
||
channel.BasicPublish( | ||
exchange: exchangeName, | ||
routingKey: "entity-changes", | ||
basicProperties: properties, | ||
body: sendBytes); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw ex; | ||
} | ||
finally | ||
{ | ||
_objectPool.Return(channel); | ||
} | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...Management/src/ClinicManagement.Infrastructure/Messaging/RabbitModelPooledObjectPolicy.cs
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,49 @@ | ||
using Microsoft.Extensions.ObjectPool; | ||
using RabbitMQ.Client; | ||
using PluralsightDdd.SharedKernel; | ||
|
||
namespace ClinicManagement.Infrastructure.Messaging | ||
{ | ||
// source: https://www.c-sharpcorner.com/article/publishing-rabbitmq-message-in-asp-net-core/ | ||
public class RabbitModelPooledObjectPolicy : IPooledObjectPolicy<IModel> | ||
{ | ||
private readonly IConnection _connection; | ||
|
||
public RabbitModelPooledObjectPolicy() | ||
{ | ||
_connection = GetConnection(); | ||
} | ||
|
||
private IConnection GetConnection() | ||
{ | ||
var factory = new ConnectionFactory() | ||
{ | ||
HostName = "localhost", // TODO: Read from config | ||
UserName = MessagingConstants.Credentials.DEFAULT_USERNAME, | ||
Password = MessagingConstants.Credentials.DEFAULT_PASSWORD, | ||
Port = 5672, | ||
VirtualHost = "/", | ||
}; | ||
|
||
return factory.CreateConnection(); | ||
} | ||
|
||
public IModel Create() | ||
{ | ||
return _connection.CreateModel(); | ||
} | ||
|
||
public bool Return(IModel obj) | ||
{ | ||
if (obj.IsOpen) | ||
{ | ||
return true; | ||
} | ||
else | ||
{ | ||
obj?.Dispose(); | ||
return false; | ||
} | ||
} | ||
} | ||
} |
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.