/// <summary>
 /// Adds the specified <see cref="IEventSubscription"/> to the subscribers' collection.
 /// </summary>
 /// <param name="eventSubscription">The subscriber.</param>
 /// <returns>The <see cref="SubscriptionToken"/> that uniquely identifies every subscriber.</returns>
 /// <remarks>
 /// Adds the subscription to the internal list and assigns it a new <see cref="SubscriptionToken"/>.
 /// </remarks>
 protected virtual SubscriptionToken InternalSubscribe(IEventSubscription eventSubscription)
 {
     eventSubscription.SubscriptionToken = new SubscriptionToken();
     lock (Subscriptions)
     {
         Subscriptions.Add(eventSubscription);
     }
     return eventSubscription.SubscriptionToken;
 }
        /// <summary>
        /// Adds the specified <see cref="IEventSubscription"/> to the subscribers' collection.
        /// </summary>
        /// <param name="eventSubscription">The subscriber.</param>
        /// <returns>The <see cref="SubscriptionToken"/> that uniquely identifies every subscriber.</returns>
        /// <remarks>
        /// Adds the subscription to the internal list and assigns it a new <see cref="SubscriptionToken"/>.
        /// </remarks>
        protected virtual SubscriptionToken InternalSubscribe(IEventSubscription eventSubscription)
        {
            if (eventSubscription == null) throw new System.ArgumentNullException("eventSubscription");

            eventSubscription.SubscriptionToken = new SubscriptionToken();
            lock (Subscriptions)
            {
                Subscriptions.Add(eventSubscription);
            }
            return eventSubscription.SubscriptionToken;
        }
        protected virtual SubscriptionToken Subscribe(IEventSubscription eventSubscription)
        {
            eventSubscription.SubscriptionToken = new SubscriptionToken();

            lock (this._subscriptions)
            {
                this._subscriptions.Add(eventSubscription);
            }

            return eventSubscription.SubscriptionToken;
        }
Exemple #4
0
        /// <summary>
        /// Adds the specified <see cref="IEventSubscription"/> to the subscribers' collection.
        /// </summary>
        /// <param name="eventSubscription">The subscriber.</param>
        /// <returns>The <see cref="SubscriptionToken"/> that uniquely identifies every subscriber.</returns>
        /// <remarks>
        /// Adds the subscription to the internal list and assigns it a new <see cref="SubscriptionToken"/>.
        /// </remarks>
        protected virtual SubscriptionToken InternalSubscribe(IEventSubscription eventSubscription)
        {
            if (eventSubscription == null) throw new ArgumentNullException(nameof(eventSubscription));

            eventSubscription.SubscriptionToken = new SubscriptionToken(Unsubscribe);

            lock (Subscriptions)
            {
                Subscriptions.Add(eventSubscription);
            }
            return eventSubscription.SubscriptionToken;
        }
        public void AddSubscription(IEventSubscription sub)
        {
            // if the subscription is a wiretap, add it to a different list
            if (sub.IsWiretap()) { _wiretaps.Add(sub); return; }

            lock (_subscriptions)
            {
                if (false == _subscriptions.ContainsKey(sub.Topic))
                {
                    _subscriptions.Add(sub.Topic, new List<IEventSubscription>());
                }

                _subscriptions[sub.Topic].Add(sub);
            }
        }
        public void AddSubscription(IEventSubscription subscription)
        {
            List<IExchange> exchanges = new List<IExchange>();

            if (subscription.IsWiretap())
            {
                exchanges.AddRange(_exchangeLocator.GetAllExchanges());
            }
            else
            {
                // find out which exchange we need to bind to
                exchanges.Add(_exchangeLocator.GetExchange(subscription.Topic));
            }

            exchanges.ForEach(ex =>
            {
                // now, get or declare the queue for this exchange
                IQueue queue = this.GetQueue(ex);

                // create a binding
                AmqpBinding binding = new AmqpBinding(ex, subscription.Topic);

                // add the binding to the queue if it doesn't already exist
                if (false == queue.ContainsBinding(binding))
                {
                    queue.AddBinding(binding);
                }

                // ensure an entry for this binding exists
                if (false == _bindingCount.ContainsKey(binding))
                {
                    _bindingCount.Add(binding, 0);
                }

                // finally, increment the count of subscriptions for this binding
                _bindingCount[binding]++;
            });
        }
