接收相同的MSMQ消息两次? [英] Receiving the Same MSMQ Message Twice?

查看:174
本文介绍了接收相同的MSMQ消息两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MSMQ为基础的系统与相互通信三层。为简单起见,我称它们为第1层,2和3。他们坐​​在这样的:

I have an MSMQ-based system with three layers that communicate with each other. For the sake of simplicity, I'll refer to them as Layer 1, 2, and 3. They sit like this:

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

所以1层的会谈只有二层,三层的会谈只有2层,第2层会谈,双方他人。我有四个队列,对于这一点,

So Layer 1 talks only to Layer 2, Layer 3 talks only to Layer 2, and Layer 2 talks to both others. I have four queues for this,

Layer1_in
Layer1_out
Layer3_out
Layer3_in

和层在此基础通信:

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)

总之,一个消息被从层1传递到第2层,一些处理发生,然后另一个(相关)消息被发送到3层,反之亦然。我遇到的问题是,有时我从第1层发送至2层两个消息,但不是为了接收两个消息它收到的第一条消息的两倍。我使用 BeginReceive Layer1_out 异步接收消息。当我完成再处理接收到的消息和呼叫 BeginReceive 获得下一条消息。

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. 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. I'm using BeginReceive on Layer1_out to asynchronously receive the message. When that completes I process the received message and call BeginReceive again to get the next message.

要跟踪下来我实现了在发送方信息计数器,我把它写入一个文本文件中。我使用了扩展属性来存储一个字符串,再此消息数量presentation,这样我就可以获取关于处理方面的消息编号。当我收到一个消息,我想借此号,并写到不同的文件。这将产生两个文件具有相同的内容,而是我看到的东西像

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. 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

在收据日志,表明消息215处理两次,216没有来过。对于我而言它不会影响任何东西,我来处理同一个消息两次(所以为什么这已经被忽视了一段时间),但缺少消息完全是个大问题。

In the receipt log, indicating that message 215 was processed twice and 216 didn't come through. 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.

有什么想法?

推荐答案

问题解决了......数字,我会施舍的多线程建议1天后来就被侵犯自己的下一个被咬了!

Problem solved...figures that I would dole out multithreading advice one day then get bitten by a violation myself the next!

罪魁祸首是这一行:

receiveResult = <queueName>.BeginReceive()

当我在队列中已等待另一条消息,我的 ReceiveCompleted 事件被射击在不同的线程,并让我的 EndReceive(receiveResult)从 BeginReceive 返回前值调用实际写入 receiveResult 变量。这样一来,我打电话 EndReceive 具有相同的价值,并再次得到相同的消息!锁定整个事件处理程序周围的同步对象了这个问题的照顾。

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. As a result, I was calling EndReceive with the same value and getting the same message again! Locking the whole event handler around a sync object took care of the issue.

这篇关于接收相同的MSMQ消息两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