コード例 #1
0
        public MessageDistributor(
            NsqBus bus,
            IObjectBuilder objectBuilder,
            ILogger logger,
            MessageHandlerMetadata messageHandlerMetadata
        )
        {
            if (bus == null)
                throw new ArgumentNullException("bus");
            if (objectBuilder == null)
                throw new ArgumentNullException("objectBuilder");
            if (logger == null)
                throw new ArgumentNullException("logger");
            if (messageHandlerMetadata == null)
                throw new ArgumentNullException("messageHandlerMetadata");

            _bus = bus;
            _objectBuilder = objectBuilder;
            _logger = logger;

            _serializer = messageHandlerMetadata.Serializer;
            _handlerType = messageHandlerMetadata.HandlerType;
            _messageType = messageHandlerMetadata.MessageType;
            _messageAuditor = messageHandlerMetadata.MessageAuditor;
            _topic = messageHandlerMetadata.Topic;
            _channel = messageHandlerMetadata.Channel;

            var possibleMethods = _handlerType.GetMethods().Where(p => p.Name == "Handle" && !p.IsGenericMethod);
            foreach (var possibleMethod in possibleMethods)
            {
                var parameters = possibleMethod.GetParameters();
                if (parameters.Length == 1 && parameters[0].ParameterType == _messageType)
                {
                    _handleMethod = possibleMethod;
                    break;
                }
            }

            if (_handleMethod == null)
                throw new Exception(string.Format("Handle({0}) not found on {1}", _messageType, _handlerType));

            if (!_messageType.IsInterface)
            {
                _concreteMessageType = _messageType;
            }
            else
            {
                _concreteMessageType = InterfaceBuilder.CreateType(_messageType);
            }
        }
コード例 #2
0
ファイル: BusConfiguration.cs プロジェクト: bcowcher/NsqSharp
        private void AddMessageHandler(Type handlerType, Type messageType, string topic, string channel,
                                       IMessageSerializer messageSerializer = null,
                                       Config config         = null,
                                       int?threadsPerHandler = null,
                                       params string[] nsqLookupdHttpAddresses
                                       )
        {
            if (_bus != null)
            {
                throw new Exception("Handlers can only be added before the bus is started");
            }

            if (!Protocol.IsValidTopicName(topic))
            {
                throw new ArgumentException(string.Format("'{0}' is not a valid topic name", topic), "topic");
            }
            if (!Protocol.IsValidChannelName(channel))
            {
                throw new ArgumentException(string.Format("'{0}' is not a valid channel name", topic), "channel");
            }

            if (handlerType.IsAbstract && !handlerType.IsInterface)
            {
                throw new ArgumentException("handlerType must be instantiable; cannot be an abstract class", "handlerType");
            }

            if (nsqLookupdHttpAddresses == null || nsqLookupdHttpAddresses.Length == 0)
            {
                nsqLookupdHttpAddresses = _defaultNsqlookupdHttpEndpoints;
            }

            string key = string.Format("{0}/{1}", topic, channel);

            List <MessageHandlerMetadata> list;

            if (!_topicChannelHandlers.TryGetValue(key, out list))
            {
                list = new List <MessageHandlerMetadata>();
                _topicChannelHandlers.Add(key, list);
            }

            // remove duplicates based on topic/channel/handlerType; replace with new values
            foreach (var item in new List <MessageHandlerMetadata>(list))
            {
                if (item.HandlerType == handlerType)
                {
                    list.Remove(item);
                }
            }

            // add topic/channel handler
            var messageHandlerMetadata = new MessageHandlerMetadata
            {
                Topic                   = topic,
                Channel                 = channel,
                HandlerType             = handlerType,
                MessageType             = messageType,
                NsqLookupdHttpAddresses = nsqLookupdHttpAddresses,
                Serializer              = messageSerializer ?? _defaultMessageSerializer,
                MessageAuditor          = _messageAuditor,
                Config                  = config ?? _nsqConfig,
                InstanceCount           = threadsPerHandler ?? _defaultThreadsPerHandler
            };

            list.Add(messageHandlerMetadata);
        }