forked from DevArchitecture/DevArchitecture
-
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.
Merge branch 'master' into cassandra-branch
- Loading branch information
Showing
13 changed files
with
173 additions
and
95 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
14 changes: 14 additions & 0 deletions
14
Core/CrossCuttingConcerns/Logging/Serilog/Loggers/ConsoleLogger.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,14 @@ | ||
using Serilog; | ||
|
||
namespace Core.CrossCuttingConcerns.Logging.Serilog.Loggers; | ||
|
||
public class ConsoleLogger : LoggerServiceBase | ||
{ | ||
public ConsoleLogger() | ||
{ | ||
var seriLogConfig = new LoggerConfiguration() | ||
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}") | ||
.CreateLogger(); | ||
Logger = seriLogConfig; | ||
} | ||
} |
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,9 @@ | ||
using System.Threading.Tasks; | ||
using Core.Utilities.Results; | ||
|
||
namespace Core.Utilities.MessageBrokers; | ||
|
||
public interface IMessageBrokerHelper | ||
{ | ||
Task<IResult> QueueMessageAsync<T>(T messageModel); | ||
} |
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,6 @@ | ||
namespace Core.Utilities.MessageBrokers; | ||
|
||
public interface IMessageConsumer | ||
{ | ||
void GetQueue(); | ||
} |
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,51 @@ | ||
|
||
using System.Threading.Tasks; | ||
using Confluent.Kafka; | ||
using Core.Utilities.IoC; | ||
using Core.Utilities.MessageBrokers.RabbitMq; | ||
using Core.Utilities.Results; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Newtonsoft.Json; | ||
|
||
namespace Core.Utilities.MessageBrokers.Kafka; | ||
|
||
public class KafkaMessageBroker : IMessageBrokerHelper | ||
{ | ||
private readonly MessageBrokerOptions _kafkaOptions; | ||
|
||
public KafkaMessageBroker() | ||
{ | ||
var configuration = ServiceTool.ServiceProvider.GetService<IConfiguration>(); | ||
if (configuration != null) | ||
_kafkaOptions = configuration | ||
.GetSection("MessageBrokerOptions").Get<MessageBrokerOptions>(); | ||
} | ||
|
||
public async Task<IResult> QueueMessageAsync<T>(T messageModel) | ||
{ | ||
var producerConfig = new ProducerConfig | ||
{ | ||
BootstrapServers = $"{_kafkaOptions.HostName}:{_kafkaOptions.Port}", | ||
Acks = Acks.All | ||
}; | ||
|
||
var message = JsonConvert.SerializeObject(messageModel); | ||
var topicName = typeof(T).Name; | ||
using var p = new ProducerBuilder<Null, string>(producerConfig).Build(); | ||
try | ||
{ | ||
await p.ProduceAsync(topicName | ||
, new Message<Null, string> | ||
{ | ||
Value = message | ||
}); | ||
return new SuccessResult(); | ||
} | ||
|
||
catch (ProduceException<Null, string> e) | ||
{ | ||
return new ErrorResult(e.Message); | ||
} | ||
} | ||
} |
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,9 @@ | ||
namespace Core.Utilities.MessageBrokers; | ||
|
||
public class MessageBrokerOptions | ||
{ | ||
public string HostName { get; set; } | ||
public string UserName { get; set; } | ||
public string Password { get; set; } | ||
public int Port { get; set; } | ||
} |
7 changes: 0 additions & 7 deletions
7
Core/Utilities/MessageBrokers/RabbitMq/IMessageBrokerHelper.cs
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
Core/Utilities/MessageBrokers/RabbitMq/MessageBrokerOptions.cs
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,51 @@ | ||
using System.Text; | ||
using System; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Core.Utilities.Results; | ||
using Microsoft.Extensions.Configuration; | ||
using Newtonsoft.Json; | ||
using RabbitMQ.Client; | ||
|
||
namespace Core.Utilities.MessageBrokers.RabbitMq | ||
namespace Core.Utilities.MessageBrokers.RabbitMq; | ||
|
||
public class RMqQueueHelper : IMessageBrokerHelper | ||
{ | ||
public class MqQueueHelper : IMessageBrokerHelper | ||
{ | ||
private readonly MessageBrokerOptions _brokerOptions; | ||
private readonly MessageBrokerOptions _brokerOptions; | ||
|
||
public MqQueueHelper(IConfiguration configuration) | ||
{ | ||
Configuration = configuration; | ||
_brokerOptions = Configuration.GetSection("MessageBrokerOptions").Get<MessageBrokerOptions>(); | ||
} | ||
public RMqQueueHelper(IConfiguration configuration) | ||
{ | ||
Configuration = configuration; | ||
_brokerOptions = Configuration.GetSection("MessageBrokerOptions").Get<MessageBrokerOptions>(); | ||
} | ||
|
||
public IConfiguration Configuration { get; } | ||
public IConfiguration Configuration { get; } | ||
|
||
public void QueueMessage(string messageText) | ||
{ | ||
var factory = new ConnectionFactory | ||
public Task<IResult> QueueMessageAsync<T>(T messageModel) | ||
{ | ||
using var connection = new ConnectionFactory() | ||
{ | ||
HostName = _brokerOptions.HostName, | ||
Port = _brokerOptions.Port, | ||
UserName = _brokerOptions.UserName, | ||
Password = _brokerOptions.Password | ||
}; | ||
using var connection = factory.CreateConnection(); | ||
using var channel = connection.CreateModel(); | ||
channel.QueueDeclare( | ||
queue: "DArchQueue", | ||
durable: false, | ||
exclusive: false, | ||
autoDelete: false, | ||
arguments: null); | ||
|
||
var message = JsonConvert.SerializeObject(messageText); | ||
var body = Encoding.UTF8.GetBytes(message); | ||
|
||
channel.BasicPublish(exchange: string.Empty, routingKey: "DArchQueue", basicProperties: null, body: body); | ||
} | ||
Password = _brokerOptions.Password, | ||
AutomaticRecoveryEnabled = true, | ||
NetworkRecoveryInterval = new TimeSpan(2000), | ||
} | ||
.CreateConnection(); | ||
using var channel = connection.CreateModel(); | ||
var topicName = typeof(T).Name; | ||
channel.QueueDeclare( | ||
topicName, | ||
false, | ||
false, | ||
false, | ||
null); | ||
|
||
var message = JsonConvert.SerializeObject(messageModel); | ||
var body = Encoding.UTF8.GetBytes(message); | ||
|
||
channel.BasicPublish(string.Empty, topicName, null, body); | ||
return Task.FromResult<IResult>(new SuccessResult()); | ||
|
||
} | ||
} |