Exemplo n.º 1
0
        public void PurgeErrorMessages <TTransportInit>(QueueConnection queueConnection,
                                                        bool addInterceptors, ILogger logProvider, bool actuallyPurge, ICreationScope scope)
            where TTransportInit : ITransportInit, new()
        {
            using (var trace = SharedSetup.CreateTrace("consumer-error"))
            {
                using (var metrics = new Metrics.Metrics(queueConnection.Queue))
                {
                    var addInterceptorConsumer = InterceptorAdding.No;
                    if (addInterceptors)
                    {
                        addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                    }

                    var processedCount = new IncrementWrapper();
                    using (
                        var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider,
                                                                                 metrics, false, false, scope, trace.Source)
                        )
                    {
                        using (var schedulerCreator =
                                   new SchedulerContainer(
                                       // ReSharper disable once AccessToDisposedClosure
                                       serviceRegister => serviceRegister.Register(() => metrics, LifeStyles.Singleton).RegisterNonScopedSingleton(trace.Source),
                                       options => SharedSetup.SetOptions(options, false)))
                        {
                            using (var taskScheduler = schedulerCreator.CreateTaskScheduler())
                            {
                                taskScheduler.Start();
                                var taskFactory = schedulerCreator.CreateTaskFactory(taskScheduler);

                                using (
                                    var queue =
                                        creator
                                        .CreateConsumerQueueScheduler(
                                            queueConnection, taskFactory))
                                {
                                    SharedSetup.SetupDefaultConsumerQueueErrorPurge(queue.Configuration, actuallyPurge);
                                    SharedSetup.SetupDefaultErrorRetry(queue.Configuration);

                                    var waitForFinish = new ManualResetEventSlim(false);
                                    waitForFinish.Reset();

                                    //start looking for work
                                    queue.Start <TMessage>((message, notifications) =>
                                                           throw new Exception("There should have been no data to process"));

                                    //wait for 30 seconds
                                    waitForFinish.Wait(15000);
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void RunConsumer <TTransportInit>(string queueName, string connectionString, bool addInterceptors,
                                                 ILogProvider logProvider,
                                                 int messageCount, int workerCount, int timeOut,
                                                 int queueSize, int readerCount,
                                                 TimeSpan heartBeatTime, TimeSpan heartBeatMonitorTime,
                                                 string updateTime,
                                                 string route,
                                                 bool enableChaos)
            where TTransportInit : ITransportInit, new()
        {
            if (enableChaos)
            {
                timeOut *= 2;
            }

            using (var metrics = new Metrics.Metrics(queueName))
            {
                var addInterceptorConsumer = InterceptorAdding.No;
                if (addInterceptors)
                {
                    addInterceptorConsumer = InterceptorAdding.ConfigurationOnly;
                }

                var processedCount = new IncrementWrapper();
                using (
                    var creator = SharedSetup.CreateCreator <TTransportInit>(addInterceptorConsumer, logProvider, metrics, false, enableChaos)
                    )
                {
                    using (var schedulerCreator =
                               new SchedulerContainer(
                                   // ReSharper disable once AccessToDisposedClosure
                                   serviceRegister => serviceRegister.Register(() => metrics, LifeStyles.Singleton), options => SharedSetup.SetOptions(options, enableChaos)))
                    {
                        bool rollBacks;
                        using (var taskScheduler = schedulerCreator.CreateTaskScheduler())
                        {
                            taskScheduler.Configuration.MaximumThreads = workerCount;
                            taskScheduler.Configuration.MaxQueueSize   = queueSize;

                            taskScheduler.Start();
                            var taskFactory = schedulerCreator.CreateTaskFactory(taskScheduler);

                            using (
                                var queue =
                                    creator
                                    .CreateConsumerQueueScheduler(
                                        queueName, connectionString, taskFactory))
                            {
                                rollBacks = queue.Configuration.TransportConfiguration.MessageRollbackSupported;
                                SharedSetup.SetupDefaultConsumerQueue(queue.Configuration, readerCount, heartBeatTime,
                                                                      heartBeatMonitorTime, updateTime, route);
                                SharedSetup.SetupDefaultErrorRetry(queue.Configuration);

                                var waitForFinish = new ManualResetEventSlim(false);
                                waitForFinish.Reset();

                                //start looking for work
                                queue.Start <TMessage>((message, notifications) =>
                                {
                                    MessageHandlingShared.HandleFakeMessagesError(processedCount, waitForFinish,
                                                                                  messageCount, message);
                                });

                                waitForFinish.Wait(timeOut * 1000);

                                //wait for last error to be saved if needed.
                                Thread.Sleep(3000);
                            }
                        }

                        if (rollBacks)
                        {
                            VerifyMetrics.VerifyRollBackCount(queueName, metrics.GetCurrentMetrics(), messageCount, 2, 2);
                        }
                    }
                }
            }
        }