Exemple #7
0
 public async Task OnEventAsync(IEventSubscription subscription, StoredEvent storedEvent)
 {
     await eventSubscriber.OnEventAsync(subscription, storedEvent);
 }
 void HandleOnJoin(IEventSubscription sub)
 {
     NotifyChannels(sub.Channels, "cmd.onJoin", sub.Meta);
 }
 void HandleOnHeartbeat(IEventSubscription sub)
 {
     NotifyChannel(sub.Channel, "cmd.onHeartbeat", sub.Meta);
 }
Exemple #10
0
 public Task OnErrorAsync(IEventSubscription subscription, Exception exception)
 {
     throw exception;
 }
Exemple #11
0
 Task IEventSubscriber.OnEventAsync(IEventSubscription subscription, StoredEvent storedEvent)
 {
     return(dispatcher.DispatchAsync(() => HandleEventAsync(subscription, storedEvent)));
 }
 public Task OnEventAsync(IEventSubscription subscription, StoredEvent storedEvent)
 {
     return(currentSubscriber !.OnEventAsync(subscription, storedEvent));
 }
Exemple #13
0
 private void Unsubscribe()
 {
     currentSubscription?.StopAsync().Forget();
     currentSubscription = null;
 }
Exemple #14
0
 void HandleOnJoin(IEventSubscription sub)
 {
     NotifyChannels(sub.Channels, "cmd.onJoin", sub.Meta);
 }
Exemple #15
0
 private Task OnEventAsync(IEventSubscription subscriber, StoredEvent ev)
 {
     return(sutSubscriber.OnEventAsync(subscriber, ev));
 }
Exemple #16
0
        public BatchSubscriber(
            EventConsumerGrain grain,
            IEventDataFormatter eventDataFormatter,
            IEventConsumer eventConsumer,
            Func <IEventSubscriber, IEventSubscription> factory,
            TaskScheduler scheduler)
        {
            var batchSize  = Math.Max(1, eventConsumer !.BatchSize);
            var batchDelay = Math.Max(100, eventConsumer.BatchDelay);

            var parse = new TransformBlock <Job, Job>(job =>
            {
                if (job.StoredEvent != null)
                {
                    job.ShouldHandle = eventConsumer.Handles(job.StoredEvent);
                }

                if (job.ShouldHandle)
                {
                    try
                    {
                        job.Event = eventDataFormatter.ParseIfKnown(job.StoredEvent !);
                    }
                    catch (Exception ex)
                    {
                        job.Exception = ex;
                    }
                }

                return(job);
            }, new ExecutionDataflowBlockOptions
            {
                BoundedCapacity        = batchSize,
                MaxDegreeOfParallelism = 1,
                MaxMessagesPerTask     = 1
            });

            var buffer = AsyncHelper.CreateBatchBlock <Job>(batchSize, batchDelay, new GroupingDataflowBlockOptions
            {
                BoundedCapacity = batchSize * 2
            });

            var handle = new ActionBlock <IList <Job> >(async jobs =>
            {
                var sender = eventSubscription?.Sender;

                foreach (var jobsBySender in jobs.GroupBy(x => x.Sender))
                {
                    if (sender != null && ReferenceEquals(jobsBySender.Key, sender))
                    {
                        var exception = jobs.FirstOrDefault(x => x.Exception != null)?.Exception;

                        if (exception != null)
                        {
                            await grain.OnErrorAsync(sender, exception);
                        }
                        else
                        {
                            await grain.OnEventsAsync(sender, GetEvents(jobsBySender), GetPosition(jobsBySender));
                        }
                    }
                }
            },
                                                        new ExecutionDataflowBlockOptions
            {
                BoundedCapacity        = 2,
                MaxDegreeOfParallelism = 1,
                MaxMessagesPerTask     = 1,
                TaskScheduler          = scheduler
            });

            parse.LinkTo(buffer, new DataflowLinkOptions
            {
                PropagateCompletion = true
            });

            buffer.LinkTo(handle, new DataflowLinkOptions
            {
                PropagateCompletion = true
            });

            pipelineStart = parse;
            pipelineEnd   = handle;

            eventSubscription = factory(this);
        }
