Example #1
0
        public Task OnSubscribed(IStreamSubscriptionHandleFactory handleFactory)
        {
            StreamSubscriptionHandle <string> handle = handleFactory.Create <string>();

            return((handle.StreamId.GetNamespace() == StreamBatchingTestConst.BatchingNameSpace)
                ? handle.ResumeAsync(OnNextBatch)
                : handle.ResumeAsync(OnNext));
        }
Example #2
0
 /// <summary>
 /// Resumes consumption from a subscription to a stream.
 /// </summary>
 /// <param name="handle">The stream handle to consume from.</param>
 /// <param name="observer">The observer reference</param>
 /// <param name="token">The (optional) stream sequence token to be used as an offset to start the subscription from.</param>
 /// <returns>A promise with an updates subscription handle.
 /// </returns>
 public static Task <StreamSubscriptionHandle <object> > ResumeAsync <T>(
     this StreamSubscriptionHandle <object> handle,
     IAsyncObserver <T> observer,
     StreamSequenceToken token = null)
 {
     return(handle.ResumeAsync(new GenericAsyncObserver <T>(observer), token));
 }
Example #3
0
 /// <summary>
 /// <exception cref="ArgumentException">Thrown if the supplied stream filter function is not suitable.
 /// Usually this is because it is not a static method. </exception>
 /// </summary>
 /// <typeparam name="T">The type of object produced by the observable.</typeparam>
 /// <param name="handle">The StreamSubscriptionHandle object.</param>
 /// <param name="onNextAsync">Delegte that is called for IAsyncObserver.OnNextAsync.</param>
 /// <param name="token">The stream sequence to be used as an offset to start the subscription from.</param>
 /// <returns>A promise for a StreamSubscriptionHandle that represents the subscription.
 /// The consumer may unsubscribe by using this handle.
 /// The subscription remains active for as long as it is not explicitely unsubscribed.
 /// </returns>
 public static Task <StreamSubscriptionHandle <object> > ResumeAsync <T>(
     this StreamSubscriptionHandle <object> handle,
     Func <T, StreamSequenceToken, Task> onNextAsync,
     StreamSequenceToken token = null)
 {
     return(handle.ResumeAsync(onNextAsync, DefaultOnError, DefaultOnCompleted, token));
 }
        public async Task <StreamSubscriptionHandle <int> > Resume(StreamSubscriptionHandle <int> handle)
        {
            logger.Info("Resume");
            if (handle == null)
            {
                throw new ArgumentNullException("handle");
            }

            // new counter for this subscription
            Tuple <Counter, Counter> counters;

            if (!consumedMessageCounts.TryGetValue(handle, out counters))
            {
                counters = Tuple.Create(new Counter(), new Counter());
            }

            int countCapture = consumerCount;

            consumerCount++;
            // subscribe
            StreamSubscriptionHandle <int> newhandle = await handle.ResumeAsync(
                (e, t) => OnNext(e, t, countCapture, counters.Item1),
                e => OnError(e, countCapture, counters.Item2));

            // track counter
            consumedMessageCounts[newhandle] = counters;

            // return handle
            return(newhandle);
        }
        public async Task <StreamSubscriptionHandle <int> > Resume(StreamSubscriptionHandle <int> handle)
        {
            logger.Info("Resume");
            if (handle == null)
            {
                throw new ArgumentNullException("handle");
            }

            // new counter for this subscription
            Counter count;

            if (!consumedMessageCounts.TryGetValue(handle, out count))
            {
                count = new Counter();
            }

            // subscribe
            StreamSubscriptionHandle <int> newhandle = await handle.ResumeAsync((e, t) =>
            {
                logger.Info("Got next event {0}", e);
                string contextValue = RequestContext.Get(SampleStreaming_ProducerGrain.RequestContextKey) as string;
                if (!String.Equals(contextValue, SampleStreaming_ProducerGrain.RequestContextValue))
                {
                    throw new Exception(String.Format("Got the wrong RequestContext value {0}.", contextValue));
                }
                count.Increment();
                return(TaskDone.Done);
            });

            // track counter
            consumedMessageCounts[newhandle] = count;

            // return handle
            return(newhandle);
        }
        public async override Task OnActivateAsync()
        {
            var streamProvider      = GetStreamProvider("SMSProvider");
            var stream              = streamProvider.GetStream <string>(this.GetPrimaryKey(), "MyStreamNamespace");
            var subscriptionHandles = await stream.GetAllSubscriptionHandles();

            // This is a common function in my real project
            if (subscriptionHandles == null || subscriptionHandles.Count == 0)
            {
                _streamHandle = await stream.SubscribeAsync(ObserveAsync);
            }
            else if (subscriptionHandles.Count == 1)
            {
                _streamHandle = subscriptionHandles.First();
                await _streamHandle.ResumeAsync(ObserveAsync);
            }
            else if (subscriptionHandles.Count > 1)
            {
                foreach (var h in subscriptionHandles)
                {
                    await h.UnsubscribeAsync();
                }

                _streamHandle = await stream.SubscribeAsync(ObserveAsync);
            }
            //
        }
Example #7
0
        public async Task <StreamSubscriptionHandle <int> > Resume(StreamSubscriptionHandle <int> handle)
        {
            logger.Info("Resume");
            if (handle == null)
            {
                throw new ArgumentNullException("handle");
            }

            // new counter for this subscription
            Counter count;

            if (!consumedMessageCounts.TryGetValue(handle, out count))
            {
                count = new Counter();
            }

            // subscribe
            StreamSubscriptionHandle <int> newhandle = await handle.ResumeAsync((e, t) => count.Increment());

            // track counter
            consumedMessageCounts[newhandle] = count;

            // return handle
            return(newhandle);
        }
