MassTransit MSMQ Pub with multi-sub. When is RuntimeServices ready?

醉酒当歌 提交于 2019-12-22 10:45:41

问题


I've set up a simple test comprising a publisher with two subscribers, all running on a single machine using MSMQ and MassTransit (2.1.1) RuntimeServices which is using a local Sql Server database.

I've included the Bus set up code below so you can see what's set up. I'm starting each component manually and independently to try to workout what happens if a subscriber ins't running.

I ran the two subscribers first so the queues and subscriptions are all set up and then quit them both, without unsubscribing from the messages. If I then run the publisher on its own, which dumps 400 messages in the queues as fast as it can, I can see that the two subscriber queues have differing numbers of messages waiting.

My assumption is that I'm publishing before RuntimeServices has been able to set itself up both destination queues. With a 5 second delay between bus setup and publish, I get what I expect 400 messages waiting in both subscriber queues, i.e. some messages weren't published to both queues.

My question is this; Is there a way to tell if RuntimeServices is ready with the subscribers already in its database when the publisher starts?

This is the publisher code

Bus.Initialize(sbc =>
        {
            sbc.SetCreateTransactionalQueues(true);
            sbc.ReceiveFrom("msmq://localhost/andy_publisher");
            sbc.UseSubscriptionService("msmq://localhost/mt_subscriptions");
            sbc.UseMsmq();
            sbc.VerifyMsmqConfiguration();
        });

        var bus = Bus.Instance;


        Thread.Sleep(5000); // this makes it all work :)

        int i = 0;
        foreach (string filename in System.IO.Directory.EnumerateFiles(@"C:\Users\andy.baker\Pictures\", "*.*", SearchOption.AllDirectories))
        {
            Console.WriteLine(filename);
            bus.Publish(new Messages.FileRegistered {FilePath = filename});
            i++;
        }

        Console.WriteLine("Published {0} messages", i);
        Console.ReadLine();

The subscribers are configured like this;

Bus.Initialize(sbc => {
                     sbc.UseMsmq();
                     sbc.VerifyMsmqConfiguration();
                     sbc.ReceiveFrom("msmq://localhost/andy_subscriber1");
                              sbc.UseSubscriptionService("msmq://localhost/mt_subscriptions");
                           }
            );

...and the second subscriber...

Bus.Initialize(sbc =>
        {
            sbc.UseMsmq();
            sbc.VerifyMsmqConfiguration();
            sbc.ReceiveFrom("msmq://localhost/andy_subscriber2");
            sbc.UseSubscriptionService("msmq://localhost/mt_subscriptions");
        }

Thanks in advance for any advice.


回答1:


How are you subscribing your consumers? To survive restarts they should be permanent. http://docs.masstransit-project.com/en/latest/configuration/sub_config_api.html

s.Consumer<TConsumer>().Permanent();


来源:https://stackoverflow.com/questions/10802678/masstransit-msmq-pub-with-multi-sub-when-is-runtimeservices-ready

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!