Ejemplo n.º 1
0
 private static void Publish_4_Rot13_messages(IMessageQueueClient mqClient)
 {
     mqClient.Publish(new Rot13 { Value = "Hello" });
     mqClient.Publish(new Rot13 { Value = "World" });
     mqClient.Publish(new Rot13 { Value = "ServiceStack" });
     mqClient.Publish(new Rot13 { Value = "Redis" });
 }
 private static void Publish_4_messages(IMessageQueueClient mqClient)
 {
     mqClient.Publish(new Reverse { Value = "Hello" });
     mqClient.Publish(new Reverse { Value = "World" });
     mqClient.Publish(new Reverse { Value = "NServiceKit" });
     mqClient.Publish(new Reverse { Value = "Redis" });
 }
        public override void HandleThisMessage(IMessage mqMessage, IMessageQueueClient client)
        {
            if (!(mqMessage.Body is StartRentalCommand m))
            {
                return;
            }

            var film = InfraHelper.GetFilm(m.FilmId);

            if (!film.IsAvailable)
            {
                return;
            }

            client.Publish(new CalculatePriceCommand
            {
                UserId     = m.UserId,
                FilmId     = m.FilmId,
                Type       = film.Type,
                UseBonuses = m.UseBonuses,
                ActiveTo   = m.ActiveTo,
                ActiveFrom = m.ActiveFrom,
                OrderId    = m.OrderId
            });

            client.Publish(new SetFilmUnavailableCommand
            {
                FilmId = m.FilmId
            });
        }
 private static void Publish_4_Rot13_messages(IMessageQueueClient mqClient)
 {
     mqClient.Publish(new Rot13 { Value = "Hello" });
     mqClient.Publish(new Rot13 { Value = "World" });
     mqClient.Publish(new Rot13 { Value = "ServiceStack" });
     mqClient.Publish(new Rot13 { Value = "Redis" });
 }
Ejemplo n.º 5
0
        public void SetLeverage(Guid id, string symbol, decimal leverage)
        {
            var setLeverage    = new SetLeverage(Guid.NewGuid(), Exchange.Types.Xchange.ByBit, id, symbol, leverage);
            var msgSetLeverage = _rabbitMqClient.CreateMessage <SetLeverage>(setLeverage);

            _rabbitMqClient.Publish <SetLeverage>(msgSetLeverage);
        }
 private static void Publish_4_messages(IMessageQueueClient mqClient)
 {
     mqClient.Publish(new Reverse {
         Value = "Hello"
     });
     mqClient.Publish(new Reverse {
         Value = "World"
     });
     mqClient.Publish(new Reverse {
         Value = "NServiceKit"
     });
     mqClient.Publish(new Reverse {
         Value = "Redis"
     });
 }
Ejemplo n.º 7
0
 internal static void Publish_4_messages(IMessageQueueClient mqClient)
 {
     mqClient.Publish(new Reverse {
         Value = "Hello"
     });
     mqClient.Publish(new Reverse {
         Value = "World"
     });
     mqClient.Publish(new Reverse {
         Value = "ServiceStack"
     });
     mqClient.Publish(new Reverse {
         Value = "Redis"
     });
 }
Ejemplo n.º 8
0
 private void Publish_4_messages(IMessageQueueClient mqClient)
 {
     mqClient.Publish(new Reverse
     {
         Value = "Hello"
     });
     mqClient.Publish(new Reverse
     {
         Value = "World"
     });
     mqClient.Publish(new Reverse
     {
         Value = "ServiceStack"
     });
     mqClient.Publish(new Reverse
     {
         Value = "Redis"
     });
 }
Ejemplo n.º 9
0
        public TResponse Send <TRequest, TResponse>(TRequest request)
        {
            _log.Info($"Sending {request.SerializeToString()}");
            var queue = _mqClient.GetTempQueueName();

            _log.Info($"to queue {queue}");
            _mqClient.Publish(new Message <TRequest>(request)
            {
                ReplyTo = queue
            });

            var response = _mqClient.Get <TResponse>(queue);

            _log.Info($" got response {response.SerializeToString()}");
            _mqClient.Ack(response);
            return(response.GetBody());
        }
        public override void HandleThisMessage(IMessage mqMessage, IMessageQueueClient client)
        {
            if (!(mqMessage.Body is StopRentalCommand m))
            {
                return;
            }

            var rent = InfraHelper.GetRent(m.RentId);

            rent.ReturnedDate = DateTime.Now;

            InfraHelper.UpdateRent(rent);

            client.Publish(new SetFilmAvailableCommand
            {
                FilmId = rent.FilmId
            });
        }
