001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.scheduling.config;
018
019import org.w3c.dom.Element;
020
021import org.springframework.beans.factory.support.BeanDefinitionBuilder;
022import org.springframework.beans.factory.support.RootBeanDefinition;
023import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
024import org.springframework.beans.factory.xml.ParserContext;
025import org.springframework.util.StringUtils;
026
027/**
028 * Parser for the 'executor' element of the 'task' namespace.
029 *
030 * @author Mark Fisher
031 * @author Juergen Hoeller
032 * @since 3.0
033 */
034public class ExecutorBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
035
036        @Override
037        protected String getBeanClassName(Element element) {
038                return "org.springframework.scheduling.config.TaskExecutorFactoryBean";
039        }
040
041        @Override
042        protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
043                String keepAliveSeconds = element.getAttribute("keep-alive");
044                if (StringUtils.hasText(keepAliveSeconds)) {
045                        builder.addPropertyValue("keepAliveSeconds", keepAliveSeconds);
046                }
047                String queueCapacity = element.getAttribute("queue-capacity");
048                if (StringUtils.hasText(queueCapacity)) {
049                        builder.addPropertyValue("queueCapacity", queueCapacity);
050                }
051                configureRejectionPolicy(element, builder);
052                String poolSize = element.getAttribute("pool-size");
053                if (StringUtils.hasText(poolSize)) {
054                        builder.addPropertyValue("poolSize", poolSize);
055                }
056        }
057
058        private void configureRejectionPolicy(Element element, BeanDefinitionBuilder builder) {
059                String rejectionPolicy = element.getAttribute("rejection-policy");
060                if (!StringUtils.hasText(rejectionPolicy)) {
061                        return;
062                }
063                String prefix = "java.util.concurrent.ThreadPoolExecutor.";
064                String policyClassName;
065                if (rejectionPolicy.equals("ABORT")) {
066                        policyClassName = prefix + "AbortPolicy";
067                }
068                else if (rejectionPolicy.equals("CALLER_RUNS")) {
069                        policyClassName = prefix + "CallerRunsPolicy";
070                }
071                else if (rejectionPolicy.equals("DISCARD")) {
072
073                }
074                else if (rejectionPolicy.equals("DISCARD_OLDEST")) {
075                        policyClassName = prefix + "DiscardOldestPolicy";
076                }
077                else {
078                        policyClassName = rejectionPolicy;
079                }
080                builder.addPropertyValue("rejectedExecutionHandler", new RootBeanDefinition(policyClassName));
081        }
082
083}