我使用rabbitmq并且我想将我的队列限制为100,000个消息,所以我添加这样的策略:rabbitmqctl set_policy my-pol "^two-messages$" '{"max-length":2,"overflow":"reject-publish"}' --apply-to queues
此工作正常并限制我的队列。
现在,当我得到一个nack异常时,我希望线程能够暂停增加时间,我这样做了:
public static async Task SendMessagage(Uri queueUri, T message, IBus bus, int interval = 10 * 1000)
{
try
{
Task<ISendEndpoint> sendEndpointTask = bus.GetSendEndpoint(queueUri);
Console.WriteLine($"Enter SendMessagage {queueUri.AbsolutePath} interval={interval}");
var result = sendEndpointTask.Result;
await result.Send(message);
Console.WriteLine($"AFTER SEND SendMessagage {queueUri.AbsolutePath} interval={interval}");
interval = 10 * 1000;
}
catch (PublishNackException e)
{
Console.WriteLine($"PublishNack EXCEPTION SendMessagage {queueUri.AbsolutePath} interval={interval}");
interval = interval * 2;
Thread.Sleep(interval);
await SendMessagage(queueUri, message, bus);
}
catch(Exception ex)
{
Console.WriteLine($"EXCEPTION SendMessagage {queueUri.AbsolutePath} interval={interval}");
interval = interval * 2;
Thread.Sleep(interval);
await SendMessagage(queueUri, message, bus);
Console.WriteLine($"EXCEPTION2 SendMessagage {queueUri.AbsolutePath} interval={interval}");
}
}
但是一段时间后我得到了超时异常。 为什么我得到这个例外,有一段时间消费者需要回复?
例外是:
The model usage threw an exception
System.TimeoutException: The operation has timed out.
at RabbitMQ.Util.BlockingCell.GetValue(TimeSpan timeout)
at RabbitMQ.Client.Impl.SimpleBlockingRpcContinuation.GetReply(TimeSpan timeout)
at RabbitMQ.Client.Impl.ModelBase.ModelRpc(MethodBase method, ContentHeaderBase header, Byte[] body)
at RabbitMQ.Client.Framing.Impl.Model._Private_ChannelOpen(String outOfBand)
at RabbitMQ.Client.Framing.Impl.Connection.CreateModel()
at System.Threading.Tasks.Task`1.InnerInvoke()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MassTransit.RabbitMqTransport.Integration.RabbitMqModelCache.<>c__DisplayClass9_0.<<SendUsingNewModel>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MassTransit.RabbitMqTransport.Integration.RabbitMqModelCache.ModelScope.<Attach>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MassTransit.RabbitMqTransport.Integration.RabbitMqModelCache.<SendUsingExistingModel>d__10.MoveNext()
我在日志文件中看到,在NackException之后我抓到了rabbitmq关闭不知道为什么,日志中的消息是:
13/5/2018 16:14:42.594 [Debug] (8) Closing model: 5 / "admin@192.168.153.103:5672/"