Ejemplo n.º 11
0
        public void ProcessMessage(IMessageQueueClient mqClient, Message <T> message)
        {
            this.MqClient = mqClient;

            try
            {
                var response   = processMessageFn(message);
                var responseEx = response as Exception;
                if (responseEx != null)
                {
                    throw responseEx;
                }

                this.TotalMessagesProcessed++;

                //If there's no response publish the request message to its OutQ
                if (response == null)
                {
                    var messageOptions = (MessageOption)message.Options;
                    if (messageOptions.Has(MessageOption.NotifyOneWay))
                    {
                        mqClient.Notify(QueueNames <T> .Out, message.ToBytes());
                    }
                }
                else
                {
                    //If there is a response send it to the typed response OutQ
                    var mqReplyTo = message.ReplyTo;

                    if (mqReplyTo == null)
                    {
                        var responseType = response.GetType();
                        if (!responseType.IsUserType())
                        {
                            return;
                        }
                        mqReplyTo = new QueueNames(responseType).In;
                    }

                    var replyClient = ReplyClientFactory(mqReplyTo);
                    if (replyClient != null)
                    {
                        try
                        {
                            replyClient.SendOneWay(mqReplyTo, response);
                            return;
                        }
                        catch (Exception ex)
                        {
                            Log.Error("Could not send response to '{0}' with client '{1}'"
                                      .Fmt(mqReplyTo, replyClient.GetType().Name), ex);

                            var responseType = response.GetType();
                            if (!responseType.IsUserType())
                            {
                                return;
                            }

                            mqReplyTo = new QueueNames(responseType).In;
                        }
                    }

                    //Otherwise send to our trusty response Queue (inc if replyClient fails)
                    var responseMessage = MessageFactory.Create(response);
                    responseMessage.ReplyId = message.Id;
                    mqClient.Publish(mqReplyTo, responseMessage.ToBytes());
                }
            }
            catch (Exception ex)
            {
                try
                {
                    TotalMessagesFailed++;
                    processInExceptionFn(message, ex);
                }
                catch (Exception exHandlerEx)
                {
                    Log.Error("Message exception handler threw an error", exHandlerEx);
                }
            }
        }
Ejemplo n.º 12
0
        public void ProcessMessage(IMessageQueueClient mqClient, IMessage <T> message)
        {
            this.MqClient = mqClient;
            bool msgHandled = false;

            try
            {
                var response   = processMessageFn(message);
                var responseEx = response as Exception;

                if (responseEx == null)
                {
                    var responseStatus = response.GetResponseStatus();
                    var isError        = responseStatus?.ErrorCode != null;
                    if (isError)
                    {
                        responseEx = new MessagingException(responseStatus, response);
                    }
                }

                if (responseEx != null)
                {
                    TotalMessagesFailed++;

                    if (message.ReplyTo != null)
                    {
                        var replyClient = ReplyClientFactory(message.ReplyTo);
                        if (replyClient != null)
                        {
                            replyClient.SendOneWay(message.ReplyTo, response);
                        }
                        else
                        {
                            var responseDto = response.GetResponseDto();
                            mqClient.Publish(message.ReplyTo, MessageFactory.Create(responseDto));
                        }
                        return;
                    }

                    msgHandled = true;
                    processInExceptionFn(this, message, responseEx);
                    return;
                }

                this.TotalMessagesProcessed++;

                //If there's no response publish the request message to its OutQ
                if (response == null)
                {
                    if (message.ReplyTo != null)
                    {
                        response = message.GetBody();
                    }
                    else
                    {
                        var messageOptions = (MessageOption)message.Options;
                        if (messageOptions.Has(MessageOption.NotifyOneWay))
                        {
                            mqClient.Notify(QueueNames <T> .Out, message);
                        }
                    }
                }

                if (response != null)
                {
                    var responseMessage = response as IMessage;
                    var responseType    = responseMessage != null
                        ? (responseMessage.Body != null ? responseMessage.Body.GetType() : typeof(object))
                        : response.GetType();

                    //If there's no explicit ReplyTo, send it to the typed Response InQ by default
                    var mqReplyTo = message.ReplyTo;
                    if (mqReplyTo == null)
                    {
                        //Disable default handling of MQ Responses if whitelist exists and Response not in whitelist
                        var publishAllResponses = PublishResponsesWhitelist == null;
                        if (!publishAllResponses)
                        {
                            var inWhitelist = PublishResponsesWhitelist.Any(
                                publishResponse => responseType.GetOperationName() == publishResponse);
                            if (!inWhitelist)
                            {
                                return;
                            }
                        }

                        // Leave as-is to work around a Mono 2.6.7 compiler bug
                        if (!responseType.IsUserType())
                        {
                            return;
                        }
                        mqReplyTo = new QueueNames(responseType).In;
                    }

                    var replyClient = ReplyClientFactory(mqReplyTo);
                    if (replyClient != null)
                    {
                        try
                        {
                            replyClient.SendOneWay(mqReplyTo, response);
                            return;
                        }
                        catch (Exception ex)
                        {
                            Log.Error($"Could not send response to '{mqReplyTo}' with client '{replyClient.GetType().GetOperationName()}'", ex);

                            // Leave as-is to work around a Mono 2.6.7 compiler bug
                            if (!responseType.IsUserType())
                            {
                                return;
                            }

                            mqReplyTo = new QueueNames(responseType).In;
                        }
                    }

                    //Otherwise send to our trusty response Queue (inc if replyClient fails)
                    if (responseMessage == null)
                    {
                        responseMessage = MessageFactory.Create(response);
                    }

                    responseMessage.ReplyId = message.Id;
                    mqClient.Publish(mqReplyTo, responseMessage);
                }
            }
            catch (Exception ex)
            {
                try
                {
                    TotalMessagesFailed++;
                    msgHandled = true;
                    processInExceptionFn(this, message, ex);
                }
                catch (Exception exHandlerEx)
                {
                    Log.Error("Message exception handler threw an error", exHandlerEx);
                }
            }
            finally
            {
                if (!msgHandled)
                {
                    mqClient.Ack(message);
                }

                this.TotalNormalMessagesReceived++;
                LastMessageProcessed = DateTime.UtcNow;
            }
        }
