private static void Conn(RabbitSenderOption option) { if (option == null) { return; } if (string.IsNullOrEmpty(option.RoutingKey)) { option.RoutingKey = option.QueueName; } if (_channel == null) { lock (Object) { if (_channel == null) { try { _channel = RabbitConfig.Connection.CreateModel(); } catch (Exception exception) { RabbitConfig.ProcessException(exception); _channel = RabbitConfig.Connection.CreateModel(); } } } } }
public RabbitSender(RabbitSenderOption rso) { this.option = rso; Conn(option); _channel.ExchangeDeclare(exchange: option.ExchangeName, type: "direct", durable: true, autoDelete: false, arguments: null); //QueueArguments就是上面为优先级定义的这个dictionary _channel.QueueDeclare(queue: option.QueueName, durable: true, exclusive: false, autoDelete: false, arguments: QueueArguments); _channel.QueueBind(queue: option.QueueName, exchange: option.ExchangeName, routingKey: option.RoutingKey); }
/// <summary> /// 队列初始化 /// </summary> public static void SendBatch(RabbitSenderOption option, IList <string> contentList) { Conn(option); var properties = GetProperties(option); foreach (var con in contentList) { var body = Encoding.UTF8.GetBytes(con); _channel.BasicPublish(option.ExchangeName, option.RoutingKey, properties, body); } }
private static IBasicProperties GetProperties(RabbitSenderOption option) { var properties = _channel.CreateBasicProperties(); properties.Persistent = true; properties.Priority = option.Priority; properties.Headers = option.Headers; properties.ContentType = string.IsNullOrWhiteSpace(option.ContentType) ? "application/json" : option.ContentType; properties.ReplyTo = option.ReplyTo; properties.CorrelationId = option.CorrelationId; return(properties); }