Skip to content

RabbitMQ

MassiveJobs needs RabbitMQ 4.4 or later, and checks it when it connects: on an older server it refuses to start. 4.4 added delayed delivery to quorum queues (x-opt-delivery-time), which scheduled jobs, retries and periodic jobs rely on. No plugin is needed.

The simplest is a connection string, an AMQP URI:

.UseRabbitMq(o => o.ConnectionString = "amqps://shop:secret@rabbit.example.com/shop")

Or the properties, e.g. for a cluster:

.UseRabbitMq(o =>
{
o.HostNames = ["rabbit-1.internal", "rabbit-2.internal", "rabbit-3.internal"];
o.VirtualHost = "shop";
o.Username = "shop";
o.Password = builder.Configuration["RabbitMq:Password"];
o.SslEnabled = true;
})

Each app opens one connection, named MassiveJobs.NET/{your app's assembly} in the management UI, with channels for publishing and for each worker. A lost connection is re-established: each worker restarts on its own, with a backoff of 1 to 60 seconds, and publishing reconnects on demand. Meanwhile the health check reports the workers as degraded.

At its first connection, an app declares (idempotently) the exchange and the queues it uses, with NamePrefix in front:

  • the direct exchange massivejobs.direct, each queue bound to it by its own name;
  • the job queues (immediate, long-running, periodic): quorum queues with
    • x-dead-letter-exchange and x-dead-letter-routing-key: the failed queue,
    • x-dead-letter-strategy = at-least-once, x-overflow = reject-publish,
    • x-max-length = MaxQueueLength, unless it’s NoLimit,
    • and on the periodic queue x-single-active-consumer;
  • the failed queue: a quorum queue with x-delivery-limit = -1, so that looking at it doesn’t use up its messages’ deliveries.

The user needs the configure, write and read permissions on these in its virtual host.

Quorum queues have a delivery limit of 20: a message delivered 20 times without being acknowledged (the job crashes the process every time, or the app keeps returning it while stopping) goes to the failed queue.

RabbitMQ refuses to declare an existing queue with other arguments (PRECONDITION_FAILED - inequivalent arg). After changing MaxQueueLength on a running system, delete the queues first, or use a new NamePrefix.

Terminal window
docker run -d --name rabbit -p 5672:5672 -p 15672:15672 rabbitmq:4-management

The management UI is on localhost:15672, user and password guest. Quorum queues are stored on disk: docker rm -fv rabbit removes the container with its data.

MassiveJobs.Core also has an in-memory broker (UseInMemoryBroker()), for tests and for single-process apps: jobs run in the process that published them, and are lost when it stops.