Finalized RabbitMqEventBus sample.

pull/666/head
Halil ibrahim Kalkan 7 years ago
parent 55981f714c
commit 40fa063c69

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

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

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

@ -6,6 +6,7 @@
</PropertyGroup>
<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="..\SharedModule\SharedModule.csproj" />
</ItemGroup>

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

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

@ -4,9 +4,12 @@ using SharedModule;
using Volo.Abp.DependencyInjection;
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;
@ -15,18 +18,13 @@ namespace App2
_distributedEventBus = distributedEventBus;
}
public Task HandleEventAsync(TextEventData eventData)
public Task HandleEventAsync(App2ToApp1TextEventData eventData)
{
Console.WriteLine("************************ INCOMING MESSAGE ****************************");
Console.WriteLine(eventData.TextMessage);
Console.WriteLine("**********************************************************************");
_distributedEventBus.PublishAsync(
new TextReceivedEventData
{
ReceivedText = eventData.TextMessage
}
);
_distributedEventBus.PublishAsync(new App1TextReceivedEventData(eventData.TextMessage));
return Task.CompletedTask;
}

@ -4,11 +4,14 @@ using SharedModule;
using Volo.Abp.DependencyInjection;
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));

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

@ -6,6 +6,7 @@
</PropertyGroup>
<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="..\SharedModule\SharedModule.csproj" />
</ItemGroup>

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

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

@ -6,7 +6,10 @@ using Volo.Abp.EventBus.Distributed;
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;
@ -15,18 +18,13 @@ namespace App2
_distributedEventBus = distributedEventBus;
}
public Task HandleEventAsync(TextEventData eventData)
public Task HandleEventAsync(App1ToApp2TextEventData eventData)
{
Console.WriteLine("************************ INCOMING MESSAGE ****************************");
Console.WriteLine(eventData.TextMessage);
Console.WriteLine("**********************************************************************");
_distributedEventBus.PublishAsync(
new TextReceivedEventData
{
ReceivedText = eventData.TextMessage
}
);
_distributedEventBus.PublishAsync(new App2TextReceivedEventData(eventData.TextMessage));
return Task.CompletedTask;
}

@ -6,9 +6,12 @@ using Volo.Abp.EventBus.Distributed;
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));

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

@ -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;
}
}
}

@ -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;
}
}
}

@ -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;
}
}
}

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

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

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