예제 #1
0
        private IObjectDefinition ParseListener(XmlElement childElement, XmlElement element, ParserContext parserContext)
        {
            var replyContainer = RabbitNamespaceUtils.ParseContainer(childElement, parserContext);

            if (replyContainer != null)
            {
                replyContainer.PropertyValues.Add("ConnectionFactory", new RuntimeObjectReference(element.GetAttribute(CONNECTION_FACTORY_ATTRIBUTE)));
                replyContainer.PropertyValues.Add("Queues", new RuntimeObjectReference(element.GetAttribute(REPLY_QUEUE_ATTRIBUTE)));
            }

            return(replyContainer);
        }
        private void ParseListener(XmlElement listenerEle, XmlElement containerEle, ParserContext parserContext)
        {
            var listenerDef = new RootObjectDefinition();

            // listenerDef.setSource(parserContext.extractSource(listenerEle));
            var aRef = listenerEle.GetAttribute(REF_ATTRIBUTE);

            if (string.IsNullOrWhiteSpace(aRef))
            {
                parserContext.ReaderContext.ReportFatalException(listenerEle, "Listener 'ref' attribute contains empty value.");
            }
            else
            {
                listenerDef.PropertyValues.Add("HandlerObject", new RuntimeObjectReference(aRef));
            }

            string method = null;

            if (listenerEle.HasAttribute(METHOD_ATTRIBUTE))
            {
                method = listenerEle.GetAttribute(METHOD_ATTRIBUTE);
                if (string.IsNullOrWhiteSpace(method))
                {
                    parserContext.ReaderContext.ReportFatalException(listenerEle, "Listener 'method' attribute contains empty value.");
                }
            }

            listenerDef.PropertyValues.Add("DefaultListenerMethod", method);

            if (containerEle.HasAttribute(MESSAGE_CONVERTER_ATTRIBUTE))
            {
                var messageConverter = containerEle.GetAttribute(MESSAGE_CONVERTER_ATTRIBUTE);
                if (string.IsNullOrWhiteSpace(messageConverter))
                {
                    parserContext.ReaderContext.ReportFatalException(containerEle, "Listener container 'message-converter' attribute contains empty value.");
                }
                else
                {
                    listenerDef.PropertyValues.Add("MessageConverter", new RuntimeObjectReference(messageConverter));
                }
            }

            var containerDef = RabbitNamespaceUtils.ParseContainer(containerEle, parserContext);

            if (listenerEle.HasAttribute(RESPONSE_EXCHANGE_ATTRIBUTE))
            {
                var responseExchange = listenerEle.GetAttribute(RESPONSE_EXCHANGE_ATTRIBUTE);
                listenerDef.PropertyValues.Add("ResponseExchange", responseExchange);
            }

            if (listenerEle.HasAttribute(RESPONSE_ROUTING_KEY_ATTRIBUTE))
            {
                var responseRoutingKey = listenerEle.GetAttribute(RESPONSE_ROUTING_KEY_ATTRIBUTE);
                listenerDef.PropertyValues.Add("ResponseRoutingKey", responseRoutingKey);
            }

            listenerDef.ObjectTypeName = "Spring.Messaging.Amqp.Rabbit.Listener.Adapter.MessageListenerAdapter";
            containerDef.PropertyValues.Add("MessageListener", listenerDef);

            var containerObjectName = containerEle.GetAttribute(ID_ATTRIBUTE);

            // If no object id is given auto generate one using the ReaderContext's ObjectNameGenerator
            if (string.IsNullOrWhiteSpace(containerObjectName))
            {
                containerObjectName = parserContext.ReaderContext.GenerateObjectName(containerDef);
            }

            if (!NamespaceUtils.IsAttributeDefined(listenerEle, QUEUE_NAMES_ATTRIBUTE) && !NamespaceUtils.IsAttributeDefined(listenerEle, QUEUES_ATTRIBUTE))
            {
                parserContext.ReaderContext.ReportFatalException(listenerEle, "Listener 'queue-names' or 'queues' attribute must be provided.");
            }

            if (NamespaceUtils.IsAttributeDefined(listenerEle, QUEUE_NAMES_ATTRIBUTE) && NamespaceUtils.IsAttributeDefined(listenerEle, QUEUES_ATTRIBUTE))
            {
                parserContext.ReaderContext.ReportFatalException(listenerEle, "Listener 'queue-names' or 'queues' attribute must be provided but not both.");
            }

            var queueNames = listenerEle.GetAttribute(QUEUE_NAMES_ATTRIBUTE);

            if (!string.IsNullOrWhiteSpace(queueNames))
            {
                var names  = StringUtils.CommaDelimitedListToStringArray(queueNames);
                var values = new ManagedList();
                foreach (var name in names)
                {
                    values.Add(new TypedStringValue(name.Trim()));
                }

                containerDef.PropertyValues.Add("QueueNames", values);
            }

            var queues = listenerEle.GetAttribute(QUEUES_ATTRIBUTE);

            if (!string.IsNullOrWhiteSpace(queues))
            {
                var names  = StringUtils.CommaDelimitedListToStringArray(queues);
                var values = new ManagedList();
                foreach (var name in names)
                {
                    values.Add(new RuntimeObjectReference(name.Trim()));
                }

                containerDef.PropertyValues.Add("Queues", values);
            }

            // Register the listener and fire event
            parserContext.Registry.RegisterObjectDefinition(containerObjectName, containerDef);
        }