Skip to content

Getting started

MassiveJobs publishes jobs to RabbitMQ. Any app can publish, and every app that runs workers takes jobs from the same queues. This page gets one app doing both, then a second app sharing the work.

  • RabbitMQ 4.4 or later. Scheduled jobs and retries use the delayed delivery of quorum queues, new in 4.4. MassiveJobs refuses to connect to an older server.
  • .NET 10 or 11, or anything that runs netstandard2.0: .NET 8 and 9, and .NET Framework 4.6.2 or later.
  • A C# compiler from the .NET 8 SDK or Visual Studio 2022 17.8 on, in the projects that register jobs: MassiveJobs’ source generator runs there.
  1. Start RabbitMQ. In a container, with the management UI on localhost:15672 (user and password guest):

    Terminal window
    docker run -d --name rabbit -p 5672:5672 -p 15672:15672 rabbitmq:4-management
  2. Create an app and add the package. A worker service here; an ASP.NET Core app works the same way.

    Terminal window
    dotnet new worker -n Shop.Worker
    cd Shop.Worker
    dotnet add package MassiveJobs.RabbitMqBroker

    MassiveJobs.RabbitMqBroker brings MassiveJobs.Core with it.

  3. Write a job. A job is a class with a Perform method. Derive it from Job<TJob, TArgs> (or JobAsync<TJob, TArgs> for an async one), where TArgs is what you pass when you publish it:

    // SendWelcomeEmail.cs
    using MassiveJobs.Core;
    public class SendWelcomeEmail(ILogger<SendWelcomeEmail> logger) : JobAsync<SendWelcomeEmail, int>
    {
    public override async Task Perform(int customerId, CancellationToken cancellationToken)
    {
    await Task.Delay(500, cancellationToken); // the email
    logger.LogInformation("Welcome email sent to customer {CustomerId}", customerId);
    }
    }

    The constructor gets its parameters from your services, in a scope created for each run.

  4. Register MassiveJobs. In Program.cs, add MassiveJobs, the jobs of this project, and RabbitMQ:

    // Program.cs
    var builder = Host.CreateApplicationBuilder(args);
    builder.Services.AddMassiveJobs()
    .WithJobsFromAssemblyOf<SendWelcomeEmail>()
    .UseRabbitMq(); // localhost, as guest
    builder.Services.AddHostedService<Worker>();
    builder.Build().Run();

    The host starts the workers with the app and stops them with it.

  5. Publish. Inject IJobPublisher where you need it. In the template’s Worker, publish a job every few seconds:

    // Worker.cs
    using MassiveJobs.Core;
    public class Worker(IServiceScopeFactory scopes) : BackgroundService
    {
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
    for (var customerId = 1; !stoppingToken.IsCancellationRequested; customerId++)
    {
    using (var scope = scopes.CreateScope())
    {
    var publisher = scope.ServiceProvider.GetRequiredService<IJobPublisher>();
    await publisher.PublishAsync<SendWelcomeEmail, int>(customerId, cancellationToken: stoppingToken);
    }
    await Task.Delay(2000, stoppingToken);
    }
    }
    }

    IJobPublisher is scoped: a controller or a minimal API handler just takes it as a parameter.

  6. Run it, twice. dotnet run in two terminals. Both publish, and the jobs are shared: each one runs once, in either app.

Each app that runs workers consumes 4 immediate queues, one job at a time each, and 2 long-running queues for jobs published with a timeout of 10 seconds or more. A job that throws is retried 25 times with growing delays, then kept in the failed queue. All of it is in Configuration; how the queues are shared is in Queues and workers.

Apps that share jobs must agree on a few things: the job tags, the queue counts, and the name prefix. The defaults agree.

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMassiveJobs()
.WithJobsFromAssemblyOf<Program>()
.UseRabbitMq(o => o.ConnectionString = builder.Configuration.GetConnectionString("RabbitMq"));
var app = builder.Build();
app.MapPost("/customers/{id:int}/welcome", async (int id, IJobPublisher publisher) =>
{
await publisher.PublishAsync<SendWelcomeEmail, int>(id);
return Results.Accepted();
});
app.Run();