Beispiel #1
0
        public void PublishEvent <TEvent>(TEvent @event) where TEvent : class, IEvent
        {
            Ensure.That(@event, () => @event).IsNotNull();

            var eventName = GetEventName(@event.GetType());

            /*
             *          int workerThreads;
             *          int completionPortThreads;
             *          ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
             *
             *          int maxCompletionPortThreads;
             *          int maxWorkerThreads;
             *          ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxCompletionPortThreads);
             *
             *
             *          int minCompletionPortThreads;
             *          int minWorkerThreads;
             *          ThreadPool.GetMinThreads(out minWorkerThreads, out minCompletionPortThreads);
             *
             *          _logger.Warn("Thread pool state WT:{0} PT:{1}  MAXWT:{2} MAXPT:{3} MINWT:{4} MINPT:{5}", workerThreads, completionPortThreads, maxWorkerThreads, maxCompletionPortThreads, minWorkerThreads, minCompletionPortThreads);
             */

            _logger.Trace("Publishing {0}", eventName);


            //call synchronous handlers first.
            foreach (var handler in _serviceFactory.BuildAll <IHandle <TEvent> >())
            {
                try
                {
                    _logger.Trace("{0} -> {1}", eventName, handler.GetType().Name);
                    handler.Handle(@event);
                    _logger.Trace("{0} <- {1}", eventName, handler.GetType().Name);
                }
                catch (Exception e)
                {
                    _logger.ErrorException(string.Format("{0} failed while processing [{1}]", handler.GetType().Name, eventName), e);
                }
            }

            foreach (var handler in _serviceFactory.BuildAll <IHandleAsync <TEvent> >())
            {
                var handlerLocal = handler;

                _taskFactory.StartNew(() =>
                {
                    _logger.Trace("{0} ~> {1}", eventName, handlerLocal.GetType().Name);
                    handlerLocal.HandleAsync(@event);
                    _logger.Trace("{0} <~ {1}", eventName, handlerLocal.GetType().Name);
                }, TaskCreationOptions.PreferFairness)
                .LogExceptions();
            }
        }
Beispiel #2
0
            public EventSubscribers(IServiceFactory serviceFactory)
            {
                _syncHandlers = serviceFactory.BuildAll <IHandle <TEvent> >()
                                .OrderBy(GetEventHandleOrder)
                                .ToArray();

                _globalHandlers = serviceFactory.BuildAll <IHandleAsync <IEvent> >()
                                  .ToArray();

                _asyncHandlers = serviceFactory.BuildAll <IHandleAsync <TEvent> >()
                                 .ToArray();
            }
        public void PublishEvent <TEvent>(TEvent @event) where TEvent : class, IEvent
        {
            Ensure.That(@event, () => @event).IsNotNull();

            var eventName = GetEventName(@event.GetType());

            _logger.Trace("Publishing {0}", eventName);

            // Call synchronous handlers.
            foreach (var handler in _serviceFactory.BuildAll <IHandle <TEvent> >())
            {
                try
                {
                    _logger.Trace("{0} -> {1}", eventName, handler.GetType().Name);

                    handler.Handle(@event);

                    _logger.Trace("{0} <- {1}", eventName, handler.GetType().Name);
                }
                catch (Exception e)
                {
                    _logger.Error(e, string.Format("{0} failed while processing [{1}]", handler.GetType().Name, eventName));
                }
            }

            // Call asynchronous handlers.
            foreach (var handler in _serviceFactory.BuildAll <IHandleAsync <TEvent> >())
            {
                var handlerLocal = handler;

                _taskFactory.StartNew(() => {
                    _logger.Trace("{0} ~> {1}", eventName, handlerLocal.GetType().Name);

                    handlerLocal.HandleAsync(@event);

                    _logger.Trace("{0} <~ {1}", eventName, handlerLocal.GetType().Name);
                }, TaskCreationOptions.PreferFairness).LogExceptions();
            }
        }