Esempio n. 1
0
        void TryProcessIncomingMessage()
        {
            using (var transactionScope = BeginTransaction())
            {
                var transportMessage = receiveMessages.ReceiveMessage();

                if (transportMessage == null)
                {
                    Thread.Sleep(20);
                    return;
                }

                BeforeMessage();

                var id    = transportMessage.Id;
                var label = transportMessage.Label;

                if (errorTracker.MessageHasFailedMaximumNumberOfTimes(id))
                {
                    log.Error("Handling message {0} has failed the maximum number of times", id);
                    MessageFailedMaxNumberOfTimes(transportMessage, errorTracker.GetErrorText(id));
                    errorTracker.StopTracking(id);
                    PoisonMessage();
                }
                else
                {
                    try
                    {
                        var message       = serializeMessages.Deserialize(transportMessage);
                        var returnAddress = message.GetHeader(Headers.ReturnAddress);

                        using (MessageContext.Enter(returnAddress))
                        {
                            foreach (var logicalMessage in message.Messages)
                            {
                                var typeToDispatch = logicalMessage.GetType();

                                log.Debug("Dispatching message {0}: {1}", id, typeToDispatch);

                                GetDispatchMethod(typeToDispatch).Invoke(this, new[] { logicalMessage });
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        log.Debug("Handling message {0} ({1}) has failed", label, id);
                        errorTracker.TrackDeliveryFail(id, exception);
                        AfterMessage(exception);
                        throw;
                    }
                }

                transactionScope.Complete();
                errorTracker.StopTracking(id);
                AfterMessage(null);
            }
        }
Esempio n. 2
0
        protected override void DoSetUp()
        {
            factory = Activator.CreateInstance <TFactory>();
            var headers = new Dictionary <string, string>
            {
                { Headers.ReturnAddress, "none" },
                { Headers.MessageId, "just_some_message_id" },
            };

            messageContext = MessageContext.Enter(headers);
            Persister      = factory.CreatePersister();
        }
Esempio n. 3
0
        void TryProcessIncomingMessage()
        {
            using (var transactionScope = BeginTransaction())
            {
                var transportMessage = receiveMessages.ReceiveMessage();

                if (transportMessage == null)
                {
                    Thread.Sleep(20);
                    return;
                }

                var id    = transportMessage.Id;
                var label = transportMessage.Label;

                MessageContext context = null;

                if (errorTracker.MessageHasFailedMaximumNumberOfTimes(id))
                {
                    log.Error("Handling message {0} has failed the maximum number of times", id);
                    MessageFailedMaxNumberOfTimes(transportMessage, errorTracker.GetErrorText(id));
                    errorTracker.StopTracking(id);

                    try
                    {
                        PoisonMessage(transportMessage);
                    }
                    catch (Exception exceptionWhileRaisingEvent)
                    {
                        log.Error("An exception occurred while raising the PoisonMessage event: {0}",
                                  exceptionWhileRaisingEvent);
                    }
                }
                else
                {
                    try
                    {
                        BeforeTransportMessage(transportMessage);

                        var message = serializeMessages.Deserialize(transportMessage);

                        // successfully deserialized the transport message, let's enter a message context
                        context = MessageContext.Enter(message.Headers);

                        foreach (var logicalMessage in message.Messages)
                        {
                            context.SetLogicalMessage(logicalMessage);

                            try
                            {
                                BeforeMessage(logicalMessage);

                                var typeToDispatch = logicalMessage.GetType();

                                log.Debug("Dispatching message {0}: {1}", id, typeToDispatch);

                                GetDispatchMethod(typeToDispatch).Invoke(this, new[] { logicalMessage });

                                AfterMessage(null, logicalMessage);
                            }
                            catch (Exception exception)
                            {
                                try
                                {
                                    AfterMessage(exception, logicalMessage);
                                }
                                catch (Exception exceptionWhileRaisingEvent)
                                {
                                    log.Error("An exception occurred while raising the AfterMessage event, and an exception occurred some time before that as well. The first exception was this: {0}. And then, when raising the AfterMessage event (including the details of the first error), this exception occurred: {1}",
                                              exception, exceptionWhileRaisingEvent);
                                }
                                throw;
                            }
                            finally
                            {
                                context.ClearLogicalMessage();
                            }
                        }

                        AfterTransportMessage(null, transportMessage);
                    }
                    catch (Exception exception)
                    {
                        log.Debug("Handling message {0} ({1}) has failed", label, id);
                        try
                        {
                            AfterTransportMessage(exception, transportMessage);
                        }
                        catch (Exception exceptionWhileRaisingEvent)
                        {
                            log.Error("An exception occurred while raising the AfterTransportMessage event, and an exception occurred some time before that as well. The first exception was this: {0}. And then, when raising the AfterTransportMessage event (including the details of the first error), this exception occurred: {1}",
                                      exception, exceptionWhileRaisingEvent);
                        }
                        errorTracker.TrackDeliveryFail(id, exception);
                        if (context != null)
                        {
                            context.Dispose();                  //< dispose it if we entered
                        }
                        throw;
                    }
                }

                transactionScope.Complete();
                if (context != null)
                {
                    context.Dispose();                  //< dispose it if we entered
                }
                errorTracker.StopTracking(id);
            }
        }