We are leaving Russia. We are against aggression and war with Ukraine. It is a tragedy for our nations, it is a nightmare

How use multiple queues with MSMQ?

Hi, I wonder if anyone could help me with setting up HangFire 1.4.0 with MSMQ and multiple queues.

I have one MVC Web app that creates jobs (when I hit a given page) and one console app that executes all jobs

the MVC web app is set up as follows:

public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);

            GlobalConfiguration.Configuration
             .UseSqlServerStorage("Server=localhost; Database=HangFire; Integrated Security=SSPI;")
            .UseMsmqQueues(@".\hangfire-{0}", "critical", "default"); 
            app.UseHangfireDashboard(VirtualPathUtility.ToAbsolute("~/BackgroundJobs"));
        }

I enqueue the jobs like this:

BackgroundJob.Enqueue(() => Message.WriteCriticalStuff("I'm critical " + new Random().Next()));
  BackgroundJob.Enqueue(() => Message.WriteDefaultStuff("I'm default " + new Random().Next()));

Where the messages are defined like this:

    [Queue("critical")]
    public static void WriteCriticalStuff(string str)
    {
        Console.WriteLine(str);
    }

    [Queue("default")]
    public static void WriteDefaultStuff(string str)
    {
        Console.WriteLine(str);
    }

My console app is basically jsut this:

GlobalConfiguration.Configuration
           .UseSqlServerStorage("Server=localhost; Database=HangFire; Integrated Security=SSPI;")
           .UseMsmqQueues(@".\hangfire-{0}", "critical", "default");

            using (var server = new BackgroundJobServer())
            {
                Console.WriteLine("Hangfire Server started. Press any key to exit...");
                Console.ReadKey();
            }

I cannot for my life get any other message than the ones using the default queue all others are stuck in the database
as ‘enqueued’. Anyone have a clue? Would really appreciate any help.

Edit: I should mention that I have created 2 public MSMQ queues: hangfire-default and hangfire-critical. Both queues have given full permissions to the Everyone user group.As said the hangfire-default seems to be working fine, the other one not so much.

OK, I figured out where it went wrong.

In the job processor. It’s not enough to specify that you use multiple queues in the .UseMsmqQueues() method call. You also need to specify the queues in the BackgroundJobServerOptions:

GlobalConfiguration.Configuration
           .UseSqlServerStorage("Server=localhost; Database=HangFire; Integrated Security=SSPI;")
           .UseMsmqQueues(@".\hangfire-{0}", "critical", "default");
            


            using (var server = new BackgroundJobServer(
                new BackgroundJobServerOptions
                {
                    Queues = new[] {"critical", "default"}
                }))
            {
                Console.WriteLine("Hangfire Server started. Press any key to exit...");
                Console.ReadKey();
            }

This will cause the job processor server to read the other queue

2 Likes

Yep, this is a little hacky, but I’ve not found more elegant way to show queue list in the dashboard when using MSMQ :frowning:.

Hi. I’m following this exactly but the queues are not being added to the server on my dashboard. Is this still the right way to go with the latest version? Thanks

Edit - has anyone implemented Queues with MSMQ? Thanks.

Edit - how can one tell that their hangfire server is using MSMQ. I can’t see anything in the logs and our latency still seems quite high.