Exemple #17
0
 private Task OnErrorAsync(IEventSubscription subscriber, Exception ex)
 {
     return(sutSubscriber.OnErrorAsync(subscriber, ex));
 }
Exemple #18
0
 public SubscriptionToken Subscribe(IEventSubscription subscription)
 {
     return(base.InternalSubscribe(subscription));
 }
 protected EventHandlerBase(IEventSubscription subscription, long id)
 {
     Subscription = subscription;
     Id           = id;
 }
 void HandleOnHeartbeat(IEventSubscription sub)
 {
     NotifySubscription(sub.SubscriptionId, "cmd.onHeartbeat", sub.Meta);
 }
Exemple #21
0
 public new SubscriptionToken Subscribe(IEventSubscription eventSubscription)
 {
     return(base.Subscribe(eventSubscription));
 }
 public Task OnErrorAsync(IEventSubscription subscription, Exception exception)
 {
     throw new NotSupportedException();
 }
Exemple #23
0
 public static bool HasChannel(this IEventSubscription sub, string channel)
 {
     return(sub != null && (channel == null || Array.IndexOf(sub.Channels, channel) >= 0));
 }
Exemple #24
0
 void HandleOnHeartbeat(IEventSubscription sub)
 {
     NotifySubscription(sub.SubscriptionId, "cmd.onHeartbeat", sub.Meta);
 }
 public GetTransactionEventHandler(IEventSubscription subscription, long id, IEthGetTransactionByHash proxy)
     : base(subscription, id)
 {
     GetTransactionProxy = proxy;
 }
Exemple #26
0
 Task IEventSubscriber.OnErrorAsync(IEventSubscription subscription, Exception exception)
 {
     return(dispatcher.DispatchAsync(() => HandleErrorAsync(subscription, exception)));
 }
Exemple #27
0
 public EventRegistration(Type handlerType, IEventSubscription eventSubscription)
 {
     HandlerType       = handlerType;
     EventSubscription = eventSubscription;
 }
 public Task OnErrorAsync(IEventSubscription subscription, Exception exception)
 {
     return(currentSubscriber !.OnErrorAsync(subscription, exception));
 }
        public void RemoveSubscription(IEventSubscription subscription)
        {
            // the exchange to which this subscription refers
            IExchange exchange = _exchangeLocator.GetExchange(subscription.Topic);

            // get the queue that is bound to the exchange
            IQueue queue = this.GetQueue(exchange);

            // create a binding
            AmqpBinding binding = new AmqpBinding(exchange, subscription.Topic);

            // don't assume that a binding exists
            if (_bindingCount.ContainsKey(binding))
            {
                // decrement the count of bindings
                _bindingCount[binding]--;

                if (0 == _bindingCount[binding])
                {
                    // since there are no more subscriptions for this binding,
                    // we should actually remove the binding from the queue.
                    queue.RemoveBinding(binding);
                }
            }
        }
        public void RemoveSubscription(IEventSubscription sub)
        {
            // if the subscription is a wiretap, remove it from a different list
            if (sub.IsWiretap()) { _wiretaps.Remove(sub); return; }

            lock (_subscriptions)
            {
                if (_subscriptions.ContainsKey(sub.Topic))
                {
                    if (_subscriptions[sub.Topic].Contains(sub))
                    {
                        _subscriptions[sub.Topic].Remove(sub);
                    }
                }
            }
        }
 private Task OnErrorAsync(IEventSubscription subscription, Exception exception)
 {
     return(sut.OnErrorAsync(subscription, exception));
 }
