Example #1
0
        public void AddMessageHandler <T>(Func <IHandler <T> > futureHandler) where T : Message
        {
            List <Func <Message, bool> > handlers;

            if (!_handlers.TryGetValue(typeof(T), out handlers))
            {
                handlers = new List <Func <Message, bool> >();
                _handlers.Add(typeof(T), handlers);
            }
            var handlerInstance    = futureHandler();
            var guaranteedDelivery = new GuaranteedOnceDelivery <T>(handlerInstance);

            IHandler <T> handler = new FutureHandler <T>(futureHandler);

            if (guaranteedDelivery.Enabled)
            {
                if (_messageLock == null)
                {
                    throw new Exception("IMessageLock is null. You need to specify an implementation for IMessageLock.");
                }

                handler = new ExactlyOnceHandler <T>(handler, _messageLock, guaranteedDelivery.TimeOut, handlerInstance.GetType().FullName.ToLower());
            }
            var executionTimeMonitoring = _messagingMonitor as IMeasureHandlerExecutionTime;

            if (executionTimeMonitoring != null)
            {
                handler = new StopwatchHandler <T>(handler, executionTimeMonitoring);
            }

            Subscribers.Add(new Subscriber(typeof(T)));
            handlers.Add(message => handler.Handle((T)message));
        }
        public void AddMessageHandler <T>(IHandler <T> handler) where T : Message
        {
            List <Func <Message, bool> > handlers;

            if (!_handlers.TryGetValue(typeof(T), out handlers))
            {
                handlers = new List <Func <Message, bool> >();
                _handlers.Add(typeof(T), handlers);
            }
            var guaranteedDelivery = new GuaranteedOnceDelivery <T>(handler);

            if (guaranteedDelivery.Enabled)
            {
                if (_messageLock == null)
                {
                    throw new Exception("IMessageLock is null. You need to specify an implementation for IMessageLock.");
                }

                handler = new ExactlyOnceHandler <T>(handler, _messageLock, guaranteedDelivery.TimeOut);
            }
            var executionTimeMonitoring = _messagingMonitor as IMeasureHandlerExecutionTime;

            if (executionTimeMonitoring != null)
            {
                handler = new StopwatchHandler <T>(handler, executionTimeMonitoring);
            }

            handlers.Add(DelegateAdjuster.CastArgument <Message, T>(x => handler.Handle(x)));
        }