Exemplo n.º 1
0
        private IBasicProperties CreateBasicProperties(IModel _channel, SendMessageOption option)
        {
            if (option == null)
            {
                throw new ArgumentNullException(nameof(option));
            }

            var basicProperties = _channel.CreateBasicProperties();
            var header          = new Dictionary <string, object>();

            //参数
            if (ValidateHelper.IsNotEmpty(option.Properties))
            {
                header.AddDict(option.Properties.ToDictionary(x => x.Key, x => x.Value));
            }
            //延迟
            if (option.Delay != null)
            {
                header["x-delay"] = Math.Abs((long)option.Delay.Value.TotalMilliseconds);
            }
            //持久化
            if (option.Persistent)
            {
                basicProperties.DeliveryMode = (byte)2;
                basicProperties.Persistent   = true;
            }
            else
            {
                basicProperties.DeliveryMode = (byte)1;
                basicProperties.Persistent   = false;
            }
            //优先级
            if (option.Priority != null)
            {
                basicProperties.Priority = (byte)option.Priority.Value;
            }

            //headers
            basicProperties.Headers = header;

            return(basicProperties);
        }
Exemplo n.º 2
0
        public void SendMessage <T>(string exchange, string routeKey, T message, SendMessageOption option = null)
        {
            option ??= new SendMessageOption()
            {
            };
            using var _channel = this._connection.CreateModel();
            var bs    = this._serializer.Serialize(message);
            var props = this.CreateBasicProperties(_channel, option);

            using var confirm = new ConfirmOrNot(_channel, option.Confirm, option.ConfirmTimeout);
            try
            {
                _channel.BasicPublish(exchange: exchange,
                                      routingKey: routeKey,
                                      basicProperties: props,
                                      body: bs);
            }
            catch
            {
                confirm.DontConfirmAnyMore();
                throw;
            }
        }
Exemplo n.º 3
0
        void SendMessage <T>(string queue, T message, SendMessageOption option = null)
        {
            option ??= new SendMessageOption()
            {
            };
            using var _channel = this._connection.CreateModel();
            var bs    = this._serializer.SerializeToBytes(message);
            var props = this.CreateBasicProperties(_channel, option);

            using var confirm = new ConfirmOrNot(_channel, option.Confirm, option.ConfirmTimeout);
            try
            {
                //这里queue为什么也是routing key
                _channel.BasicPublish(exchange: string.Empty,
                                      routingKey: queue,
                                      basicProperties: props,
                                      body: bs);
            }
            catch
            {
                confirm.DontConfirmAnyMore();
                throw;
            }
        }