Monday, August 20, 2018

Exploring MassTransit InMemory Scheduled Messaging using RabbitMQ and .NET Core

On this post, let's explore MassTransit's scheduler messaging system using RabbitMQ
On a previous post, I demoed how to create a MassTransit client/server application using RabbitMQ, .Net Core and Linux. Today we will explore another very interesting functionality: the ability to Schedule messages and send them in the future.

I'm very satisfied with MassTransit so far. However, there are a few use cases that I still would like to test. On this post, we cover the scheduled message use case, testing the in-memory solution.

Persistence Requirements

Before we start, a few highlights from their web site:
Scheduled messages can be achieved using:
  • Quartz.Net in a hosted server
  • Azure Service Bus
  • RabbitMQ by installing a plugin
  • a test-driven In-Memory implementation

Because most of them are already complex enough and would require more than a simple blog post to cover, I decided to spike out the in-memory solution as I'm interested in how MassTransit handles those use cases more on how to configure the underlying storage.

Referencing Packages

MassTransit's Scheduling Api utilizes Quartz.Net. So, for it to work, we will need the to add a reference to the MassTransit.Quartz package to your project with:
$ dotnet add reference <project-name> MassTransit.Quartz --version 5.1.3
Once the reference is added and a dotnet restore is done, you'll then have the necessary extension methods to do the initialization.


The initalization code for the in-memory implementation as simple as adding a call to UseInMemoryScheduler() on your bus configuration.

Important: Using the in-memory scheduler uses non-durable storage. If the process terminates, any scheduled messages will be lost, immediately, never to be found again. For any production system, using a standalone service is recommended with persistent storage.

Source Code

This gist contains most of what you need. Remember, you need to have some instance of RabbitMQ running and point to it to make this code work. The source for this post is located on my GitHub page.

Running the Demo app

So, I run my app and my code schedules a message to be sent 3 seconds after sent by the user. This is my output:


Hope this serves as an introduction to the scheduling feature within MassTransit. Being a not very popular framework, sometimes it's difficult to find sample code. Hope this helps you with this feature.

Next week I will revise sagas. And I would still like to review transactions, auditing and who knows, creating a real world scenario in which a web app communicates to the backend using MassTransit as an intermediary.

More about MassTransit?

 In case you're interested, consider reading:

See Also

For more posts on MassTransit, please click here.
Do you have any comment or suggestion about this post? Please contact me @BrunoHilden