Browse Source

Finalized RabbitMqEventBus sample.

pull/666/head
Halil ibrahim Kalkan 8 years ago
parent
commit
40fa063c69
  1. 4
      framework/src/Volo.Abp.EventBus.RabbitMQ/Volo.Abp.EventBus.RabbitMQ.csproj
  2. 10
      framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs
  3. 6
      framework/src/Volo.Abp.RabbitMQ/Volo/Abp/RabbitMQ/RabbitMqMessageConsumer.cs
  4. 1
      samples/RabbitMqEventBus/App1/App1.csproj
  5. 14
      samples/RabbitMqEventBus/App1/App1MessagingService.cs
  6. 8
      samples/RabbitMqEventBus/App1/App1Module.cs
  7. 16
      samples/RabbitMqEventBus/App1/App1TextEventHandler.cs
  8. 9
      samples/RabbitMqEventBus/App1/App1TextReceivedEventHandler.cs
  9. 9
      samples/RabbitMqEventBus/App1/Program.cs
  10. 1
      samples/RabbitMqEventBus/App2/App2.csproj
  11. 12
      samples/RabbitMqEventBus/App2/App2MessagingService.cs
  12. 6
      samples/RabbitMqEventBus/App2/App2Module.cs
  13. 14
      samples/RabbitMqEventBus/App2/App2TextEventHandler.cs
  14. 7
      samples/RabbitMqEventBus/App2/App2TextReceivedEventHandler.cs
  15. 5
      samples/RabbitMqEventBus/App2/Program.cs
  16. 23
      samples/RabbitMqEventBus/SharedModule/App1TextReceivedEventData.cs
  17. 23
      samples/RabbitMqEventBus/SharedModule/App1ToApp2TextEventData.cs
  18. 23
      samples/RabbitMqEventBus/SharedModule/App2TextReceivedEventData.cs
  19. 23
      samples/RabbitMqEventBus/SharedModule/App2ToApp1TextEventData.cs
  20. 4
      samples/RabbitMqEventBus/SharedModule/SharedModule.csproj
  21. 7
      samples/RabbitMqEventBus/SharedModule/TextEventData.cs
  22. 7
      samples/RabbitMqEventBus/SharedModule/TextReceivedEventData.cs

4
framework/src/Volo.Abp.EventBus.RabbitMQ/Volo.Abp.EventBus.RabbitMQ.csproj

@ -4,8 +4,8 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>Volo.Abp.EventBus.Distributed.RabbitMQ</AssemblyName> <AssemblyName>Volo.Abp.EventBus.RabbitMQ</AssemblyName>
<PackageId>Volo.Abp.EventBus.Distributed.RabbitMQ</PackageId> <PackageId>Volo.Abp.EventBus.RabbitMQ</PackageId>
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback> <AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> <GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> <GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>

10
framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs

@ -38,7 +38,7 @@ namespace Volo.Abp.EventBus.Distributed.RabbitMq
IHybridServiceScopeFactory serviceScopeFactory, IHybridServiceScopeFactory serviceScopeFactory,
IOptions<DistributedEventBusOptions> distributedEventBusOptions, IOptions<DistributedEventBusOptions> distributedEventBusOptions,
IRabbitMqMessageConsumerFactory messageConsumerFactory) IRabbitMqMessageConsumerFactory messageConsumerFactory)
: base(serviceScopeFactory) : base(serviceScopeFactory)
{ {
ConnectionPool = connectionPool; ConnectionPool = connectionPool;
Serializer = serializer; Serializer = serializer;
@ -52,7 +52,8 @@ namespace Volo.Abp.EventBus.Distributed.RabbitMq
Consumer = MessageConsumerFactory.Create( Consumer = MessageConsumerFactory.Create(
new ExchangeDeclareConfiguration( new ExchangeDeclareConfiguration(
RabbitMqDistributedEventBusOptions.ExchangeName, RabbitMqDistributedEventBusOptions.ExchangeName,
type: "direct" type: "direct",
durable: true
), ),
new QueueDeclareConfiguration( new QueueDeclareConfiguration(
RabbitMqDistributedEventBusOptions.ClientName, RabbitMqDistributedEventBusOptions.ClientName,
@ -96,7 +97,6 @@ namespace Volo.Abp.EventBus.Distributed.RabbitMq
if (handlerFactories.Count == 1) //TODO: Multi-threading! if (handlerFactories.Count == 1) //TODO: Multi-threading!
{ {
var eventName = EventNameAttribute.GetNameOrDefault(eventType); var eventName = EventNameAttribute.GetNameOrDefault(eventType);
Consumer.BindAsync(eventName); Consumer.BindAsync(eventName);
} }
@ -166,8 +166,8 @@ namespace Volo.Abp.EventBus.Distributed.RabbitMq
{ {
channel.ExchangeDeclare( channel.ExchangeDeclare(
RabbitMqDistributedEventBusOptions.ExchangeName, RabbitMqDistributedEventBusOptions.ExchangeName,
"direct" "direct",
//TODO: Other properties like durable? durable: true
); );
var properties = channel.CreateBasicProperties(); var properties = channel.CreateBasicProperties();

6
framework/src/Volo.Abp.RabbitMQ/Volo/Abp/RabbitMQ/RabbitMqMessageConsumer.cs

@ -145,7 +145,10 @@ namespace Volo.Abp.RabbitMQ
channel.ExchangeDeclare( channel.ExchangeDeclare(
exchange: Exchange.ExchangeName, exchange: Exchange.ExchangeName,
type: Exchange.Type type: Exchange.Type,
durable: Exchange.Durable,
autoDelete: Exchange.AutoDelete,
arguments: Exchange.Arguments
); );
channel.QueueDeclare( channel.QueueDeclare(
@ -212,6 +215,7 @@ namespace Volo.Abp.RabbitMQ
public virtual void Dispose() public virtual void Dispose()
{ {
Timer.Stop();
DisposeChannel(); DisposeChannel();
} }

1
samples/RabbitMqEventBus/App1/App1.csproj

@ -6,6 +6,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\framework\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" />
<ProjectReference Include="..\..\..\framework\src\Volo.Abp.EventBus.RabbitMQ\Volo.Abp.EventBus.RabbitMQ.csproj" /> <ProjectReference Include="..\..\..\framework\src\Volo.Abp.EventBus.RabbitMQ\Volo.Abp.EventBus.RabbitMQ.csproj" />
<ProjectReference Include="..\SharedModule\SharedModule.csproj" /> <ProjectReference Include="..\SharedModule\SharedModule.csproj" />
</ItemGroup> </ItemGroup>

14
samples/RabbitMqEventBus/App1/App1MessagingService.cs

@ -4,7 +4,7 @@ using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus; using Volo.Abp.EventBus;
using Volo.Abp.EventBus.Distributed; using Volo.Abp.EventBus.Distributed;
namespace App2 namespace App1
{ {
public class App1MessagingService : ITransientDependency public class App1MessagingService : ITransientDependency
{ {
@ -17,21 +17,25 @@ namespace App2
public void Run() public void Run()
{ {
Console.WriteLine("Press ENTER (without writing a message) to stop application..."); Console.WriteLine("*** Started the APPLICATION 1 ***");
Console.WriteLine(); Console.WriteLine("Write a message and press ENTER to send to the App2.");
Console.WriteLine("Press ENTER (without writing a message) to stop the application.");
string message; string message;
do do
{ {
Console.WriteLine();
Console.WriteLine("Send message to App2: ");
message = Console.ReadLine(); message = Console.ReadLine();
if (!message.IsNullOrEmpty()) if (!message.IsNullOrEmpty())
{ {
_distributedEventBus.Publish(new TextEventData { TextMessage = message }); _distributedEventBus.Publish(new App1ToApp2TextEventData(message));
} }
else else
{ {
_distributedEventBus.Publish(new TextEventData { TextMessage = "App1 is exiting. Bye bye...!" }); _distributedEventBus.Publish(new App1ToApp2TextEventData("App1 is exiting. Bye bye...!"));
} }
} while (!message.IsNullOrEmpty()); } while (!message.IsNullOrEmpty());

8
samples/RabbitMqEventBus/App1/App1Module.cs

@ -1,10 +1,12 @@
using Volo.Abp.EventBus.Distributed.RabbitMq; using Volo.Abp.Autofac;
using Volo.Abp.EventBus.Distributed.RabbitMq;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
namespace App2 namespace App1
{ {
[DependsOn( [DependsOn(
typeof(AbpEventBusRabbitMqModule) typeof(AbpEventBusRabbitMqModule),
typeof(AbpAutofacModule)
)] )]
public class App1Module : AbpModule public class App1Module : AbpModule
{ {

16
samples/RabbitMqEventBus/App1/App1TextEventHandler.cs

@ -4,9 +4,12 @@ using SharedModule;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Distributed; using Volo.Abp.EventBus.Distributed;
namespace App2 namespace App1
{ {
public class App1TextEventHandler : IDistributedEventHandler<TextEventData>, ITransientDependency /// <summary>
/// Used to listen messages sent to App2 by App1.
/// </summary>
public class App1TextEventHandler : IDistributedEventHandler<App2ToApp1TextEventData>, ITransientDependency
{ {
private readonly IDistributedEventBus _distributedEventBus; private readonly IDistributedEventBus _distributedEventBus;
@ -15,18 +18,13 @@ namespace App2
_distributedEventBus = distributedEventBus; _distributedEventBus = distributedEventBus;
} }
public Task HandleEventAsync(TextEventData eventData) public Task HandleEventAsync(App2ToApp1TextEventData eventData)
{ {
Console.WriteLine("************************ INCOMING MESSAGE ****************************"); Console.WriteLine("************************ INCOMING MESSAGE ****************************");
Console.WriteLine(eventData.TextMessage); Console.WriteLine(eventData.TextMessage);
Console.WriteLine("**********************************************************************"); Console.WriteLine("**********************************************************************");
_distributedEventBus.PublishAsync( _distributedEventBus.PublishAsync(new App1TextReceivedEventData(eventData.TextMessage));
new TextReceivedEventData
{
ReceivedText = eventData.TextMessage
}
);
return Task.CompletedTask; return Task.CompletedTask;
} }

9
samples/RabbitMqEventBus/App1/App1TextReceivedEventHandler.cs

@ -4,11 +4,14 @@ using SharedModule;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Distributed; using Volo.Abp.EventBus.Distributed;
namespace App2 namespace App1
{ {
public class App1TextReceivedEventHandler : IDistributedEventHandler<TextReceivedEventData>, ITransientDependency /// <summary>
/// Used to know when App2 has received a message sent by App1.
/// </summary>
public class App1TextReceivedEventHandler : IDistributedEventHandler<App2TextReceivedEventData>, ITransientDependency
{ {
public Task HandleEventAsync(TextReceivedEventData eventData) public Task HandleEventAsync(App2TextReceivedEventData eventData)
{ {
Console.WriteLine("--------> App2 has received the message: " + eventData.ReceivedText.TruncateWithPostfix(32)); Console.WriteLine("--------> App2 has received the message: " + eventData.ReceivedText.TruncateWithPostfix(32));

9
samples/RabbitMqEventBus/App1/Program.cs

@ -1,16 +1,21 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp; using Volo.Abp;
namespace App2 namespace App1
{ {
internal class Program internal class Program
{ {
private static void Main(string[] args) private static void Main(string[] args)
{ {
using (var application = AbpApplicationFactory.Create<App1Module>()) using (var application = AbpApplicationFactory.Create<App1Module>(options =>
{
options.UseAutofac();
}))
{ {
application.Initialize(); application.Initialize();
var x = application.ServiceProvider.GetRequiredService<App1TextEventHandler>();
var messagingService = application var messagingService = application
.ServiceProvider .ServiceProvider
.GetRequiredService<App1MessagingService>(); .GetRequiredService<App1MessagingService>();

1
samples/RabbitMqEventBus/App2/App2.csproj

@ -6,6 +6,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\framework\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" />
<ProjectReference Include="..\..\..\framework\src\Volo.Abp.EventBus.RabbitMQ\Volo.Abp.EventBus.RabbitMQ.csproj" /> <ProjectReference Include="..\..\..\framework\src\Volo.Abp.EventBus.RabbitMQ\Volo.Abp.EventBus.RabbitMQ.csproj" />
<ProjectReference Include="..\SharedModule\SharedModule.csproj" /> <ProjectReference Include="..\SharedModule\SharedModule.csproj" />
</ItemGroup> </ItemGroup>

12
samples/RabbitMqEventBus/App2/App2MessagingService.cs

@ -17,21 +17,25 @@ namespace App2
public void Run() public void Run()
{ {
Console.WriteLine("Press ENTER (without writing a message) to stop application..."); Console.WriteLine("*** Started the APPLICATION 2 ***");
Console.WriteLine(); Console.WriteLine("Write a message and press ENTER to send to the App1.");
Console.WriteLine("Press ENTER (without writing a message) to stop the application...");
string message; string message;
do do
{ {
Console.WriteLine();
Console.WriteLine("Send message to App1: ");
message = Console.ReadLine(); message = Console.ReadLine();
if (!message.IsNullOrEmpty()) if (!message.IsNullOrEmpty())
{ {
_distributedEventBus.Publish(new TextEventData { TextMessage = message }); _distributedEventBus.Publish(new App2ToApp1TextEventData(message));
} }
else else
{ {
_distributedEventBus.Publish(new TextEventData { TextMessage = "App2 is exiting. Bye bye...!" }); _distributedEventBus.Publish(new App2ToApp1TextEventData("App2 is exiting. Bye bye...!"));
} }
} while (!message.IsNullOrEmpty()); } while (!message.IsNullOrEmpty());

6
samples/RabbitMqEventBus/App2/App2Module.cs

@ -1,10 +1,12 @@
using Volo.Abp.EventBus.Distributed.RabbitMq; using Volo.Abp.Autofac;
using Volo.Abp.EventBus.Distributed.RabbitMq;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
namespace App2 namespace App2
{ {
[DependsOn( [DependsOn(
typeof(AbpEventBusRabbitMqModule) typeof(AbpEventBusRabbitMqModule),
typeof(AbpAutofacModule)
)] )]
public class App2Module : AbpModule public class App2Module : AbpModule
{ {

14
samples/RabbitMqEventBus/App2/App2TextEventHandler.cs

@ -6,7 +6,10 @@ using Volo.Abp.EventBus.Distributed;
namespace App2 namespace App2
{ {
public class App2TextEventHandler : IDistributedEventHandler<TextEventData>, ITransientDependency /// <summary>
/// Used to listen messages sent to App2 by App1.
/// </summary>
public class App2TextEventHandler : IDistributedEventHandler<App1ToApp2TextEventData>, ITransientDependency
{ {
private readonly IDistributedEventBus _distributedEventBus; private readonly IDistributedEventBus _distributedEventBus;
@ -15,18 +18,13 @@ namespace App2
_distributedEventBus = distributedEventBus; _distributedEventBus = distributedEventBus;
} }
public Task HandleEventAsync(TextEventData eventData) public Task HandleEventAsync(App1ToApp2TextEventData eventData)
{ {
Console.WriteLine("************************ INCOMING MESSAGE ****************************"); Console.WriteLine("************************ INCOMING MESSAGE ****************************");
Console.WriteLine(eventData.TextMessage); Console.WriteLine(eventData.TextMessage);
Console.WriteLine("**********************************************************************"); Console.WriteLine("**********************************************************************");
_distributedEventBus.PublishAsync( _distributedEventBus.PublishAsync(new App2TextReceivedEventData(eventData.TextMessage));
new TextReceivedEventData
{
ReceivedText = eventData.TextMessage
}
);
return Task.CompletedTask; return Task.CompletedTask;
} }

7
samples/RabbitMqEventBus/App2/App2TextReceivedEventHandler.cs

@ -6,9 +6,12 @@ using Volo.Abp.EventBus.Distributed;
namespace App2 namespace App2
{ {
public class App2TextReceivedEventHandler : IDistributedEventHandler<TextReceivedEventData>, ITransientDependency /// <summary>
/// Used to know when App1 has received a message sent by App2.
/// </summary>
public class App2TextReceivedEventHandler : IDistributedEventHandler<App1TextReceivedEventData>, ITransientDependency
{ {
public Task HandleEventAsync(TextReceivedEventData eventData) public Task HandleEventAsync(App1TextReceivedEventData eventData)
{ {
Console.WriteLine("--------> App1 has received the message: " + eventData.ReceivedText.TruncateWithPostfix(32)); Console.WriteLine("--------> App1 has received the message: " + eventData.ReceivedText.TruncateWithPostfix(32));

5
samples/RabbitMqEventBus/App2/Program.cs

@ -7,7 +7,10 @@ namespace App2
{ {
private static void Main(string[] args) private static void Main(string[] args)
{ {
using (var application = AbpApplicationFactory.Create<App2Module>()) using (var application = AbpApplicationFactory.Create<App2Module>(options =>
{
options.UseAutofac();
}))
{ {
application.Initialize(); application.Initialize();

23
samples/RabbitMqEventBus/SharedModule/App1TextReceivedEventData.cs

@ -0,0 +1,23 @@
using Volo.Abp.EventBus;
namespace SharedModule
{
/// <summary>
/// Used to indicate that App2 has received a text message.
/// </summary>
[EventName("Test.App1TextReceived")] //Optional event name
public class App1TextReceivedEventData
{
public string ReceivedText { get; set; }
public App1TextReceivedEventData()
{
}
public App1TextReceivedEventData(string receivedText)
{
ReceivedText = receivedText;
}
}
}

23
samples/RabbitMqEventBus/SharedModule/App1ToApp2TextEventData.cs

@ -0,0 +1,23 @@
using Volo.Abp.EventBus;
namespace SharedModule
{
/// <summary>
/// Used to send a text message from App1 to App2.
/// </summary>
[EventName("Test.App1ToApp2Text")] //Optional event name
public class App1ToApp2TextEventData
{
public string TextMessage { get; set; }
public App1ToApp2TextEventData()
{
}
public App1ToApp2TextEventData(string textMessage)
{
TextMessage = textMessage;
}
}
}

23
samples/RabbitMqEventBus/SharedModule/App2TextReceivedEventData.cs

@ -0,0 +1,23 @@
using Volo.Abp.EventBus;
namespace SharedModule
{
/// <summary>
/// Used to indicate that App2 has received a text message.
/// </summary>
[EventName("Test.App2TextReceived")] //Optional event name
public class App2TextReceivedEventData
{
public string ReceivedText { get; set; }
public App2TextReceivedEventData()
{
}
public App2TextReceivedEventData(string receivedText)
{
ReceivedText = receivedText;
}
}
}

23
samples/RabbitMqEventBus/SharedModule/App2ToApp1TextEventData.cs

@ -0,0 +1,23 @@
using Volo.Abp.EventBus;
namespace SharedModule
{
/// <summary>
/// Used to send a text message from App2 to App1.
/// </summary>
[EventName("Test.App2ToApp1Text")] //Optional event name
public class App2ToApp1TextEventData
{
public string TextMessage { get; set; }
public App2ToApp1TextEventData()
{
}
public App2ToApp1TextEventData(string textMessage)
{
TextMessage = textMessage;
}
}
}

4
samples/RabbitMqEventBus/SharedModule/SharedModule.csproj

@ -4,4 +4,8 @@
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\framework\src\Volo.Abp.EventBus\Volo.Abp.EventBus.csproj" />
</ItemGroup>
</Project> </Project>

7
samples/RabbitMqEventBus/SharedModule/TextEventData.cs

@ -1,7 +0,0 @@
namespace SharedModule
{
public class TextEventData
{
public string TextMessage { get; set; }
}
}

7
samples/RabbitMqEventBus/SharedModule/TextReceivedEventData.cs

@ -1,7 +0,0 @@
namespace SharedModule
{
public class TextReceivedEventData
{
public string ReceivedText { get; set; }
}
}
Loading…
Cancel
Save