How to enforce message queue sequence with multiple WCF service instances

爱⌒轻易说出口 提交于 2019-11-30 13:56:14
Karell Ste-Marie

batwad,

You are trying to manually create a service bus. Why don't you try to use an existing one?

NServiceBus, MassTransit, ServiceStack

At least 2 of those work with MSMQ.

Furthermore, if you absolutely need order it may actually be for another reason - you want to be able to send a message and you don't want dependent messages to be processed before the first message. You are looking for the Saga Pattern. NServiceBus and MassTransit both will allow you to manage Sagas easily, they will both allow you to simply trigger the initial message and then trigger the remaining messages based on conditions. It will allow you to implement the plumping of your distributed application a snap.

You can then even scale up to thousands of clients, queue servers and message processors without having to write a single line of code nor have any issues.

We tried to implement our own service bus over msmq here, we gave up because another issue kept creeping up. We went with NServiceBus but MassTransit is also an excellent product (it's 100% open source, NServiceBus isn't). ServiceStack is awesome at making APIs and using Message Queues - I'm sure you could use it to make Services that act as Queue front-ends in minutes.

Oh, did I mention that in the case of NSB and MT both only require under 10 lines of code to fully implement queues, senders and handlers?

----- ADDED -----

Udi Dahan (one of the main contributers of NServiceBus) talks about this in: "In-Order Messaging a Myth" by Udi Dahan "Message Ordering: Is it Cost Effective?" with Udi Dahan

Chris Patterson (one of the main contributers of Mass Transit) "Using Sagas to ensure proper sequential message order" question

StackOverflow questions/answers: "Preserve message order when consuming MSMQ messages in a WCF application"

----- QUESTION -----

I must say that I'm baffled as to why you need to guarantee message order - would you be in the same position if you were using an HTTP/SOAP protocol? My guess is no, then why is it a problem in MSMQ?

Good luck, hope this helps,

Ensuring in-order delivery of messages is one of the de-facto sticky issues with high volume messaging.

In an ideal world, your message destinations should be able to handle out-of-order messaging. This can be achieved by ensuring that your message source includes some kind of sequencing information. Again ideally this takes the form of some kind of x-of-n batch stamp (message 1 of 10, 2 of 10, etc). Your message destination is then required to assemble the data into order once it has been delivered.

However, in the real world there often is no scope for changing downstream systems to handle messages arriving out of order. In this instance you have two choices:

  1. Go entirely single threaded - actually you can usually find some kind of 'grouping id' which means you can go single-threaded in a for-each-group sense, meaning you still have concurrency across different message groups.
  2. Implement a re-sequencer wrapper around each of your consumer systems you want to receive in-order messages.

Neither solution is very nice, but that's the only way I think you can have concurrency and in-order message delivery.

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