Exemple #32
0
 public void when()
 {
     subscription = EventBus.Subscribe<EventMessage>(new SimpleSubscriber());
 }
Exemple #33
0
 private void SetStateValues(IEventSubscription subscription, DecodedEvent decodedEvent)
 {
     subscription.State.Increment("EventsHandled");
     decodedEvent.State["SubscriberId"]        = subscription.SubscriberId;
     decodedEvent.State["EventSubscriptionId"] = subscription.Id;
 }
 void HandleOnUpdate(IEventSubscription sub)
 {
     using (var redis = clientsManager.GetClient())
     {
         StoreSubscriptionInfo(redis, sub.GetInfo());
     }
     NotifyChannels(sub.Channels, "cmd.onUpdate", sub.Meta);
 }
Exemple #35
0
 public void given()
 {
     subscription = EventBus.Subscribe<EventMessage>(_ => notified = true);
 }
Exemple #36
0
 public new SubscriptionToken Subscribe(IEventSubscription eventSubscription)
 {
     return base.Subscribe(eventSubscription);
 }
Exemple #37
0
 public void given()
 {
     subscription = EventBus.Subscribe<EventMessage>(subscriber);
 }
Exemple #38
0
 public void when()
 {
     subscription = EventBus.Subscribe<EventMessage>(_ => { });
 }
