Пример #1
0
        protected override void OnSetReceiveHandler(IPartitionReceiveHandler newReceiveHandler)
        {
            lock (this.receivePumpLock)
            {
                if (this.receiveHandler != null)
                {
                    // Notify existing handler first (but don't wait).
                    this.receiveHandler.ProcessErrorAsync(new OperationCanceledException("New handler has registered for this receiver.")); // .Fork();
                }

                this.receiveHandler = newReceiveHandler;
                if (this.receiveHandler != null)
                {
                    // We have a new receiveHandler, ensure pump is running.
                    if (this.receivePumpTask == null)
                    {
                        this.receivePumpCancellationSource = new CancellationTokenSource();
                        this.receivePumpTask = this.ReceivePumpAsync(this.receivePumpCancellationSource.Token);
                    }
                }
                else
                {
                    // We have no receiveHandler, ensure pump is shut down.
                    if (this.receivePumpTask != null)
                    {
                        this.receivePumpCancellationSource.Cancel();
                        this.receivePumpCancellationSource.Dispose();
                        this.receivePumpCancellationSource = null;
                        this.receivePumpTask = null;
                    }
                }
            }
        }
 /// <summary>
 /// Set a mock receive handler.
 /// </summary>
 /// <param name="receiveHandler"></param>
 /// <param name="invokeWhenNoEvents"></param>
 public void SetReceiveHandler(IPartitionReceiveHandler receiveHandler, bool invokeWhenNoEvents = false)
 {
     EventProcessorEventSource.Current.Message("MOCK IPartitionReceiver.SetReceiveHandler");
     this.outerHandler       = receiveHandler;
     this.invokeWhenNoEvents = invokeWhenNoEvents; // TODO mock does not emulate receive timeouts
     if (this.outerHandler != null)
     {
         Task.Run(() => GenerateMessages());
     }
 }
 private async void GenerateMessages()
 {
     while ((!this.token.IsCancellationRequested) && (this.outerHandler != null))
     {
         EventProcessorEventSource.Current.Message("MOCK Generating messages and sending to handler");
         IEnumerable <EventData>  events          = ReceiveAsync(10, TimeSpan.FromSeconds(10.0)).Result; // TODO get count from somewhere real
         IPartitionReceiveHandler capturedHandler = this.outerHandler;
         if (capturedHandler != null)
         {
             await capturedHandler.ProcessEventsAsync(events);
         }
     }
     EventProcessorEventSource.Current.Message("MOCK Message generation ending");
 }
Пример #4
0
        // Encapsulates taking the receivePumpLock, checking this.receiveHandler for null,
        // calls this.receiveHandler.CloseAsync (starting this operation inside the receivePumpLock).
        void ReceiveHandlerClose()
        {
            lock (this.receivePumpLock)
            {
                if (this.receiveHandler != null)
                {
                    if (this.receivePumpTask != null)
                    {
                        this.receivePumpCancellationSource.Cancel();
                        this.receivePumpCancellationSource.Dispose();
                        this.receivePumpCancellationSource = null;
                        this.receivePumpTask = null;
                    }

                    this.receiveHandler = null;
                }
            }
        }
Пример #5
0
        protected override void OnSetReceiveHandler(IPartitionReceiveHandler newReceiveHandler, bool invokeWhenNoEvents)
        {
            lock (this.receivePumpLock)
            {
                if (newReceiveHandler != null)
                {
                    if (this.receiveHandler != null)
                    {
                        // Notify existing handler first (but don't wait).
                        Task.Run(() =>
                                 this.receiveHandler.ProcessErrorAsync(new OperationCanceledException("New handler has registered for this receiver.")))
                        .ContinueWith(t =>
                                      t.Exception.Handle(ex =>
                        {
                            // We omit any failures from ProcessErrorAsync
                            return(true);
                        }), TaskContinuationOptions.OnlyOnFaulted);
                    }

                    this.receiveHandler = newReceiveHandler;

                    // We have a new receiveHandler, ensure pump is running.
                    if (this.receivePumpTask == null)
                    {
                        this.receivePumpCancellationSource = new CancellationTokenSource();
                        this.receivePumpTask = this.ReceivePumpAsync(this.receivePumpCancellationSource.Token, invokeWhenNoEvents);
                    }
                }
                else
                {
                    // newReceiveHandler == null, so this is an unregister call, ensure pump is shut down.
                    if (this.receivePumpTask != null)
                    {
                        // Do not wait as could block and would still match the previous behavior
                        this.ReceiveHandlerClose();
                    }

                    this.receiveHandler = null;
                }
            }
        }
