001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.core.xml;
018    
019    import java.util.concurrent.ExecutorService;
020    import java.util.concurrent.RejectedExecutionHandler;
021    import java.util.concurrent.TimeUnit;
022    import javax.xml.bind.annotation.XmlAccessType;
023    import javax.xml.bind.annotation.XmlAccessorType;
024    import javax.xml.bind.annotation.XmlAttribute;
025    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
026    
027    import org.apache.camel.CamelContext;
028    import org.apache.camel.ThreadPoolRejectedPolicy;
029    import org.apache.camel.builder.xml.TimeUnitAdapter;
030    import org.apache.camel.util.CamelContextHelper;
031    
032    /**
033     * A factory which instantiates {@link java.util.concurrent.ExecutorService} objects
034     *
035     * @version 
036     */
037    @XmlAccessorType(XmlAccessType.FIELD)
038    public abstract class AbstractCamelThreadPoolFactoryBean extends AbstractCamelFactoryBean<ExecutorService> {
039    
040        @XmlAttribute(required = true)
041        private String poolSize;
042        @XmlAttribute
043        private String maxPoolSize;
044        @XmlAttribute
045        private String keepAliveTime;
046        @XmlAttribute
047        @XmlJavaTypeAdapter(TimeUnitAdapter.class)
048        private TimeUnit timeUnit = TimeUnit.SECONDS;
049        @XmlAttribute
050        private String maxQueueSize;
051        @XmlAttribute
052        private ThreadPoolRejectedPolicy rejectedPolicy = ThreadPoolRejectedPolicy.CallerRuns;
053        @XmlAttribute(required = true)
054        private String threadName;
055    
056        public ExecutorService getObject() throws Exception {
057            int size = CamelContextHelper.parseInteger(getCamelContext(), poolSize);
058            if (size <= 0) {
059                throw new IllegalArgumentException("PoolSize must be a positive number");
060            }
061    
062            int max = size;
063            if (maxPoolSize != null) {
064                max = CamelContextHelper.parseInteger(getCamelContext(), maxPoolSize);
065            }
066    
067            RejectedExecutionHandler rejected = null;
068            if (rejectedPolicy != null) {
069                rejected = rejectedPolicy.asRejectedExecutionHandler();
070            }
071    
072            long keepAlive = 60;
073            if (keepAliveTime != null) {
074                keepAlive = CamelContextHelper.parseLong(getCamelContext(), keepAliveTime);
075            }
076    
077            int queueSize = -1;
078            if (maxQueueSize != null) {
079                queueSize = CamelContextHelper.parseInteger(getCamelContext(), keepAliveTime);
080            }
081    
082            ExecutorService answer = getCamelContext().getExecutorServiceStrategy().newThreadPool(getId(), getThreadName(),
083                    size, max, keepAlive, getTimeUnit(), queueSize, rejected, true);
084            return answer;
085        }
086    
087        protected abstract CamelContext getCamelContextWithId(String camelContextId);
088    
089        public Class<ExecutorService> getObjectType() {
090            return ExecutorService.class;
091        }
092    
093        public String getPoolSize() {
094            return poolSize;
095        }
096    
097        public void setPoolSize(String poolSize) {
098            this.poolSize = poolSize;
099        }
100    
101        public String getMaxPoolSize() {
102            return maxPoolSize;
103        }
104    
105        public void setMaxPoolSize(String maxPoolSize) {
106            this.maxPoolSize = maxPoolSize;
107        }
108    
109        public String getKeepAliveTime() {
110            return keepAliveTime;
111        }
112    
113        public void setKeepAliveTime(String keepAliveTime) {
114            this.keepAliveTime = keepAliveTime;
115        }
116    
117        public TimeUnit getTimeUnit() {
118            return timeUnit;
119        }
120    
121        public void setTimeUnit(TimeUnit timeUnit) {
122            this.timeUnit = timeUnit;
123        }
124    
125        public String getMaxQueueSize() {
126            return maxQueueSize;
127        }
128    
129        public void setMaxQueueSize(String maxQueueSize) {
130            this.maxQueueSize = maxQueueSize;
131        }
132    
133        public ThreadPoolRejectedPolicy getRejectedPolicy() {
134            return rejectedPolicy;
135        }
136    
137        public void setRejectedPolicy(ThreadPoolRejectedPolicy rejectedPolicy) {
138            this.rejectedPolicy = rejectedPolicy;
139        }
140    
141        public String getThreadName() {
142            return threadName;
143        }
144    
145        public void setThreadName(String threadName) {
146            this.threadName = threadName;
147        }
148    
149    
150    }