integration.amqp.subscriber - grecosoft/NetFusion GitHub Wiki
This section discusses how to subscribe to Queues and Topics. The consuming application subscribes to queues and topics by decorating handler methods with the Queue and Topic attributes. When a message is received, the corresponding handler is invoked having a parameter matching the command or domain-event.
As with all message handlers in NetFusion, the method can be synchronous or asynchronous and can be easily changed based on the handler's needed implementation.
The handler methods, marked with the Queue and Topic attributes need to know which configured AMQP host to subscribe. The AMQP host is specified by decorating the handler's class with the Host attribute. The host attribute, takes as a parameter the host name specified within the application's configuration. For this example, the host name would be: claims-bus.
This section defines a class that will handle the CreateClaimSubmission command and ClaimStatusUpdated domain-event. Within the Demo.Subscriber project, define the below classes.
So there is no coupling between the publishing and subscribing applications, define versions of the command and domain-event with the same structure as declared within the Demo.Domain assembly. The Demo.Subscriber can optionally reference the Demo.Domain project. This is not being done within this example since the publisher and subscriber will often not be part of the same business application. Define the following within the Commands and Events directories of the Demo.Subscriber project:
touch ./src/Demo.Subscriber/Commands/CreateClaimSubmission.cs
nano ./src/Demo.Subscriber/Commands/CreateClaimSubmission.cs
using NetFusion.Messaging.Types;
namespace Demo.Subscriber.Commands
{
public class CreateClaimSubmission : Command
{
public string InsuredId { get; set; }
public string InsuredFistName { get; set; }
public string InsuredLastName { get; set; }
public decimal InsuredDeductible { get; set; }
public decimal ClaimEstimate { get; set; }
public string ClaimDescription { get; set; }
}
}
touch ./src/Demo.Subscriber/Events/ClaimStatusUpdated.cs
nano ./src/Demo.Subscriber/Events/ClaimStatusUpdated.cs
using System;
using NetFusion.Messaging.Types;
namespace Demo.Subscriber.Events
{
public class ClaimStatusUpdated : DomainEvent
{
public string InsuredId { get; set; }
public string CurrentStatus { get; set; }
public DateTime NextStatusUpdate { get; set; }
public string NextStatus { get; set; }
}
}
Next, define the message handler class:
dotnet add ./src/Demo.Subscriber/Demo.Subscriber.csproj package NetFusion.AMQP
touch ./src/Demo.Subscriber/SampleAmqpHandler.cs
nano ./src/Demo.Subscriber/SampleAmqpHandler.cs
using System;
using NetFusion.AMQP.Subscriber;
using NetFusion.Common.Extensions;
using NetFusion.Messaging;
using Demo.Subscriber.Commands;
using Demo.Subscriber.Events;
namespace Demo.Subscriber
{
[Host("claims-bus")]
public class SampleAmqpHandler : IMessageConsumer
{
[Queue("claim-submissions")]
public void OnClaimSubmission(CreateClaimSubmission command)
{
Console.WriteLine(command.ToIndentedJson());
}
[Topic("claim-status-notification", "all-notifications")]
public void OnClaimStatus(ClaimStatusUpdated domainEvent)
{
Console.WriteLine(domainEvent.ToIndentedJson());
}
}
}
Lastly, the client needs the same AMQP as contained within the publishing Web application. Add the the following configuration to the appsettings.json within the Service.Client project:
{
"netfusion": {
"amqp": {
"hosts": [
{
"hostName": "claims-bus",
"hostAddress": "claims-bus.servicebus.windows.net",
"username": "Service-Bus-User",
"password": "Service-Bus-User-Password"
}
]
}
}
}
Also, the NetFusion.AMQP NuGet containing the AMQP plugin must be referenced and added to the composite-container by calling the AddAmqp method:
public void ConfigureServices(IServiceCollection services)
{
services.CompositeContainer(_configuration, new SerilogExtendedLogger())
.AddAmqp()
.AddPlugin<InfraPlugin>()
.AddPlugin<AppPlugin>()
.AddPlugin<DomainPlugin>()
.AddPlugin<WebApiPlugin>()
.Compose();
services.AddControllers();
}
This section publishes command and domain-event messages to the AmqpController defined within the Demo.WebApi project. Before executing the below examples, make sure the Demo.Subscriber application is running. After starting the Demo.WebApi project, execute the following within Postman:
After the above is request is made, a log message should appear on the console of the Demo.Subscriber application for the published command.
After the above is request is made, a log message should appear on the console of the Demo.Subscriber application for the published domain-event.