Beispiel #1
0
        public static IBasicProperties Copy(this IBasicProperties basicProperties)
        {
            var bp = new RabbitMQ.Client.Framing.BasicProperties
            {
                AppId           = basicProperties.AppId,
                ClusterId       = basicProperties.ClusterId,
                ContentEncoding = basicProperties.ContentEncoding,
                ContentType     = basicProperties.ContentType,
                CorrelationId   = basicProperties.CorrelationId,
                DeliveryMode    = basicProperties.DeliveryMode,
                Expiration      = basicProperties.Expiration,
                Headers         = basicProperties.Headers != null
                    ? new Dictionary <string, object>(basicProperties.Headers)
                    : null,
                Type           = basicProperties.Type,
                MessageId      = basicProperties.MessageId,
                Timestamp      = basicProperties.Timestamp,
                Persistent     = basicProperties.Persistent,
                Priority       = basicProperties.Priority,
                ReplyTo        = basicProperties.ReplyTo,
                UserId         = basicProperties.UserId,
                ReplyToAddress = basicProperties.ReplyToAddress
            };

            return(bp);
        }
        static void Main(string[] args)
        {
            // default host is localhost
            var factory = new ConnectionFactory();

            using var connection = factory.CreateConnection();
            using var channel    = connection.CreateModel();

            channel.ExchangeDeclare
            (
                ExchangeName,
                "direct",
                true,
                false,
                new Dictionary <string, object> {
                { "passive", false }
            }
            );

            var messageProperties = new BasicProperties
            {
                ContentType = "text/plain"
            };
            var messageBody = Encoding.UTF8.GetBytes(args[0]);

            channel.BasicPublish
            (
                ExchangeName,
                "hola",
                messageProperties,
                messageBody
            );
        }
Beispiel #3
0
 public void TestBodyLength()
 {
     RabbitMQ.Client.Framing.BasicProperties prop =
         new RabbitMQ.Client.Framing.BasicProperties();
     prop.WriteTo(m_w.BaseWriter, 0x123456789ABCDEF0UL);
     Check(m_w, new byte[] { 0x00, 0x00,                                     // weight
                             0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, // body len
                             0x00, 0x00 });                                  // props flags
 }
        static void Main(string[] args)
        {
            // default host is localhost
            var factory = new ConnectionFactory();

            using var connection = factory.CreateConnection();
            using var channel    = connection.CreateModel();

            channel.ExchangeDeclare
            (
                ExchangeName,
                "direct",
                true,
                false,
                new Dictionary <string, object> {
                { "passive", false }
            }
            );

            channel.ConfirmSelect();
            Console.WriteLine("Channel in 'confirm' mode!");

            channel.BasicAcks += (sender, eventArgs) =>
            {
                if (MessageIds.Contains(eventArgs.DeliveryTag))
                {
                    Console.WriteLine("Confirm received!");
                    MessageIds.Remove(eventArgs.DeliveryTag);
                }
            };
            channel.BasicNacks += (sender, eventArgs) =>
            {
                if (MessageIds.Contains(eventArgs.DeliveryTag))
                {
                    Console.WriteLine("Message lost!");
                }
            };

            var messageProperties = new BasicProperties
            {
                ContentType = "text/plain"
            };
            var messageBody = Encoding.UTF8.GetBytes(args[0]);

            channel.BasicPublish
            (
                ExchangeName,
                "hola",
                messageProperties,
                messageBody
            );

            MessageIds.Add((ulong)MessageIds.Count + 1);

            channel.WaitForConfirmsOrDie();
        }
        /// <summary>
        /// Получение из свойств тела сообщения и свойств сообщения сообщения в формате шины.
        /// </summary>
        /// <param name="bodyProperties">Свойства тела сообщения.</param>
        /// <param name="properties">Свойства сообщения (тэги).</param>
        /// <returns>Сообщение в формате шины.</returns>
        public MessageWithNotTypedPk ConvertFromMqFormat(byte[] messagePayload, IDictionary <string, object> properties)
        {
            var result = new MessageWithNotTypedPk();

            BasicProperties rmqProperties = new RabbitMQ.Client.Framing.BasicProperties();

            rmqProperties.Headers = properties;
            var mapMessageReader = new MapMessageReader(rmqProperties, messagePayload);

            var bodyProperties = mapMessageReader.Body;

            if (bodyProperties.ContainsKey(this._bodyPropertyName))
            {
                result.Body = (string)mapMessageReader.Body[this._bodyPropertyName];
            }

            if (bodyProperties.ContainsKey(this._attachmentPropertyName))
            {
                result.BinaryAttachment = (byte[])mapMessageReader.Body[this._attachmentPropertyName];
            }

            if (bodyProperties.ContainsKey(this._senderIdPropepertyName))
            {
                result.Sender = (string)mapMessageReader.Body[this._senderIdPropepertyName];
            }

            var headers = mapMessageReader.Properties.Headers;

            if (headers != null)
            {
                var messageTags = new Dictionary <string, string>();
                foreach (var property in headers)
                {
                    if (property.Key.StartsWith(this._tagPropertiesPrefix))
                    {
                        var tagKey = property.Key.Substring(this._tagPropertiesPrefix.Length);
                        var value  = Encoding.UTF8.GetString((byte[])property.Value);
                        messageTags.Add(tagKey, value);
                    }
                }

                if (headers.ContainsKey(this.TimestampPropertyName))
                {
                    var unixtimestamp = (long)headers[this.TimestampPropertyName];
                    result.ReceivingTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc).AddMilliseconds(unixtimestamp);
                }

                result.Tags = messageTags.Any() ? messageTags.Select(x => $"{x.Key}:{x.Value}").Aggregate((x, y) => $"{x}, {y}") : "";
            }

            return(result);
        }