Example #8
0
        public virtual async Task <StreamSubscription> Resume(Func <object, Task> callback)
        {
            Requires.NotNull(callback, nameof(callback));
            var observer = new StreamRef.Observer((item, token) => callback(item));

            return(new StreamSubscription(await handle.ResumeAsync(observer)));
        }
 public async override Task OnActivateAsync()
 {
     if (null != _subscription)
     {
         await _subscription.ResumeAsync(this);
     }
 }
Example #10
0
        private async Task OnAdd2(StreamSubscriptionHandle <string> handle)
        {
            var observer  = new ConsumerObserver <string>(this.logger);
            var newhandle = await handle.ResumeAsync(observer);

            this.consumerObservers2.Add(observer);
            this.consumerHandles2.Add(newhandle);
        }
Example #11
0
        private async Task OnAdd(StreamSubscriptionHandle <int> handle)
        {
            this.onAddCalledCount++;
            var observer  = new ConsumerObserver <int>(this.logger);
            var newhandle = await handle.ResumeAsync(observer);

            this.consumerObservers.Add(observer);
            this.consumerHandles.Add(newhandle);
        }
Example #12
0
        public async Task OnSubscribed(StreamSubscriptionHandle <IFruit> handle)
        {
            logger.Info("OnAdd");
            this.onAddCalledCount++;
            var observer  = new CounterObserver <IFruit>(this.logger);
            var newhandle = new ExternalStreamSubscriptionHandle <IFruit>(await handle.ResumeAsync(observer)) as IExternalStreamSubscriptionHandle;

            this.consumerHandles.Add(newhandle);
            this.consumerObservers.Add(observer);
        }
Example #13
0
        /// <summary>
        /// Resumes consumption of a stream using delegates.
        /// This method is a helper for the StreamSubscriptionHandle.ResumeAsync allowing the subscribing class to inline the
        /// handler methods instead of requiring an instance of IAsyncObserver.
        /// </summary>
        /// <typeparam name="T">The type of object produced by the observable.</typeparam>
        /// <param name="handle">The Observable object.</param>
        /// <param name="onNextAsync">Delegte that is called for IAsyncObserver.OnNextAsync.</param>
        /// <param name="onErrorAsync">Delegte that is called for IAsyncObserver.OnErrorAsync.</param>
        /// <param name="onCompletedAsync">Delegte that is called for IAsyncObserver.OnCompletedAsync.</param>
        /// <param name="token">The stream sequence to be used as an offset to start the subscription from.</param>
        /// <returns>A promise for a StreamSubscriptionHandle that represents the subscription.
        /// The consumer may unsubscribe by using this handle.
        /// The subscription remains active for as long as it is not explicitely unsubscribed.
        /// </returns>
        public static Task <StreamSubscriptionHandle <object> > ResumeAsync <T>(
            this StreamSubscriptionHandle <object> handle,
            Func <T, StreamSequenceToken, Task> onNextAsync,
            Func <Exception, Task> onErrorAsync,
            Func <Task> onCompletedAsync,
            StreamSequenceToken token = null)
        {
            var genericObserver = new DelegateAsyncObserver <T>(onNextAsync, onErrorAsync, onCompletedAsync);

            return(handle.ResumeAsync(genericObserver, token));
        }
        public async Task<StreamSubscriptionHandle<int>> Resume(StreamSubscriptionHandle<int> handle)
        {
            logger.Info("Resume");
            if(handle == null)
                throw new ArgumentNullException("handle");

            // new counter for this subscription
            Counter count;
            if(!consumedMessageCounts.TryGetValue(handle, out count))
            {
                count = new Counter();
            }

            // subscribe
            StreamSubscriptionHandle<int> newhandle = await handle.ResumeAsync((e, t) => count.Increment());

            // track counter
            consumedMessageCounts[newhandle] = count;

            // return handle
            return newhandle;

        }
        public async Task<StreamSubscriptionHandle<int>> Resume(StreamSubscriptionHandle<int> handle)
        {
            logger.Info("Resume");
            if(handle == null)
                throw new ArgumentNullException("handle");

            // new counter for this subscription
            Tuple<Counter,Counter> counters;
            if (!consumedMessageCounts.TryGetValue(handle, out counters))
            {
                counters = Tuple.Create(new Counter(), new Counter());
            }

            int countCapture = consumerCount;
            consumerCount++;
            // subscribe
            StreamSubscriptionHandle<int> newhandle = await handle.ResumeAsync(
                (e, t) => OnNext(e, t, countCapture, counters.Item1),
                e => OnError(e, countCapture, counters.Item2));

            // track counter
            consumedMessageCounts[newhandle] = counters;

            // return handle
            return newhandle;

        }
        public async Task<StreamSubscriptionHandle<int>> Resume(StreamSubscriptionHandle<int> handle)
        {
            logger.Info("Resume");
            if(handle == null)
                throw new ArgumentNullException("handle");

            // new counter for this subscription
            Counter count;
            if(!consumedMessageCounts.TryGetValue(handle, out count))
            {
                count = new Counter();
            }

            // subscribe
            StreamSubscriptionHandle<int> newhandle = await handle.ResumeAsync((e, t) =>
            {
                logger.Info("Got next event {0}", e);
                string contextValue = RequestContext.Get(SampleStreaming_ProducerGrain.RequestContextKey) as string;
                if (!String.Equals(contextValue, SampleStreaming_ProducerGrain.RequestContextValue))
                {
                    throw new Exception(String.Format("Got the wrong RequestContext value {0}.", contextValue));
                }
                count.Increment();
                return TaskDone.Done;
            });

            // track counter
            consumedMessageCounts[newhandle] = count;

            // return handle
            return newhandle;

        }