/// <summary>
        /// Create a reader.
        /// </summary>
        public IReader CreateReader(ReaderOptions options)
        {
            ThrowIfDisposed();

            var correlationId = Guid.NewGuid();
            var executor      = new Executor(correlationId, _processManager, _exceptionHandler);
            var subscribe     = new CommandSubscribe
            {
                ConsumerName   = options.ReaderName,
                Durable        = false,
                ReadCompacted  = options.ReadCompacted,
                StartMessageId = options.StartMessageId.ToMessageIdData(),
                Subscription   = $"Reader-{Guid.NewGuid():N}",
                Topic          = options.Topic
            };
            var messagePrefetchCount  = options.MessagePrefetchCount;
            var batchHandler          = new BatchHandler(false);
            var decompressorFactories = CompressionFactories.DecompressorFactories();
            var factory      = new ConsumerChannelFactory(correlationId, _processManager, _connectionPool, executor, subscribe, messagePrefetchCount, batchHandler, decompressorFactories);
            var stateManager = new StateManager <ReaderState>(ReaderState.Disconnected, ReaderState.Closed, ReaderState.ReachedEndOfTopic, ReaderState.Faulted);
            var reader       = new Reader(correlationId, ServiceUrl, options.Topic, _processManager, new NotReadyChannel(), executor, stateManager);

            if (options.StateChangedHandler is not null)
            {
                _ = StateMonitor.MonitorReader(reader, options.StateChangedHandler);
            }
            var process = new ReaderProcess(correlationId, stateManager, factory, reader);

            _processManager.Add(process);
            process.Start();
            return(reader);
        }
        /// <summary>
        /// Create a consumer.
        /// </summary>
        public IConsumer CreateConsumer(ConsumerOptions options)
        {
            ThrowIfDisposed();

            var correlationId = Guid.NewGuid();
            var executor      = new Executor(correlationId, _processManager, _exceptionHandler);
            var subscribe     = new CommandSubscribe
            {
                ConsumerName    = options.ConsumerName,
                InitialPosition = (CommandSubscribe.InitialPositionType)options.InitialPosition,
                PriorityLevel   = options.PriorityLevel,
                ReadCompacted   = options.ReadCompacted,
                Subscription    = options.SubscriptionName,
                Topic           = options.Topic,
                Type            = (CommandSubscribe.SubType)options.SubscriptionType
            };
            var messagePrefetchCount  = options.MessagePrefetchCount;
            var batchHandler          = new BatchHandler(true);
            var decompressorFactories = CompressionFactories.DecompressorFactories();
            var factory      = new ConsumerChannelFactory(correlationId, _processManager, _connectionPool, executor, subscribe, messagePrefetchCount, batchHandler, decompressorFactories);
            var stateManager = new StateManager <ConsumerState>(ConsumerState.Disconnected, ConsumerState.Closed, ConsumerState.ReachedEndOfTopic, ConsumerState.Faulted);
            var consumer     = new Consumer(correlationId, ServiceUrl, options.SubscriptionName, options.Topic, _processManager, new NotReadyChannel(), executor, stateManager);

            if (options.StateChangedHandler is not null)
            {
                _ = StateMonitor.MonitorConsumer(consumer, options.StateChangedHandler);
            }
            var process = new ConsumerProcess(correlationId, stateManager, factory, consumer, options.SubscriptionType == SubscriptionType.Failover);

            _processManager.Add(process);
            process.Start();
            return(consumer);
        }
