我按如下方式创建客户端(exchangeAndQueue2
是持久的并且存在):
var factory = new ConnectionFactory() {
HostName = hostname,
Port = port,
UserName = userName,
Password = password
};
factory.AutomaticRecoveryEnabled = true;
factory.NetworkRecoveryInterval = TimeSpan.FromSeconds(10);
connection = factory.CreateConnection();
channel = connection.CreateModel();
channel.ExchangeDeclare(exchange1, ExchangeType.Fanout, durable: true);
consumer = new EventingBasicConsumer(channel);
consumer.Registered += (s, e) => { Trace.TraceInformation("Consumer Registered"); };
consumer.ConsumerCancelled += (s, e) => { Trace.TraceInformation("Consumer Cancelled"); };
consumer.Received += NewMessage;
channel.BasicConsume(exchangeAndQueue2, autoAck: false, consumer: consumer);
在大多数情况下,它可以正常工作,但是有时候,由于RabbitMQ,我的整个应用程序都无提示地关闭了。我订阅了FirstChanceException
和ProcessExit
事件
AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) => { Trace.TraceError(eventArgs.Exception.ToString()); };
AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) => {
Trace.TraceWarning("Application is shutting down...");
mq.CloseConnection();
}
这就是我设法抓住的地方:
Server Error: 0 : System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
Server Error: 0 : System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
Server Error: 0 : System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
at RabbitMQ.Client.Impl.InboundFrame.ReadFrom(NetworkBinaryReader reader)
Server Information: 0 : RabbitMQ Consumer Cancelled
Server Warning: 0 : Application is shutting down...
可能是什么,以及如何调试这些东西?库甚至有可能关闭整个应用程序? NewMessage
处理程序的整个代码包装在try/catch (Exception ex)
...
服务器版本3.1.5,客户端版本5.1.0
我的Main
方法以channel.BasicConsume
调用结束,好像我不明白如何组织防弹接收。
我猜想应用程序在接收方线程退出时退出...如何正确重生它?
答案 0 :(得分:0)
根据RabbitMQ documentation,我不应该退出Main
方法。他们使用ReadLine
,但我更喜欢像this answer中那样捕捉Ctrl + C:
static void Main(string[] args) {
//...
channel.BasicConsume(exchangeAndQueue2, autoAck: false, consumer: consumer);
Trace.TraceInformation("Application started. Press Ctrl+C to shut down.");
var exitEvent = new AutoResetEvent(false);
Console.CancelKeyPress += (s, e) => { e.Cancel = true; exitEvent.Set(); };
exitEvent.WaitOne();
Trace.TraceInformation("Ctrl+C pressed.");
mq.CloseConnection();
}