Пример #6
0
        // Encapsulates taking the receivePumpLock, checking this.receiveHandler for null,
        // calls this.receiveHandler.CloseAsync (starting this operation inside the receivePumpLock).
        Task ReceiveHandlerClose()
        {
            Task task = null;

            lock (this.receivePumpLock)
            {
                if (this.receiveHandler != null)
                {
                    if (this.receivePumpTask != null)
                    {
                        task = this.receivePumpTask;
                        this.receivePumpCancellationSource.Cancel();
                        this.receivePumpCancellationSource.Dispose();
                        this.receivePumpCancellationSource = null;
                        this.receivePumpTask = null;
                    }

                    this.receiveHandler = null;
                }
            }

            return(task ?? Task.CompletedTask);
        }
        // Encapsulates taking the receivePumpLock, checking this.receiveHandler for null,
        // calls this.receiveHandler.CloseAsync (starting this operation inside the receivePumpLock).
        private Task ReceiveHandlerClose()
        {
            Task task = null;

            lock (receivePumpLock)
            {
                if (receiveHandler != null)
                {
                    if (receivePumpTask != null)
                    {
                        task = receivePumpTask;
                        receivePumpCancellationSource.Cancel();
                        receivePumpCancellationSource.Dispose();
                        receivePumpCancellationSource = null;
                        receivePumpTask = null;
                    }

                    receiveHandler = null;
                }
            }

            return(task ?? Task.CompletedTask);
        }
Пример #8
0
 protected override void OnSetReceiveHandler(IPartitionReceiveHandler receiveHandler, bool invokeWhenNoEvents)
 {
     //TODO: Implement
     throw new NotImplementedException();
 }
Пример #9
0
 /// <summary></summary>
 /// <param name="receiveHandler"></param>
 /// <param name="invokeWhenNoEvents"></param>
 protected abstract void OnSetReceiveHandler(IPartitionReceiveHandler receiveHandler, bool invokeWhenNoEvents);
Пример #10
0
 /// <summary>
 /// Sets the <see cref="IPartitionReceiveHandler"/> to process events.
 /// </summary>
 /// <param name="receiveHandler">The <see cref="IPartitionReceiveHandler"/> used to process events.</param>
 /// <param name="invokeWhenNoEvents">Flag to indicate whether the handler should be invoked when the receive call times out.</param>
 public void SetReceiveHandler(IPartitionReceiveHandler receiveHandler, bool invokeWhenNoEvents = false)
 {
     EventHubsEventSource.Log.SetReceiveHandlerStart(this.ClientId, receiveHandler != null ? receiveHandler.GetType().ToString() : "null");
     this.OnSetReceiveHandler(receiveHandler, invokeWhenNoEvents);
     EventHubsEventSource.Log.SetReceiveHandlerStop(this.ClientId);
 }
Пример #11
0
 public void SetReceiveHandler(IPartitionReceiveHandler receiveHandler, bool invokeWhenNoEvents = false)
 {
     this.inner.SetReceiveHandler(receiveHandler, invokeWhenNoEvents);
 }
 /// <summary></summary>
 /// <param name="receiveHandler"></param>
 protected abstract void OnSetReceiveHandler(IPartitionReceiveHandler receiveHandler);
 /// <summary>
 /// Sets the <see cref="IPartitionReceiveHandler"/> to process events.
 /// </summary>
 /// <param name="receiveHandler">The <see cref="IPartitionReceiveHandler"/> used to process events.</param>
 public void SetReceiveHandler(IPartitionReceiveHandler receiveHandler)
 {
     EventHubsEventSource.Log.SetReceiveHandlerStart(this.ClientId, receiveHandler != null ? receiveHandler.GetType().ToString() : "null");
     this.OnSetReceiveHandler(receiveHandler);
     EventHubsEventSource.Log.SetReceiveHandlerStop(this.ClientId);
 }
Пример #14
0
        /// <inheritdoc />
        public BoolResult StartProcessing(OperationContext context, EventSequencePoint sequencePoint, IPartitionReceiveHandler processor)
        {
            Tracer.Info(context, $"{Tracer.Name}: Initializing event processing for event hub '{_configuration.EventHubName}' and consumer group '{_configuration.ConsumerGroupName}'.");

            if (_partitionReceiver == null)
            {
                _partitionReceiver = _eventHubClient.CreateReceiver(
                    _configuration.ConsumerGroupName,
                    PartitionId,
                    GetInitialOffset(context, sequencePoint),
                    new ReceiverOptions()
                {
                    Identifier = _hostName
                });

                _partitionReceiver.SetReceiveHandler(processor);
            }

            return(BoolResult.Success);
        }
Пример #15
0
 private void Dispatch(EventData eventData, IPartitionReceiveHandler processor)
 {
     processor.ProcessEventsAsync(new[] { eventData }).GetAwaiter().GetResult();
 }
Пример #16
0
        /// <inheritdoc />
        public BoolResult StartProcessing(OperationContext context, EventSequencePoint sequencePoint, IPartitionReceiveHandler processor)
        {
            using (_lock.AcquireWriteLock())
            {
                _handler = ev => Dispatch(ev, processor);
                var events = _hub.SubscribeAndGetEventsStartingAtSequencePoint(sequencePoint, _handler);

                foreach (var eventData in events)
                {
                    _handler(eventData);
                }
            }

            return(BoolResult.Success);
        }