-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from LeonAquitaine/master
Added support for RabbitMQ, Docker-compose example
- Loading branch information
Showing
18 changed files
with
332 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Microsoft.Extensions.Options; | ||
using Zen.Base.Common; | ||
using Zen.Base.Extension; | ||
using Zen.Base.Module.Service; | ||
|
||
namespace Zen.MessageQueue | ||
{ | ||
public class Configuration : IConfigureOptions<Configuration.IOptions> | ||
{ | ||
private readonly IOptions _options; | ||
|
||
public Configuration(IOptions<Options> options) => _options = options.Value; | ||
|
||
public void Configure(IOptions options) | ||
{ | ||
_options.CopyMembersTo(options); | ||
} | ||
|
||
public interface IOptions | ||
{ | ||
int HttpPort { get; set; } | ||
int HttpsPort { get; set; } | ||
} | ||
|
||
[IoCIgnore] | ||
public class Options : AutoOptions { } | ||
|
||
[Priority(Level = -99)] | ||
public class AutoOptions : IOptions // If nothing else is defined, AutoOptions kicks in. | ||
{ | ||
public int HttpPort { get; set; } = 5000; | ||
public int HttpsPort { get; set; } = 5001; | ||
} | ||
} | ||
} |
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,8 @@ | ||
namespace Zen.MessageQueue | ||
{ | ||
public static class Current | ||
{ | ||
|
||
public readonly static Configuration.IOptions Options = Base.Configuration.GetSettings<Configuration.IOptions, Configuration.Options>(new Configuration.Options(), "MessageQueue"); | ||
} | ||
} |
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,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Zen.Base.Extension; | ||
using Zen.Base.Module.Service; | ||
using Zen.MessageQueue.Shared; | ||
|
||
namespace Zen.MessageQueue | ||
{ | ||
|
||
public delegate void MessageReceivedHandler(object item); | ||
|
||
public static class Queue | ||
{ | ||
private static readonly IMessageQueueBundle DefaultBundle = (IMessageQueueBundle)IoC.GetClassesByInterface<IMessageQueueBundle>(false)?.First()?.CreateInstance(); | ||
|
||
private static readonly Dictionary<Type, object> _cache = new Dictionary<Type, object>(); | ||
|
||
public static event MessageReceivedHandler Receive; | ||
|
||
|
||
public static void RegisterType<T>(this T targetObject) | ||
{ | ||
|
||
if (DefaultBundle == null) | ||
throw new InvalidOperationException("No Message Queue adapter specificed. Try adding a Zen.Module.MQ.* reference."); | ||
|
||
var type = typeof(T); | ||
|
||
if (_cache.ContainsKey(type)) return; | ||
MessageQueuePrimitive<T> adapter = DefaultBundle.AdapterType.CreateGenericInstance<T, MessageQueuePrimitive<T>>(); | ||
|
||
_cache[typeof(T)] = adapter; | ||
|
||
adapter.Receive += (item) => | ||
{ | ||
Receive?.Invoke(item); | ||
}; | ||
} | ||
|
||
public static void Send<T>(this T targetObject) | ||
{ | ||
((MessageQueuePrimitive<T>)_cache[typeof(T)]).Send(targetObject); | ||
} | ||
} | ||
} |
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; | ||
|
||
namespace Zen.MessageQueue.Shared | ||
{ | ||
public interface IMessageQueueBundle | ||
{ | ||
Type AdapterType { get; set; } | ||
} | ||
} |
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,11 @@ | ||
namespace Zen.MessageQueue.Shared | ||
{ | ||
public delegate void MessageReceivedHandler<T>(T item); | ||
|
||
public abstract class MessageQueuePrimitive<T> | ||
{ | ||
public virtual void Send(T item) { } | ||
public virtual event MessageReceivedHandler<T> Receive; | ||
|
||
} | ||
} |
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,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Zen.Base\Zen.Base.csproj" /> | ||
</ItemGroup> | ||
|
||
</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,44 @@ | ||
using Microsoft.Extensions.Options; | ||
using Zen.Base.Common; | ||
using Zen.Base.Module.Service; | ||
|
||
namespace Zen.Module.MQ.RabbitMQ | ||
{ | ||
public class Configuration : IConfigureOptions<Configuration.Options> | ||
{ | ||
private readonly IOptions _options; | ||
|
||
public Configuration(IOptions<Options> options) => _options = options.Value; | ||
|
||
public void Configure(Options options) | ||
{ | ||
options.HostName = _options.HostName; | ||
} | ||
|
||
public interface IOptions | ||
{ | ||
string HostName { get; set; } | ||
bool Durable { get; set; } | ||
bool Exclusive { get; set; } | ||
bool AutoDelete { get; set; } | ||
} | ||
|
||
[IoCIgnore] | ||
public class Options : IOptions | ||
{ | ||
public string HostName { get; set; } | ||
public bool Durable { get; set; } | ||
public bool Exclusive { get; set; } | ||
public bool AutoDelete { get; set; } | ||
} | ||
|
||
[Priority(Level = -99)] | ||
public class AutoOptions : IOptions // If nothing else is defined, AutoOptions kicks in. | ||
{ | ||
public string HostName { get; set; } = @"localhost"; | ||
public bool Durable { get; set; } = true; | ||
public bool Exclusive { get; set; } = false; | ||
public bool AutoDelete { get; set; } = true; | ||
} | ||
} | ||
} |
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,56 @@ | ||
using RabbitMQ.Client; | ||
using Zen.Base; | ||
using Zen.MessageQueue.Shared; | ||
using Zen.Base.Extension; | ||
using System.Text; | ||
using RabbitMQ.Client.Events; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
|
||
namespace Zen.Module.MQ.RabbitMQ | ||
{ | ||
public class RabbitMQAdapter<T> : MessageQueuePrimitive<T> | ||
{ | ||
private readonly Configuration.IOptions _options; | ||
private readonly IModel _channel; | ||
private readonly string _queueName; | ||
private readonly List<string> _categories; | ||
|
||
public RabbitMQAdapter() | ||
{ | ||
_options = new Configuration.Options().GetSettings<Configuration.IOptions, Configuration.Options>("MessageQueue:RabbitMQ"); | ||
|
||
var factory = new ConnectionFactory { HostName = _options.HostName }; | ||
var connection = factory.CreateConnection(); | ||
_channel = connection.CreateModel(); | ||
|
||
_categories = typeof(T).GetParentTypes().Select(i => i.Name).ToList(); | ||
_queueName = typeof(T).FullName; | ||
|
||
|
||
_channel.QueueDeclare(_queueName, durable: _options.Durable, exclusive: _options.Exclusive, autoDelete: _options.AutoDelete); | ||
|
||
var consumer = new EventingBasicConsumer(_channel); | ||
|
||
consumer.Received += (model, ea) => | ||
{ | ||
var body = ea.Body.ToArray(); | ||
var item = Encoding.UTF8.GetString(body).FromJson<T>(); | ||
|
||
Receive?.Invoke(item); | ||
}; | ||
|
||
_channel.BasicConsume(queue: _queueName, autoAck: true, consumer: consumer); | ||
} | ||
|
||
public override event MessageReceivedHandler<T> Receive; | ||
|
||
public override void Send(T item) | ||
{ | ||
var payload = item.ToJson(); | ||
var body = Encoding.UTF8.GetBytes(payload); | ||
|
||
_channel.BasicPublish(exchange: string.Empty, routingKey: _queueName, basicProperties: null, body: body); | ||
} | ||
} | ||
} |
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,12 @@ | ||
using System; | ||
using Zen.Base.Common; | ||
using Zen.MessageQueue.Shared; | ||
|
||
namespace Zen.Module.MQ.RabbitMQ | ||
{ | ||
[Priority(Level = -2)] | ||
public class RabbitMQDefaultBundle : IMessageQueueBundle | ||
{ | ||
public Type AdapterType { get; set; } = typeof(RabbitMQAdapter<>); | ||
} | ||
} |
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,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="RabbitMQ.Client" Version="6.5.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Zen.Base\Zen.Base.csproj" /> | ||
<ProjectReference Include="..\Zen.MessageQueue\Zen.MessageQueue.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.