Ejemplo n.º 13
0
        /// <summary>Process the message.</summary>
        ///
        /// <exception>Thrown when a response ex error condition occurs.</exception>
        ///
        /// <param name="mqClient">The mq client.</param>
        /// <param name="message"> The message.</param>
        public void ProcessMessage(IMessageQueueClient mqClient, Message <T> message)
        {
            this.MqClient = mqClient;

            try
            {
                var response   = processMessageFn(message);
                var responseEx = response as Exception;
                if (responseEx != null)
                {
                    throw responseEx;
                }

                this.TotalMessagesProcessed++;

                //If there's no response publish the request message to its OutQ
                if (response == null)
                {
                    var messageOptions = (MessageOption)message.Options;
                    if (messageOptions.Has(MessageOption.NotifyOneWay))
                    {
                        mqClient.Notify(QueueNames <T> .Out, message.ToBytes());
                    }
                }
                else
                {
                    var responseType = response.GetType();

                    //If there's no explicit ReplyTo, send it to the typed Response InQ by default
                    var mqReplyTo = message.ReplyTo;
                    if (mqReplyTo == null)
                    {
                        //Disable default handling of MQ Responses if whitelist exists and Response not in whitelist
                        var publishAllResponses = PublishResponsesWhitelist == null;
                        if (!publishAllResponses)
                        {
                            var inWhitelist = PublishResponsesWhitelist.Any(publishResponse => responseType.Name == publishResponse);
                            if (!inWhitelist)
                            {
                                return;
                            }
                        }

                        // Leave as-is to work around a Mono 2.6.7 compiler bug
                        if (!StringExtensions.IsUserType(responseType))
                        {
                            return;
                        }
                        mqReplyTo = new QueueNames(responseType).In;
                    }

                    var replyClient = ReplyClientFactory(mqReplyTo);
                    if (replyClient != null)
                    {
                        try
                        {
                            replyClient.SendOneWay(mqReplyTo, response);
                            return;
                        }
                        catch (Exception ex)
                        {
                            Log.Error("Could not send response to '{0}' with client '{1}'"
                                      .Fmt(mqReplyTo, replyClient.GetType().Name), ex);

                            // Leave as-is to work around a Mono 2.6.7 compiler bug
                            if (!StringExtensions.IsUserType(responseType))
                            {
                                return;
                            }

                            mqReplyTo = new QueueNames(responseType).In;
                        }
                    }

                    //Otherwise send to our trusty response Queue (inc if replyClient fails)
                    var responseMessage = MessageFactory.Create(response);
                    responseMessage.ReplyId = message.Id;
                    mqClient.Publish(mqReplyTo, responseMessage.ToBytes());
                }
            }
            catch (Exception ex)
            {
                try
                {
                    TotalMessagesFailed++;
                    processInExceptionFn(message, ex);
                }
                catch (Exception exHandlerEx)
                {
                    Log.Error("Message exception handler threw an error", exHandlerEx);
                }
            }
        }
Ejemplo n.º 14
0
 public void Publish <T>(T messageBody)
 {
     _innerClient.Publish(messageBody);
 }