Exemple #39
0
        /// <summary>
        /// Unsubscribes from a specific event.
        /// </summary>
        /// <param name="domainRelationshipInfo">DomainRelationshipInfo specifying from what to unsubscribe.</param>
        /// <param name="parentElementId">Parent element Id specifying from what to unsubscribe.</param>
        /// <param name="action">Action identifying what to unsubscribe.</param>
        public void Unsubscribe(DomainRelationshipInfo domainRelationshipInfo, bool bSourceRole, Guid parentElementId, Action <ElementAddedEventArgs> action)
        {
            #region dictionarySource
            if (bSourceRole)
            {
                lock (dictionarySource)
                {
                    Guid domainClassId = domainRelationshipInfo.Id;
                    if (dictionarySource.Keys.Contains(domainClassId))
                    {
                        if (dictionarySource[domainClassId].Keys.Contains(parentElementId))
                        {
                            IEventSubscription eventSubscription = dictionarySource[domainClassId][parentElementId].Cast <EventSubscription <ElementAddedEventArgs> >().FirstOrDefault(evt => evt.Action == action);
                            if (eventSubscription != null)
                            {
                                dictionarySource[domainClassId][parentElementId].Remove(eventSubscription);
                                if (dictionarySource[domainClassId][parentElementId].Count == 0)
                                {
                                    dictionarySource[domainClassId].Remove(parentElementId);
                                }
                            }
                            if (dictionarySource[domainClassId].Count == 0)
                            {
                                dictionarySource.Remove(domainClassId);
                            }
                        }
                    }

                    // process descendants
                    foreach (DomainRelationshipInfo info in domainRelationshipInfo.AllDescendants)
                    {
                        domainClassId = info.Id;
                        if (dictionarySource.Keys.Contains(domainClassId))
                        {
                            if (dictionarySource[domainClassId].Keys.Contains(parentElementId))
                            {
                                IEventSubscription eventSubscription = dictionarySource[domainClassId][parentElementId].Cast <EventSubscription <ElementAddedEventArgs> >().FirstOrDefault(evt => evt.Action == action);
                                if (eventSubscription != null)
                                {
                                    dictionarySource[domainClassId][parentElementId].Remove(eventSubscription);
                                    if (dictionarySource[domainClassId][parentElementId].Count == 0)
                                    {
                                        dictionarySource[domainClassId].Remove(parentElementId);
                                    }
                                }
                                if (dictionarySource[domainClassId].Count == 0)
                                {
                                    dictionarySource.Remove(domainClassId);
                                }
                            }
                        }
                    }
                }
            }
            #endregion

            #region dictionaryTarget
            if (!bSourceRole)
            {
                lock (dictionaryTarget)
                {
                    Guid domainClassId = domainRelationshipInfo.Id;
                    if (dictionaryTarget.Keys.Contains(domainClassId))
                    {
                        if (dictionaryTarget[domainClassId].Keys.Contains(parentElementId))
                        {
                            IEventSubscription eventSubscription = dictionaryTarget[domainClassId][parentElementId].Cast <EventSubscription <ElementAddedEventArgs> >().FirstOrDefault(evt => evt.Action == action);
                            if (eventSubscription != null)
                            {
                                dictionaryTarget[domainClassId][parentElementId].Remove(eventSubscription);
                                if (dictionaryTarget[domainClassId][parentElementId].Count == 0)
                                {
                                    dictionaryTarget[domainClassId].Remove(parentElementId);
                                }
                            }
                            if (dictionaryTarget[domainClassId].Count == 0)
                            {
                                dictionaryTarget.Remove(domainClassId);
                            }
                        }
                    }

                    // process descendants
                    foreach (DomainRelationshipInfo info in domainRelationshipInfo.AllDescendants)
                    {
                        domainClassId = info.Id;
                        if (dictionaryTarget.Keys.Contains(domainClassId))
                        {
                            if (dictionaryTarget[domainClassId].Keys.Contains(parentElementId))
                            {
                                IEventSubscription eventSubscription = dictionaryTarget[domainClassId][parentElementId].Cast <EventSubscription <ElementAddedEventArgs> >().FirstOrDefault(evt => evt.Action == action);
                                if (eventSubscription != null)
                                {
                                    dictionaryTarget[domainClassId][parentElementId].Remove(eventSubscription);
                                    if (dictionaryTarget[domainClassId][parentElementId].Count == 0)
                                    {
                                        dictionaryTarget[domainClassId].Remove(parentElementId);
                                    }
                                }
                                if (dictionaryTarget[domainClassId].Count == 0)
                                {
                                    dictionaryTarget.Remove(domainClassId);
                                }
                            }
                        }
                    }
                }
            }
            #endregion
        }
Exemple #40
0
 public SubscriptionToken Subscribe(IEventSubscription subscription)
 {
     return base.InternalSubscribe(subscription);
 }
Exemple #41
0
 private Task OnErrorAsync(IEventSubscription subscriber, Exception ex)
 {
     return(sut.OnErrorAsync(subscriber.AsImmutable(), ex.AsImmutable()));
 }
        void HandleOnLeave(IEventSubscription sub)
        {
            var info = sub.GetInfo();
            RemoveSubscriptionFromRedis(info);

            NotifyChannels(sub.Channels, "cmd.onLeave", sub.Meta);
        }
Exemple #43
0
 private Task OnEventAsync(IEventSubscription subscriber, StoredEvent ev)
 {
     return(sut.OnEventAsync(subscriber.AsImmutable(), ev.AsImmutable()));
 }
        public void Register(IEventSubscription sub, Dictionary<string, string> connectArgs = null)
        {
            if (sub == null)
                throw new ArgumentNullException("subscription");

            var info = sub.GetInfo();
            using (var redis = clientsManager.GetClient())
            {
                StoreSubscriptionInfo(redis, info);
            }

            if (connectArgs != null)
                sub.Publish("cmd.onConnect", connectArgs.ToJson());

            local.Register(sub);
        }
Exemple #45
0
        private static async Task WaitAndStopAsync(IEventSubscription sut)
        {
            await Task.Delay(200);

            await sut.StopAsync();
        }