Beispiel #6
0
        public void TestBodyLength()
        {
            RabbitMQ.Client.Framing.BasicProperties prop =
                new RabbitMQ.Client.Framing.BasicProperties();
            int bytesNeeded = prop.GetRequiredBufferSize();

            byte[] bytes        = new byte[bytesNeeded];
            int    bytesWritten = prop.WriteTo(bytes, 0x123456789ABCDEF0UL);

            prop.WriteTo(bytes, 0x123456789ABCDEF0UL);
            Check(bytes.AsMemory().Slice(0, bytesWritten), new byte[] { 0x00, 0x00,                                     // weight
                                                                        0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, // body len
                                                                        0x00, 0x00 });                                  // props flags
        }
Beispiel #7
0
 public void TestSimpleProperties()
 {
     RabbitMQ.Client.Framing.BasicProperties prop =
         new RabbitMQ.Client.Framing.BasicProperties();
     prop.ContentType = "text/plain";
     prop.WriteTo(m_w.BaseWriter, 0x123456789ABCDEF0UL);
     Check(m_w, new byte[] { 0x00, 0x00,                                     // weight
                             0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, // body len
                             0x80, 0x00,                                     // props flags
                             0x0A,                                           // shortstr len
                             0x74, 0x65, 0x78, 0x74,
                             0x2F, 0x70, 0x6C, 0x61,
                             0x69, 0x6E });
 }
Beispiel #8
0
        public void TestSimpleProperties()
        {
            RabbitMQ.Client.Framing.BasicProperties prop =
                new RabbitMQ.Client.Framing.BasicProperties
            {
                ContentType = "text/plain"
            };
            int bytesNeeded = prop.GetRequiredPayloadBufferSize();

            byte[] bytes = new byte[bytesNeeded];
            var    m_w   = new ContentHeaderPropertyWriter(bytes);

            prop.WritePropertiesTo(ref m_w);
            Check(bytes.AsMemory().Slice(0, m_w.Offset), new byte[] {
                0x80, 0x00,                                                // props flags
                0x0A,                                                      // shortstr len
                0x74, 0x65, 0x78, 0x74, 0x2F, 0x70, 0x6C, 0x61, 0x69, 0x6E // text/plain
            });
        }
Beispiel #9
0
        public void TestSimpleProperties()
        {
            RabbitMQ.Client.Framing.BasicProperties prop =
                new RabbitMQ.Client.Framing.BasicProperties
            {
                ContentType = "text/plain"
            };
            int bytesNeeded = prop.GetRequiredBufferSize();

            byte[] bytes        = new byte[bytesNeeded];
            int    bytesWritten = prop.WriteTo(bytes, 0x123456789ABCDEF0UL);

            Check(bytes.AsMemory().Slice(0, bytesWritten), new byte[] { 0x00, 0x00,                                     // weight
                                                                        0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, // body len
                                                                        0x80, 0x00,                                     // props flags
                                                                        0x0A,                                           // shortstr len
                                                                        0x74, 0x65, 0x78, 0x74,
                                                                        0x2F, 0x70, 0x6C, 0x61,
                                                                        0x69, 0x6E });
        }
Beispiel #10
0
        private Task PublishAsync <TMessage>(TMessage body, string exchange, string routingKey)
            where TMessage : Message
        {
            try
            {
                byte[] data       = _serializer.Serialize(body);
                var    properties = new RabbitMQ.Client.Framing.BasicProperties()
                {
                    MessageId     = typeof(TMessage).Name,
                    CorrelationId = body.CorrelationId
                };

                _channel.BasicPublish(exchange, routingKey, properties, data);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            return(Task.CompletedTask);
        }
        public void TestSimpleProperties()
        {
            RabbitMQ.Client.Framing.BasicProperties prop =
                new RabbitMQ.Client.Framing.BasicProperties();
            prop.ContentType = "text/plain";
            prop.WriteTo(m_w.BaseWriter, 0x123456789ABCDEF0UL);
            Check(m_w, new byte[] { 0x00, 0x00, // weight
			          0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, // body len
			          0x80, 0x00, // props flags
			          0x0A, // shortstr len
			          0x74, 0x65, 0x78, 0x74,
			          0x2F, 0x70, 0x6C, 0x61,
			          0x69, 0x6E });
        }
        public void TestBodyLength()
        {
            RabbitMQ.Client.Framing.BasicProperties prop =
                new RabbitMQ.Client.Framing.BasicProperties();
            prop.WriteTo(m_w.BaseWriter, 0x123456789ABCDEF0UL);
            Check(m_w, new byte[] { 0x00, 0x00, // weight
			          0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, // body len
			          0x00, 0x00}); // props flags
        }