Beispiel #3
0
        public Task TestSimpleSetup(Func <object, object> itemHandler = null, Action onBatchProcessed = null, int minimumTimeIntervalMs = 1000, long maximumTimeIntervalMs = -1, int maximumCount = -1, UnprocessedItemAction unprocessedItemAction = UnprocessedItemAction.ReturnDefaultValue)
        {
            if (itemHandler == null)
            {
                itemHandler = (req) => req;
            }

            var batchHandler = new BatchHandler <object, object>(
                (items) =>
            {
                foreach (var item in items)
                {
                    item.SetResponse(itemHandler(item.Request));
                }

                if (onBatchProcessed != null)
                {
                    onBatchProcessed();
                }

                return(Task.FromResult(true));
            });

            return(TestSimpleSetup(batchHandler, minimumTimeIntervalMs, maximumTimeIntervalMs, maximumCount, unprocessedItemAction));
        }
        public async Task <bool> StartupAsync(BatchHandler <TRequest, TResponse> batchHandler, TimeSpan minimumTimeInterval, TimeSpan maximumTimeInterval, int maximumCount)
        {
            await ShutdownAsync();

            if (minimumTimeInterval > maximumTimeInterval)
            {
                throw new ArgumentException("Maximum time interval cannot be less than the minimum time interval");
            }

            if (minimumTimeInterval <= TimeSpan.Zero)
            {
                throw new ArgumentException("Minimum time interval must be greater than zero");
            }

            this.userBatchProcessorHandler = batchHandler;
            this.MinimumTimeInterval       = minimumTimeInterval;
            this.MaximumTimeInterval       = maximumTimeInterval;
            this.MaximumCount = maximumCount;
            IsRunning         = true;

            batchProcessor = new AsyncListProcessor <List <InternalBatchProcessorItem> >(batchProcessor_DoWork, () => IsRunning);
            if (!await batchProcessor.StartupAsync())
            {
                TraceQueue.Trace(this, TracingLevel.Warning, "Failed to startup batch processor.  Shutting down...");
                await ShutdownAsync();

                return(false);
            }

            return(true);
        }
 public async Task TestSimpleSetup(BatchHandler<object, object> batchHandler, double minimumTimeIntervalMs = 1000, double maximumTimeIntervalMs = -1, int maximumCount = -1, UnprocessedItemAction unprocessedItemAction = UnprocessedItemAction.ReturnDefaultValue)
 {
     //Add an item to the queue, and then wait the minimum amount of time specified on the batch processor and check that our
     //item was processed
     processor = new BatchProcessor<object, object>();
     processor.UnprocessedItemAction = unprocessedItemAction;
     if (!await processor.StartupAsync(
         batchHandler, 
         TimeSpan.FromMilliseconds(minimumTimeIntervalMs), 
         (maximumTimeIntervalMs == -1 ? TimeSpan.MaxValue : TimeSpan.FromMilliseconds(maximumTimeIntervalMs)), 
         maximumCount))
     {
         Assert.Inconclusive("Failed to startup batch processor");
     }
 }
Beispiel #6
0
 public async Task TestSimpleSetup(BatchHandler <object, object> batchHandler, double minimumTimeIntervalMs = 1000, double maximumTimeIntervalMs = -1, int maximumCount = -1, UnprocessedItemAction unprocessedItemAction = UnprocessedItemAction.ReturnDefaultValue)
 {
     //Add an item to the queue, and then wait the minimum amount of time specified on the batch processor and check that our
     //item was processed
     processor = new BatchProcessor <object, object>();
     processor.UnprocessedItemAction = unprocessedItemAction;
     if (!await processor.StartupAsync(
             batchHandler,
             TimeSpan.FromMilliseconds(minimumTimeIntervalMs),
             (maximumTimeIntervalMs == -1 ? TimeSpan.MaxValue : TimeSpan.FromMilliseconds(maximumTimeIntervalMs)),
             maximumCount))
     {
         Assert.Inconclusive("Failed to startup batch processor");
     }
 }
        public Task TestSimpleSetup(Func<object, object> itemHandler = null, Action onBatchProcessed = null, int minimumTimeIntervalMs = 1000, long maximumTimeIntervalMs = -1, int maximumCount = -1, UnprocessedItemAction unprocessedItemAction = UnprocessedItemAction.ReturnDefaultValue)
        {
            if (itemHandler == null)
                itemHandler = (req) => req;

            var batchHandler = new BatchHandler<object, object>(
                (items) =>
                {
                    foreach (var item in items)
                        item.SetResponse(itemHandler(item.Request));

                    if (onBatchProcessed != null)
                        onBatchProcessed();

                    return Task.FromResult(true);
                });

            return TestSimpleSetup(batchHandler, minimumTimeIntervalMs, maximumTimeIntervalMs, maximumCount, unprocessedItemAction);
        }
 public Task <bool> StartupAsync(BatchHandler <TRequest, TResponse> batchHandler, TimeSpan minimumTimeInterval, TimeSpan maximumTimeInterval)
 {
     return(StartupAsync(batchHandler, minimumTimeInterval, maximumTimeInterval, -1));
 }