排队的 WCF 服务每 X 秒处理一次

发布于 2024-08-28 04:40:27 字数 159 浏览 10 评论 0原文

我需要创建一个可以按配置的时间间隔处理排队请求的服务。例如,访问网络并从网站获取财务数据,要求我们将请求限制为每秒一次。我是 WCF 新手,不确定 (1) WCF 与 MSMQ 是否是实现此功能的正确选择? (2) 如果是的话,执行间隔的最佳机制是什么?一个线程等待?一个计时器(不知道它是如何工作的)。

I need to create a service which can process queued requests on a configured time interval. For example go to the web and get financial data from a site the requires we limit requests to once per second. I am new to WCF and I am not sure if (1) WCF with MSMQ a proper choice for implementing this? and (2) if so what is the best mechanism for enforcing the interval? a thread wait? a timer (not sure how that would work).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆伤 2024-09-04 04:40:27

WCF 中没有内置任何内容可以让您显式处理此问题,因此您仍然需要自己完成所有工作。

虽然您的服务当然可以处理来自 MSMQ 的请求,但 WCF 中的 MSMQ 侦听器将尽快挑选并处理消息;你不能将它们配置为仅每 X 秒处理一次消息(如果有正确的工具,你可以伪造它,但在我看来,这不会那么好)。

如果处理请求之间的延迟不是很短,一种选择是使用中间队列来保存待处理的请求。也就是说,无论发送什么实际请求,都会将它们写入无人直接侦听的队列(队列 A),而您的 WCF 服务则侦听不同的队列(队列 B)。然后,有其他东西(可以像从任务计划程序运行的脚本一样简单)每 X 秒/分钟/无论什么运行一次,并且仅将 1 条消息从队列 A 移动到队列 B,从而触发实际的 WCF 服务运行。

There's nothing built into WCF that would allow you to handle this explicitly, so you'd still need to do all the work yourself.

While your service could certainly process requests from MSMQ, the MSMQ listeners in WCF will pick and process messages as soon as possible; you can't configure them to process messages every X seconds only (you could fake it given the right tools, but seems to me it wouldn't be all that great).

One option if your delay between processing requests isn't very short, would be to use an intermediate queue to hold pending requests. That is, whatever sends the real requests writes them to a queue nobody is directly listening to (queue A), while your WCF service listens on a differet queue (queue B). Then, have something else (could be as simple as a script run from task scheduler) that runs once every X seconds/minutes/whatever and moves just 1 message from queue A to queue B, thus triggering the actual WCF service to run.

橙幽之幻 2024-09-04 04:40:27

WCF 和 MSMQ 是一个很棒的团队!绝对值得一看。

WCF 未提供开箱即用的部分是“每 x 秒检查一次”。这里最好的方法是将 WCF 服务托管在 Windows NT 服务中,并在 NT 服务中设置一个计时器,每 x 秒检查一次 MSMQ 队列。真的,实施起来应该不会太难。美妙之处在于:您可以非常轻松地在 NT 服务中自行托管 WCF 服务 - 只需几行代码,您就可以完全控制发生的情况和时间。请参阅 MSDN 文档,了解如何在托管应用程序中托管 WCF 服务 了解详情。

资源

WCF and MSMQ are a great team! Definitely worth checking out.

The part that WCF doesn't provide out of the box is the "check every x seconds". The best approach here would be to host your WCF service inside a Windows NT Service, and have a timer inside the NT Service that goes to check the MSMQ queue only once every x seconds. Shouldn't be too hard to implement, really. The beauty is: you can very easily self-host a WCF Service inside a NT Service - just a few lines of code, and you get complete control over what's happening, and when. See the MSDN docs on How to Host a WCF service in a managed application for details.

Resources:

帅气称霸 2024-09-04 04:40:27

或者您可以只使用窗口服务来消费消息。如果您不使用在发布消息后立即使用消息的 WCF 功能,那么您可能没有理由首先使用 wcf

Or you could just use a window service to consume the messages instead. If you are not using the WCF functionality of consuming a message as soon as it is posted, then you probably have no reason to use wcf in the first place

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文