multi-threading based RabbitMQ consumer(基于多线程的 RabbitMQ 消费者)
问题描述
我们有一个 Windows 服务,它监听单个 RabbitMQ 队列并处理消息.
We have a windows service which listen to single RabbitMQ queue and process the message.
我们希望扩展相同的 Windows 服务,以便它可以监听多个 RabbitMQ 队列并处理消息.
We would like to extend same windows services so that it can listen to multiple queues of RabbitMQ and process the message.
不确定是否可以通过使用多线程来实现,因为每个线程都必须监听(阻塞)队列.
Not sure if that can be possible by using multi-threading as each thread will have to listening (blocking) the queue.
由于我对多线程非常陌生,因此需要以下几点的高级指南,这将有助于我开始构建原型.
As I am very new to multi-threading, need high level guideline on following point, which will help me to start building the prototype.
- 是否可以使用线程在单个应用程序中监听多个队列?
- 单线程被关闭的情况如何处理down(由于异常等),如何在不重新启动的情况下恢复整个 Windows 服务.
- 任何可以帮助我处理这种情况的设计模式或开源实现.
推荐答案
我喜欢你写问题的方式 - 它一开始就非常广泛,而且专注于细节.我已经成功地实现了一些非常相似的东西,并且目前正在开发一个开源项目,以吸取我的经验教训并将其回馈给社区.不幸的是,我还没有把我的代码整齐地打包,这对你没有多大帮助!无论如何,回答你的问题:
I like how you wrote your question - it started out very broad and focused in to specifics. I have successfully implemented something very similar, and am currently working on an open-source project to take my lessons learned and give them back to the community. Unfortunately, though- I have yet to package my code up neatly, which doesn't help you much! Anyway, to answer your questions:
<代码>1.是否可以对多个队列使用线程.
A:是的,但它可能充满陷阱.也就是说,RabbitMQ .NET 库并不是最好的代码,我发现它是 AMQP 协议的一个相对繁琐的实现.最有害的警告之一是它如何处理接收"或消费"行为,如果你不小心,很容易导致死锁.幸运的是,它在 API 文档中得到了很好的说明.建议 - 如果可以,请使用单例连接对象.然后,在每个线程中,使用连接创建一个新的IModel和对应的消费者.
A: Yes, but it can be full of pitfalls. Namely, the RabbitMQ .NET library is not the best-written piece of code out there, and I have found it to be a relatively cumbersome implementation of the AMQP protocol. One of the most pernicious caveats is how it handles the "receiving" or "consuming" behavior, which can cause deadlocks quite easily if you aren't careful. Fortunately, it is well-illustrated in the API documentation.
Advice - if you can, use a singleton connection object. Then, in each thread, use the connection to create a new IModel and corresponding consumers.
<代码>2.如何优雅地处理线程中的异常 - 我相信这是另一个主题,我不会在这里讨论它,因为您可以使用多种方法.
2. How to gracefully handle exceptions in threads
- I believe this is another topic and I will not address it here as there are several methods you can use.
<代码>3.有任何开源项目吗? - 我喜欢 EasyNetQ 背后的想法,尽管我最终还是自己推出了自己的项目.希望我的开源项目完成后我会记得跟进,因为我相信它比 EasyNetQ 有更好的改进.
3. Any open-source projects?
- I liked the thinking behind EasyNetQ, although I ended up rolling my own anyway. I'll hopefully remember to follow back when my open source project is completed, as I believe it is an even better improvement than EasyNetQ.
这篇关于基于多线程的 RabbitMQ 消费者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:基于多线程的 RabbitMQ 消费者
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01