/// <summary>
        /// 发布消息
        /// </summary>
        /// <param name="queue"></param>
        /// <param name="messages"></param>
        /// <param name="options"></param>
        public void Publish(string queue, string[] messages, QueueOptions options = null)
        {
            if (string.IsNullOrEmpty(queue))
            {
                throw new ArgumentException("queue cannot be empty", nameof(queue));
            }

            options = options ?? new QueueOptions();
            var channel = GetChannel();

            PrepareQueueChannel(channel, queue, options);

            foreach (var message in messages)
            {
                var buffer = Encoding.UTF8.GetBytes(message);
                channel.BasicPublish("", queue, null, buffer);
            }
            channel.Close();
        }
Beispiel #2
0
 /// <summary>
 /// 准备连接
 /// </summary>
 /// <param name="channel"></param>
 /// <param name="queue"></param>
 /// <param name="options"></param>
 protected void PrepareQueueChannel(IModel channel, string queue, QueueOptions options)
 {
     //这里的exclusive参数表示是否与当前的channel绑定
     channel.QueueDeclare(queue, options.Durable, false, options.AutoDelete, options.Arguments ?? new Dictionary <string, object>());
 }
 /// <summary>
 /// 发布消息
 /// </summary>
 /// <param name="queue"></param>
 /// <param name="message"></param>
 /// <param name="options"></param>
 public void Publish(string queue, string message, QueueOptions options = null)
 {
     Publish(queue, new string[] { message }, options);
 }