From 55981f714c20027092f89b0bea5069974f478898 Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Mon, 17 Dec 2018 17:18:09 +0300 Subject: [PATCH] Created sample rabbitmq messaging application --- .../Volo/Abp/EventBus/EventBusExtensions.cs | 36 +++++++++++++++++ .../Volo/Abp/EventBus/IEventBus.cs | 4 +- samples/RabbitMqEventBus/App1/App1.csproj | 13 ++++++ .../App1/App1MessagingService.cs | 40 +++++++++++++++++++ samples/RabbitMqEventBus/App1/App1Module.cs | 20 ++++++++++ .../App1/App1TextEventHandler.cs | 34 ++++++++++++++++ .../App1/App1TextReceivedEventHandler.cs | 18 +++++++++ samples/RabbitMqEventBus/App1/Program.cs | 24 +++++++++++ samples/RabbitMqEventBus/App2/App2.csproj | 13 ++++++ .../App2/App2MessagingService.cs | 40 +++++++++++++++++++ samples/RabbitMqEventBus/App2/App2Module.cs | 20 ++++++++++ .../App2/App2TextEventHandler.cs | 34 ++++++++++++++++ .../App2/App2TextReceivedEventHandler.cs | 18 +++++++++ samples/RabbitMqEventBus/App2/Program.cs | 24 +++++++++++ samples/RabbitMqEventBus/RabbitMqEventBus.sln | 37 +++++++++++++++++ .../SharedModule/SharedModule.csproj | 7 ++++ .../SharedModule/TextEventData.cs | 7 ++++ .../SharedModule/TextReceivedEventData.cs | 7 ++++ 18 files changed, 394 insertions(+), 2 deletions(-) create mode 100644 framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusExtensions.cs create mode 100644 samples/RabbitMqEventBus/App1/App1.csproj create mode 100644 samples/RabbitMqEventBus/App1/App1MessagingService.cs create mode 100644 samples/RabbitMqEventBus/App1/App1Module.cs create mode 100644 samples/RabbitMqEventBus/App1/App1TextEventHandler.cs create mode 100644 samples/RabbitMqEventBus/App1/App1TextReceivedEventHandler.cs create mode 100644 samples/RabbitMqEventBus/App1/Program.cs create mode 100644 samples/RabbitMqEventBus/App2/App2.csproj create mode 100644 samples/RabbitMqEventBus/App2/App2MessagingService.cs create mode 100644 samples/RabbitMqEventBus/App2/App2Module.cs create mode 100644 samples/RabbitMqEventBus/App2/App2TextEventHandler.cs create mode 100644 samples/RabbitMqEventBus/App2/App2TextReceivedEventHandler.cs create mode 100644 samples/RabbitMqEventBus/App2/Program.cs create mode 100644 samples/RabbitMqEventBus/RabbitMqEventBus.sln create mode 100644 samples/RabbitMqEventBus/SharedModule/SharedModule.csproj create mode 100644 samples/RabbitMqEventBus/SharedModule/TextEventData.cs create mode 100644 samples/RabbitMqEventBus/SharedModule/TextReceivedEventData.cs diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusExtensions.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusExtensions.cs new file mode 100644 index 0000000000..5139e1cbcb --- /dev/null +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusExtensions.cs @@ -0,0 +1,36 @@ +using System; +using JetBrains.Annotations; +using Volo.Abp.Threading; + +namespace Volo.Abp.EventBus +{ + public static class EventBusExtensions + { + /// + /// Triggers an event. + /// + /// Event type + /// Event bus instance + /// Related data for the event + public static void Publish([NotNull] this IEventBus eventBus, [NotNull] TEvent eventData) + where TEvent : class + { + Check.NotNull(eventBus, nameof(eventBus)); + + AsyncHelper.RunSync(() => eventBus.PublishAsync(eventData)); + } + + /// + /// Triggers an event. + /// + /// Event bus instance + /// Event type + /// Related data for the event + public static void Publish([NotNull] this IEventBus eventBus, [NotNull] Type eventType, [NotNull] object eventData) + { + Check.NotNull(eventBus, nameof(eventBus)); + + AsyncHelper.RunSync(() => eventBus.PublishAsync(eventType, eventData)); + } + } +} diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventBus.cs index 4526c1f3b4..1d93c05580 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventBus.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventBus.cs @@ -6,7 +6,7 @@ namespace Volo.Abp.EventBus public interface IEventBus { /// - /// Triggers an event asynchronously. + /// Triggers an event. /// /// Event type /// Related data for the event @@ -15,7 +15,7 @@ namespace Volo.Abp.EventBus where TEvent : class; /// - /// Triggers an event asynchronously. + /// Triggers an event. /// /// Event type /// Related data for the event diff --git a/samples/RabbitMqEventBus/App1/App1.csproj b/samples/RabbitMqEventBus/App1/App1.csproj new file mode 100644 index 0000000000..1d59d978ec --- /dev/null +++ b/samples/RabbitMqEventBus/App1/App1.csproj @@ -0,0 +1,13 @@ + + + + Exe + netcoreapp2.1 + + + + + + + + diff --git a/samples/RabbitMqEventBus/App1/App1MessagingService.cs b/samples/RabbitMqEventBus/App1/App1MessagingService.cs new file mode 100644 index 0000000000..718a0b76de --- /dev/null +++ b/samples/RabbitMqEventBus/App1/App1MessagingService.cs @@ -0,0 +1,40 @@ +using System; +using SharedModule; +using Volo.Abp.DependencyInjection; +using Volo.Abp.EventBus; +using Volo.Abp.EventBus.Distributed; + +namespace App2 +{ + public class App1MessagingService : ITransientDependency + { + private readonly IDistributedEventBus _distributedEventBus; + + public App1MessagingService(IDistributedEventBus distributedEventBus) + { + _distributedEventBus = distributedEventBus; + } + + public void Run() + { + Console.WriteLine("Press ENTER (without writing a message) to stop application..."); + Console.WriteLine(); + + string message; + do + { + message = Console.ReadLine(); + + if (!message.IsNullOrEmpty()) + { + _distributedEventBus.Publish(new TextEventData { TextMessage = message }); + } + else + { + _distributedEventBus.Publish(new TextEventData { TextMessage = "App1 is exiting. Bye bye...!" }); + } + + } while (!message.IsNullOrEmpty()); + } + } +} \ No newline at end of file diff --git a/samples/RabbitMqEventBus/App1/App1Module.cs b/samples/RabbitMqEventBus/App1/App1Module.cs new file mode 100644 index 0000000000..6eb1280dab --- /dev/null +++ b/samples/RabbitMqEventBus/App1/App1Module.cs @@ -0,0 +1,20 @@ +using Volo.Abp.EventBus.Distributed.RabbitMq; +using Volo.Abp.Modularity; + +namespace App2 +{ + [DependsOn( + typeof(AbpEventBusRabbitMqModule) + )] + public class App1Module : AbpModule + { + public override void ConfigureServices(ServiceConfigurationContext context) + { + Configure(options => + { + options.ClientName = "TestApp1"; + options.ExchangeName = "TestMessages"; + }); + } + } +} \ No newline at end of file diff --git a/samples/RabbitMqEventBus/App1/App1TextEventHandler.cs b/samples/RabbitMqEventBus/App1/App1TextEventHandler.cs new file mode 100644 index 0000000000..5ee9d2f406 --- /dev/null +++ b/samples/RabbitMqEventBus/App1/App1TextEventHandler.cs @@ -0,0 +1,34 @@ +using System; +using System.Threading.Tasks; +using SharedModule; +using Volo.Abp.DependencyInjection; +using Volo.Abp.EventBus.Distributed; + +namespace App2 +{ + public class App1TextEventHandler : IDistributedEventHandler, ITransientDependency + { + private readonly IDistributedEventBus _distributedEventBus; + + public App1TextEventHandler(IDistributedEventBus distributedEventBus) + { + _distributedEventBus = distributedEventBus; + } + + public Task HandleEventAsync(TextEventData eventData) + { + Console.WriteLine("************************ INCOMING MESSAGE ****************************"); + Console.WriteLine(eventData.TextMessage); + Console.WriteLine("**********************************************************************"); + + _distributedEventBus.PublishAsync( + new TextReceivedEventData + { + ReceivedText = eventData.TextMessage + } + ); + + return Task.CompletedTask; + } + } +} diff --git a/samples/RabbitMqEventBus/App1/App1TextReceivedEventHandler.cs b/samples/RabbitMqEventBus/App1/App1TextReceivedEventHandler.cs new file mode 100644 index 0000000000..6fe1b45eaa --- /dev/null +++ b/samples/RabbitMqEventBus/App1/App1TextReceivedEventHandler.cs @@ -0,0 +1,18 @@ +using System; +using System.Threading.Tasks; +using SharedModule; +using Volo.Abp.DependencyInjection; +using Volo.Abp.EventBus.Distributed; + +namespace App2 +{ + public class App1TextReceivedEventHandler : IDistributedEventHandler, ITransientDependency + { + public Task HandleEventAsync(TextReceivedEventData eventData) + { + Console.WriteLine("--------> App2 has received the message: " + eventData.ReceivedText.TruncateWithPostfix(32)); + + return Task.CompletedTask; + } + } +} diff --git a/samples/RabbitMqEventBus/App1/Program.cs b/samples/RabbitMqEventBus/App1/Program.cs new file mode 100644 index 0000000000..38583283c2 --- /dev/null +++ b/samples/RabbitMqEventBus/App1/Program.cs @@ -0,0 +1,24 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp; + +namespace App2 +{ + internal class Program + { + private static void Main(string[] args) + { + using (var application = AbpApplicationFactory.Create()) + { + application.Initialize(); + + var messagingService = application + .ServiceProvider + .GetRequiredService(); + + messagingService.Run(); + + application.Shutdown(); + } + } + } +} diff --git a/samples/RabbitMqEventBus/App2/App2.csproj b/samples/RabbitMqEventBus/App2/App2.csproj new file mode 100644 index 0000000000..1d59d978ec --- /dev/null +++ b/samples/RabbitMqEventBus/App2/App2.csproj @@ -0,0 +1,13 @@ + + + + Exe + netcoreapp2.1 + + + + + + + + diff --git a/samples/RabbitMqEventBus/App2/App2MessagingService.cs b/samples/RabbitMqEventBus/App2/App2MessagingService.cs new file mode 100644 index 0000000000..03ee9fe071 --- /dev/null +++ b/samples/RabbitMqEventBus/App2/App2MessagingService.cs @@ -0,0 +1,40 @@ +using System; +using SharedModule; +using Volo.Abp.DependencyInjection; +using Volo.Abp.EventBus; +using Volo.Abp.EventBus.Distributed; + +namespace App2 +{ + public class App2MessagingService : ITransientDependency + { + private readonly IDistributedEventBus _distributedEventBus; + + public App2MessagingService(IDistributedEventBus distributedEventBus) + { + _distributedEventBus = distributedEventBus; + } + + public void Run() + { + Console.WriteLine("Press ENTER (without writing a message) to stop application..."); + Console.WriteLine(); + + string message; + do + { + message = Console.ReadLine(); + + if (!message.IsNullOrEmpty()) + { + _distributedEventBus.Publish(new TextEventData { TextMessage = message }); + } + else + { + _distributedEventBus.Publish(new TextEventData { TextMessage = "App2 is exiting. Bye bye...!" }); + } + + } while (!message.IsNullOrEmpty()); + } + } +} \ No newline at end of file diff --git a/samples/RabbitMqEventBus/App2/App2Module.cs b/samples/RabbitMqEventBus/App2/App2Module.cs new file mode 100644 index 0000000000..c2a4959f2c --- /dev/null +++ b/samples/RabbitMqEventBus/App2/App2Module.cs @@ -0,0 +1,20 @@ +using Volo.Abp.EventBus.Distributed.RabbitMq; +using Volo.Abp.Modularity; + +namespace App2 +{ + [DependsOn( + typeof(AbpEventBusRabbitMqModule) + )] + public class App2Module : AbpModule + { + public override void ConfigureServices(ServiceConfigurationContext context) + { + Configure(options => + { + options.ClientName = "TestApp2"; + options.ExchangeName = "TestMessages"; + }); + } + } +} \ No newline at end of file diff --git a/samples/RabbitMqEventBus/App2/App2TextEventHandler.cs b/samples/RabbitMqEventBus/App2/App2TextEventHandler.cs new file mode 100644 index 0000000000..16eeaf945d --- /dev/null +++ b/samples/RabbitMqEventBus/App2/App2TextEventHandler.cs @@ -0,0 +1,34 @@ +using System; +using System.Threading.Tasks; +using SharedModule; +using Volo.Abp.DependencyInjection; +using Volo.Abp.EventBus.Distributed; + +namespace App2 +{ + public class App2TextEventHandler : IDistributedEventHandler, ITransientDependency + { + private readonly IDistributedEventBus _distributedEventBus; + + public App2TextEventHandler(IDistributedEventBus distributedEventBus) + { + _distributedEventBus = distributedEventBus; + } + + public Task HandleEventAsync(TextEventData eventData) + { + Console.WriteLine("************************ INCOMING MESSAGE ****************************"); + Console.WriteLine(eventData.TextMessage); + Console.WriteLine("**********************************************************************"); + + _distributedEventBus.PublishAsync( + new TextReceivedEventData + { + ReceivedText = eventData.TextMessage + } + ); + + return Task.CompletedTask; + } + } +} diff --git a/samples/RabbitMqEventBus/App2/App2TextReceivedEventHandler.cs b/samples/RabbitMqEventBus/App2/App2TextReceivedEventHandler.cs new file mode 100644 index 0000000000..308cedf7d8 --- /dev/null +++ b/samples/RabbitMqEventBus/App2/App2TextReceivedEventHandler.cs @@ -0,0 +1,18 @@ +using System; +using System.Threading.Tasks; +using SharedModule; +using Volo.Abp.DependencyInjection; +using Volo.Abp.EventBus.Distributed; + +namespace App2 +{ + public class App2TextReceivedEventHandler : IDistributedEventHandler, ITransientDependency + { + public Task HandleEventAsync(TextReceivedEventData eventData) + { + Console.WriteLine("--------> App1 has received the message: " + eventData.ReceivedText.TruncateWithPostfix(32)); + + return Task.CompletedTask; + } + } +} diff --git a/samples/RabbitMqEventBus/App2/Program.cs b/samples/RabbitMqEventBus/App2/Program.cs new file mode 100644 index 0000000000..c13d9a1dee --- /dev/null +++ b/samples/RabbitMqEventBus/App2/Program.cs @@ -0,0 +1,24 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp; + +namespace App2 +{ + internal class Program + { + private static void Main(string[] args) + { + using (var application = AbpApplicationFactory.Create()) + { + application.Initialize(); + + var messagingService = application + .ServiceProvider + .GetRequiredService(); + + messagingService.Run(); + + application.Shutdown(); + } + } + } +} diff --git a/samples/RabbitMqEventBus/RabbitMqEventBus.sln b/samples/RabbitMqEventBus/RabbitMqEventBus.sln new file mode 100644 index 0000000000..fae44b73bb --- /dev/null +++ b/samples/RabbitMqEventBus/RabbitMqEventBus.sln @@ -0,0 +1,37 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28010.2016 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "App1", "App1\App1.csproj", "{2C93AFEF-1677-4591-8245-35D5E0F99B03}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharedModule", "SharedModule\SharedModule.csproj", "{D6F948FB-699B-45D7-8B79-F9D43B627B68}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "App2", "App2\App2.csproj", "{A5FE0CEF-C472-47DD-9F2B-DFFFA33B8523}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {2C93AFEF-1677-4591-8245-35D5E0F99B03}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2C93AFEF-1677-4591-8245-35D5E0F99B03}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2C93AFEF-1677-4591-8245-35D5E0F99B03}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2C93AFEF-1677-4591-8245-35D5E0F99B03}.Release|Any CPU.Build.0 = Release|Any CPU + {D6F948FB-699B-45D7-8B79-F9D43B627B68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D6F948FB-699B-45D7-8B79-F9D43B627B68}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D6F948FB-699B-45D7-8B79-F9D43B627B68}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D6F948FB-699B-45D7-8B79-F9D43B627B68}.Release|Any CPU.Build.0 = Release|Any CPU + {A5FE0CEF-C472-47DD-9F2B-DFFFA33B8523}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A5FE0CEF-C472-47DD-9F2B-DFFFA33B8523}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A5FE0CEF-C472-47DD-9F2B-DFFFA33B8523}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A5FE0CEF-C472-47DD-9F2B-DFFFA33B8523}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {44A91B65-F109-4FD0-B6C0-63C052A5BEEB} + EndGlobalSection +EndGlobal diff --git a/samples/RabbitMqEventBus/SharedModule/SharedModule.csproj b/samples/RabbitMqEventBus/SharedModule/SharedModule.csproj new file mode 100644 index 0000000000..dbdcea46b6 --- /dev/null +++ b/samples/RabbitMqEventBus/SharedModule/SharedModule.csproj @@ -0,0 +1,7 @@ + + + + netstandard2.0 + + + diff --git a/samples/RabbitMqEventBus/SharedModule/TextEventData.cs b/samples/RabbitMqEventBus/SharedModule/TextEventData.cs new file mode 100644 index 0000000000..4b88a9e7bd --- /dev/null +++ b/samples/RabbitMqEventBus/SharedModule/TextEventData.cs @@ -0,0 +1,7 @@ +namespace SharedModule +{ + public class TextEventData + { + public string TextMessage { get; set; } + } +} diff --git a/samples/RabbitMqEventBus/SharedModule/TextReceivedEventData.cs b/samples/RabbitMqEventBus/SharedModule/TextReceivedEventData.cs new file mode 100644 index 0000000000..825fb19aac --- /dev/null +++ b/samples/RabbitMqEventBus/SharedModule/TextReceivedEventData.cs @@ -0,0 +1,7 @@ +namespace SharedModule +{ + public class TextReceivedEventData + { + public string ReceivedText { get; set; } + } +}