简体   繁体   English

两次收到相同的MSMQ消息?

[英]Receiving the Same MSMQ Message Twice?

I have an MSMQ-based system with three layers that communicate with each other. 我有一个基于MSMQ的系统,该系统具有相互通信的三层。 For the sake of simplicity, I'll refer to them as Layer 1, 2, and 3. They sit like this: 为了简单起见,我将它们称为第1层,第2层和第3层。它们的位置如下:

Layer 1 <-> Layer 2 <-> Layer 3

So Layer 1 talks only to Layer 2, Layer 3 talks only to Layer 2, and Layer 2 talks to both others. 因此,第1层仅与第2层对话,第3层仅与第2层对话,而第2层则与其他两者对话。 I have four queues for this, 我为此有四个队列,

Layer1_in
Layer1_out
Layer3_out
Layer3_in

And the layers communicate over this infrastructure: 各个层通过此基础结构进行通信:

Layer 1 -> Layer1_out -> Layer 2
Layer 1 <- Layer1_in <- Layer 2
Layer 3 -> Layer3_out -> Layer 2
Layer 3 <- Layer3_in <- Layer 2

(sorry if this is more exhaustive than necessary) (很抱歉,如果此方法比必要的方法更详尽)

Anyhow, a message gets passed from Layer 1 to Layer 2, some processing takes place, then another (related) message is sent to Layer 3, and vice versa. 无论如何,一条消息将从第1层传递到第2层,然后进行一些处理,然后将另一条(相关的)消息发送到第3层,反之亦然。 The issue I'm having is that occasionally I have two messages sent from Layer 1 to Layer 2, but instead of receiving both messages in order it's receiving the first message twice. 我遇到的问题是,偶尔会有两条消息从第1层发送到第2层,但不是同时接收这两条消息,而是两次接收到第一条消息。 I'm using BeginReceive on Layer1_out to asynchronously receive the message. 我使用BeginReceiveLayer1_out异步接收消息。 When that completes I process the received message and call BeginReceive again to get the next message. 完成后,我将处理收到的消息并再次调用BeginReceive以获取下一条消息。

To track this down I implemented a message counter on the sending side and I write it to a text file. 为了对此进行跟踪,我在发送方实现了一个消息计数器,并将其写入文本文件。 I'm using the Extension property to store a string representation of this message number so that I can then retrieve the message number on the processing side. 我正在使用Extension属性存储此消息号的字符串表示形式,以便随后可以在处理端检索消息号。 When I recieve a message, I take this number and write it to a different file. 当我收到一条消息时,我会拿这个号码并将其写入另一个文件中。 This SHOULD produce two files that have the same contents, but instead I'll see things like 这应该产生两个具有相同内容的文件,但是我会看到类似

00000000000000000214
00000000000000000215
00000000000000000215 <- this is bad!
00000000000000000217
00000000000000000218
00000000000000000219

In the receipt log, indicating that message 215 was processed twice and 216 didn't come through. 在收据日志中,指示消息215已被处理两次而216未通过。 For my purposes it doesn't affect anything for me to process the same message twice (hence why this has gone unnoticed for some time), but missing a message altogether is a big problem. 就我的目的而言,对同一条消息进行两次处理不会影响任何事情(因此,为什么一段时间以来未引起注意),但是完全丢失一条消息是一个大问题。

Any thoughts? 有什么想法吗?

Problem solved...figures that I would dole out multithreading advice one day then get bitten by a violation myself the next! 问题解决了……有一天我会发布多线程建议,然后第二天就被自己咬伤!

The culprit was this line: 罪魁祸首是这条线:

receiveResult = <queueName>.BeginReceive()

When I had another message already waiting in the queue, my ReceiveCompleted event was firing on a different thread and getting to my EndReceive(receiveResult) call before the return value from BeginReceive was actually written to the receiveResult variable. 当我有另外的消息已经在排队等候时,我ReceiveCompleted事件是射击在不同的线程,并让我EndReceive(receiveResult)调用之前从返回值BeginReceive实际写入receiveResult变量。 As a result, I was calling EndReceive with the same value and getting the same message again! 结果,我用相同的值调用EndReceive ,并再次收到相同的消息! Locking the whole event handler around a sync object took care of the issue. 将整个事件处理程序锁定在同步对象周围可以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM