简体   繁体   English

在WCF中批量处理邮件?

[英]Batching messages in WCF?

Here's my scenario ... I will be receiving a stream of individual messages from an outside source. 这是我的情况...我将从外部来源接收单个消息流。 The nature of this data and the processing I have to do to them is such that the more messages I can include in one batch, the more messages I can process on average. 这些数据的性质以及我必须对它们进行的处理使得一批中可以包含的消息越多,平均我可以处理的消息就越多。 To use an example: 举个例子:

  1. Messages 1 comes in, the WCF service starts processing the message 出现消息1,WCF服务开始处理该消息
  2. Messages 2 and 3 come in before step 1 is done processing. 消息2和3在完成步骤1之前进入。 Process 2 and 3 together. 一起处理2和3。

There are obviously a few assumptions here, that we can't start processing step 2 before step 1 is done, but we can process multiple messages in one run. 显然,这里有一些假设,我们不能在完成步骤1之前就开始处理步骤2,但是我们可以一次运行处理多个消息。

My initial thought was to simply use the msmq binding so that messages can queue up while one batch is processing. 我最初的想法是简单地使用msmq绑定,以便在一批处理过程中消息可以排队。 But then I realized that WCF will simply deliver each message one at a time. 但是后来我意识到WCF只会一次传递一条消息。 And I'm not even sure if it will wait until one message is done processing before pulling the next one and processing that one (perhaps in a thread?) which would be bad in this case. 而且我什至不知道它是否会等到处理完一条消息之后再拉出下一条消息并处理那个消息(也许在线程中?),这在这种情况下会很糟糕。

So, I was just wondering if there might be some guidance someone could provide on how to accomplish such a system using WCF. 因此,我只是想知道是否有人可以提供一些指导,说明如何使用WCF完成这样的系统。 thanks! 谢谢!

Maximizing Throughput 最大化吞吐量

The solution is probably to not think in terms of WCF at all. 解决的办法可能是根本不考虑WCF。 You probably need a state machine that is running on a separate thread from the ServiceHost. 您可能需要在与ServiceHost分开的线程上运行的状态机。 That state machine will host, eg, wrap, a Queue or similar type of class. 该状态机将托管(例如,包装)Queue或类似类型的类。 The state machine will be supplied a delegate that will be used for processing the messages. 将为状态机提供一个委托,该委托将用于处理消息。 When your WCF ServiceHost receives messages, it will push the messages onto the queue that is encapsulated within your state machine, and that is all it will care about. 当WCF ServiceHost收到消息时,它将消息推送到状态机内封装的队列中,而这正是它所关心的。

As the state machine iterates at your designated interval, it will pop messages from the queue in whatever quantities you desire. 当状态机以您指定的时间间隔进行迭代时,它将以所需的数量从队列中弹出消息。 Ideally, you should design this to be throttled from within your service's configuration settings. 理想情况下,应设计为从服务的配置设置中对其进行限制。 This will allow you to experiment to what batch sizes work best, similar to how it can be useful to batch together row updates to database tables for performance. 这将使您能够尝试最适合的批处理大小,类似于将行更新批处理到数据库表以提高性能的有用方式。

Batching Messages 批处理消息

Granted, you could design your messaging envelope (basically, the type you are passing as a parameter to an OperationContract method) to basically be a container for 1 to N messages. 当然,您可以将消息传递信封(基本上是作为参数传递给OperationContract方法的类型)设计为基本上是1到N条消息的容器。 That would certain optimize the delivery of bytes by minimizing the number of messages. 通过减少消息数量,一定可以优化字节的传递。 However, whether one message arrives or N messages arrive simultaneously would not change the logic needed to properly process these messages, except for adding a loop of some sort to iterate through the N messages. 但是,一条消息到达还是N条消息同时到达不会改变正确处理这些消息所需的逻辑,除了添加某种循环来遍历N条消息外。 The fact that you are willing to consider MSMQ suggests that your messaging paradigm is already asynchronous, and so it should work well with the approach I describe for maximizing throughput. 您愿意考虑MSMQ的事实表明您的消息传递范例已经是异步的,因此它应该与我描述的使吞吐量最大化的方法很好地配合使用。

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

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