In the previous post we set up the local RabbitMq environment. We also created a simple console application that built the message queue where the DDD demo project and the simulated financial application will communicate. We also prepared the way for the completion of the messaging process by creating an app setting reader and adding the RabbitMq related settings to web config.

In this post we’ll complete the demo by sending a message to the queue and reading it from the dummy financial console application.

The messaging event handler

Let’s implement the RabbitMq domain event handler. First add the same RabbitMq.Client NuGet package to the WebSuiteDemo.Loadtesting.ApplicationServices project as in the previous post:

Next we’ll add the following implementation of the IDomainEventHandler interface into the Implementations folder of the ApplicationServices layer

We extract the queue-related properties such as host, password etc. from the configuration repository. We also provide some default names if the settings do not exist. The RabbitMq related code is as simple as it gets. We just build the connection and post a byte array to the relevant queue.

That’s it, we don’t need to do anything else. There are no extensions to the Timetable or the TimetableServices class. They are happily unaware of our new event handler.

Start the Web API demo and add a new load test through the test console application like before. It should trigger both event handlers. Check the changes in the RabbitMq web console. The message was registered.

The following graphs show the message increase from 0 to 1:

And the following table shows that there’s one message waiting in the queue:

We’ll now set up the WebSuiteDDD.FinancialPlanner project as well. Here’s the code for Program.cs that will monitor the queue:

The ReceiveMessages method will read and acknowledge the messages from the queue called LoadtestEventMessageQueue. If you run the FinancialPlanner app now it should read the message from the queue and print it in the console window.

We can now start playing with our system. Start both the Web API Demo project and FinancialPlanner console app. Add a couple of load tests and you’ll see that both event handlers are triggered, i.e. we send out an email and also publish a message to RabbitMq. The message is then processed by the FinancialPlanner. Here’s the message shown in the planner application: