public void Can_consume_messages_from_RabbitMQ_with_BasicGet() { RabbitMqConfig.UsingChannel(channel => { PublishHelloRabbit(channel); while (true) { var basicGetMsg = channel.BasicGet(MqQueueNames <HelloRabbit> .Direct, false); if (basicGetMsg == null) { Console.WriteLine("End of the road..."); return; } var msg = MqMessageExtensions.FromJsonBytes <HelloRabbit>(basicGetMsg.Body); Console.WriteLine(msg); Thread.Sleep(1000); channel.BasicAck(basicGetMsg.DeliveryTag, false); } }); }
public void Can_serialize_IMqMessage_into_typed_Message() { var dto = new Incr { Value = 1 }; var iMsg = MqMessageFactory.Create(dto); var jsonBytes = MqMessageExtensions.ToJsonBytes((object)iMsg); IMqMessage <Incr> typedMessage = MqMessageExtensions.FromJsonBytes <MqMessage <Incr> >(jsonBytes); Assert.That(typedMessage.GetBody().Value, Is.EqualTo(dto.Value)); }
public static IMqMessage <T> ToMessage <T>(this BasicGetResult msgResult) { if (msgResult == null) { return(null); } var props = msgResult.BasicProperties; T body; // json only if (string.IsNullOrEmpty(props.ContentType) || props.ContentType.ToLower().Contains(Const.Json)) { body = MqMessageExtensions.FromJsonBytes <T>(msgResult.Body); } else { throw new NotSupportedException("Unknown Content-Type: " + props.ContentType); } var message = new MqMessage <T>(body) { Id = props.MessageId != null?Guid.Parse(props.MessageId) : new Guid(), CreatedDate = ((int)props.Timestamp.UnixTime).FromUnixTime(), Priority = props.Priority, ReplyTo = props.ReplyTo, Tag = msgResult.DeliveryTag.ToString(), RetryAttempts = msgResult.Redelivered ? 1 : 0 }; if (props.CorrelationId != null) { message.ReplyId = Guid.Parse(props.CorrelationId); } if (props.Headers != null) { foreach (var entry in props.Headers) { if (entry.Key == nameof(IMqMessage.Error)) { var errors = entry.Value; if (errors != null) { var errorsJson = errors is byte[] errorBytes ? errorBytes.FromUtf8Bytes() : errors.ToString(); message.Error = errorsJson.FromJson <MqErrorStatus>(); } } else { if (message.Meta == null) { message.Meta = new Dictionary <string, string>(); } var value = entry.Value is byte[] bytes ? bytes.FromUtf8Bytes() : entry.Value?.ToString(); message.Meta[entry.Key] = value; } } } return(message); }