RabbitMQ 消费者
我有一个从 RabbitMQ 队列检索消息的 Windows 服务。该服务在 Windows 7 计算机上本地运行。当我在 Windows 2008 服务器上安装该服务时,它不起作用(并且不会引发任何错误)。我的 ASP.net MVC 应用程序可以将消息发布到同一队列。这里可能存在防火墙或安全问题吗?我应该以与下面不同的方式从 RabbitMQ 检索消息吗?
public void PullFromQueue()
{
var connectionFactory = new ConnectionFactory();
using (var connection = connectionFactory.CreateConnection())
using (var channel = connection.CreateModel())
{
var consumer = new QueueingBasicConsumer(channel);
channel.ExchangeDeclare(ExchangeName, ExchangeType.Direct, true);
channel.QueueDeclare(QueueName, true);
channel.QueueBind(QueueName, ExchangeName, RoutingKey, false, null);
channel.BasicConsume(QueueName, null, consumer);
while (true)
{
try
{
var e = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
var props = e.BasicProperties;
props.DeliveryMode = PersistentDelivery;
var businessObject = DeserializeBusinessObject(e.DeliveryTag, e.Body);
processBusinessObject(businessObject);
channel.BasicAck(e.DeliveryTag, false);
}
catch (Exception ex)
{
Log<RabbitMQWrapper>.Error("Error in pulling Business Object from Queue", ex);
}
}
}
}
I have a Windows Service that retrieves messages from a RabbitMQ queue. The service works locally on a Windows 7 machine. When I install the service on a Windows 2008 server it does not work (and does not throw any errors). My ASP.net MVC app can publish messages to the same queue. Could there be a firewall or security issue here? Should I be retrieving messages from RabbitMQ differently than below?
public void PullFromQueue()
{
var connectionFactory = new ConnectionFactory();
using (var connection = connectionFactory.CreateConnection())
using (var channel = connection.CreateModel())
{
var consumer = new QueueingBasicConsumer(channel);
channel.ExchangeDeclare(ExchangeName, ExchangeType.Direct, true);
channel.QueueDeclare(QueueName, true);
channel.QueueBind(QueueName, ExchangeName, RoutingKey, false, null);
channel.BasicConsume(QueueName, null, consumer);
while (true)
{
try
{
var e = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
var props = e.BasicProperties;
props.DeliveryMode = PersistentDelivery;
var businessObject = DeserializeBusinessObject(e.DeliveryTag, e.Body);
processBusinessObject(businessObject);
channel.BasicAck(e.DeliveryTag, false);
}
catch (Exception ex)
{
Log<RabbitMQWrapper>.Error("Error in pulling Business Object from Queue", ex);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
忘记了 GAC。当我在本地安装RabbitMQ.Client时,它被放置在GAC中。未将 RabbitMQ.Client DLL 设置为复制本地。我觉得奇怪的是它没有生成运行时错误。我觉得自己很蠢。
Forgot about the GAC. When I installed the RabbitMQ.Client locally, it was placed in the GAC. Did not set the RabbitMQ.Client DLL to copy local. I find it curious that it did not generate a runtime error. I feel dumb.
我做了同样的事情,它对我有用。确保您正在 NetworkService 下运行该服务。防火墙也可能是一个问题。
I do the same thing and it works for me. Ensure you are running the service under NetworkService. Also the firewall could be an issue.
如果您的服务无法启动,请使用事件日志查看器检查事件日志(应用程序事件日志)。
如果没有线索,您应该首先确定您现在遇到的问题(log4net 可能很有用,或者只是将异常写入事件日志):
如果您在本地计算机上使用 quest 用户使用 RabbitMQ,则 quest 用户只能通过环回!在这种情况下,您应该添加不同的用户,例如:
问候...
If you service just fail to start check the Event Logs (Application Event Logs) with Event Log Viewer.
If there is no clue, you should first determine what issue you are having right now (log4net can be usefull or simply just write exception to eventlog):
If you are using RabbitMQ on your local machine with quest user, quest user can only access via loop-back! In this case, you should add different user like:
Regards...