Exemple #1
0
        protected static void SendMessageToNewSaga <TMessage>(ISagaPolicy <TSaga, TMessage> policy, TMessage message, Action <TSaga> consumerAction, Action <TSaga> removeAction)
        {
            TSaga saga;

            if (!policy.CreateSagaWhenMissing(message, out saga))
            {
                return;
            }

            if (_log.IsDebugEnabled)
            {
                _log.DebugFormat("Created saga [{0}] - {1}", typeof(TSaga).ToFriendlyName(), saga.CorrelationId);
            }

            try
            {
                lock (saga)
                    consumerAction(saga);

                if (policy.ShouldSagaBeRemoved(saga))
                {
                    removeAction(saga);
                }
            }
            catch (Exception ex)
            {
                var sex = new SagaException("Saga consumer exception", typeof(TSaga), typeof(TMessage), saga.CorrelationId, ex);
                if (_log.IsErrorEnabled)
                {
                    _log.Error("Existing Saga Exception: ", sex);
                }

                throw sex;
            }
        }
Exemple #2
0
        protected static bool SendMessageToExistingSagas <TMessage>(IEnumerable <TSaga> existingSagas,
                                                                    ISagaPolicy <TSaga, TMessage> policy,
                                                                    Action <TSaga> consumerAction,
                                                                    TMessage message,
                                                                    Action <TSaga> removeAction)
        {
            int       sagaCount     = 0;
            Exception lastException = null;

            foreach (TSaga saga in existingSagas)
            {
                try
                {
                    sagaCount++;

                    if (_log.IsDebugEnabled)
                    {
                        _log.DebugFormat("Found saga [{0}] - {1}", typeof(TSaga).ToFriendlyName(), saga.CorrelationId);
                    }

                    policy.ForExistingSaga(message);

                    lock (saga)
                        consumerAction(saga);

                    if (policy.ShouldSagaBeRemoved(saga))
                    {
                        removeAction(saga);
                    }
                }
                catch (Exception ex)
                {
                    var sex = new SagaException("Saga consumer exception", typeof(TSaga), typeof(TMessage), saga.CorrelationId, ex);
                    if (_log.IsErrorEnabled)
                    {
                        _log.Error("Existing Saga Exception: ", sex);
                    }

                    lastException = sex;
                }
            }

            if (lastException != null)
            {
                throw lastException;
            }

            return(sagaCount > 0);
        }