/// <summary> 订阅属性 </summary> /// <param name="queue">队列名称</param> /// <param name="routeKey">路由键,默认为事件属性的RouteKey</param> /// <param name="fetchCount">每次处理数量,小于等于0不限制,默认1</param> /// <param name="retry">是否开启重试</param> /// <param name="times">秒</param> public SubscriptionAttribute(string queue, string routeKey = null, int fetchCount = 1, bool retry = true, int[] times = null) { Option = new RabbitMqSubscribeOption(queue, routeKey) { PrefetchCount = fetchCount, EnableRetry = retry }; if (times != null && times.Any()) { Option.Times = times.Select(t => TimeSpan.FromSeconds(t)).ToArray(); } Queue = queue; RouteKey = routeKey; }
/// <summary> 申明队列并设置死信队列 </summary> /// <param name="channel"></param> /// <param name="queue"></param> /// <param name="exchange"></param> /// <param name="routeKey"></param> /// <param name="option"></param> public static void DeclareWithDlx(this IModel channel, string queue, string exchange, string routeKey, RabbitMqSubscribeOption option) { var xRouteKey = string.IsNullOrWhiteSpace(option.XDeadRouteKey) ? $"~dlx_{queue}" : option.XDeadRouteKey; var xExchange = string.IsNullOrWhiteSpace(option.XDeadExchange) ? exchange : option.XDeadExchange; var args = new Dictionary <string, object> { { "x-dead-letter-exchange", xExchange }, { "x-dead-letter-routing-key", xRouteKey } }; //死信队列 //1.消息被拒绝,并且设置ReQueue参数false; //2.消息过期; //3.队列打到最大长度; channel.QueueDeclare(xRouteKey, true, false); channel.QueueBind(xRouteKey, xExchange, xRouteKey, null); //声明队列 channel.QueueDeclare(queue, true, false, false, args); channel.QueueBind(queue, xExchange, routeKey); }