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.XmlRootElement;
026    import javax.xml.bind.annotation.XmlTransient;
027    import javax.xml.bind.annotation.XmlType;
028    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
029    
030    import org.apache.camel.CamelContext;
031    import org.apache.camel.CamelContextAware;
032    import org.apache.camel.ThreadPoolRejectedPolicy;
033    import org.apache.camel.builder.xml.TimeUnitAdapter;
034    import org.apache.camel.model.IdentifiedType;
035    
036    import static org.apache.camel.util.ObjectHelper.notNull;
037    
038    /**
039     * A factory which instantiates {@link java.util.concurrent.ExecutorService} objects
040     *
041     * @version $Revision: 925208 $
042     */
043    @XmlAccessorType(XmlAccessType.FIELD)
044    public abstract class AbstractCamelThreadPoolFactoryBean extends IdentifiedType implements CamelContextAware {
045    
046        @XmlAttribute(required = true)
047        private Integer poolSize;
048        @XmlAttribute
049        private Integer maxPoolSize;
050        @XmlAttribute
051        private Long keepAliveTime = 60L;
052        @XmlAttribute
053        @XmlJavaTypeAdapter(TimeUnitAdapter.class)
054        private TimeUnit timeUnit = TimeUnit.SECONDS;
055        @XmlAttribute
056        private Integer maxQueueSize = -1;
057        @XmlAttribute
058        private ThreadPoolRejectedPolicy rejectedPolicy = ThreadPoolRejectedPolicy.CallerRuns;
059        @XmlAttribute(required = true)
060        private String threadName;
061        @XmlAttribute
062        private Boolean daemon = Boolean.TRUE;
063        @XmlAttribute
064        private String camelContextId;
065        @XmlTransient
066        private CamelContext camelContext;
067    
068        public Object getObject() throws Exception {
069            if (camelContext == null && camelContextId != null) {
070                camelContext = getCamelContextWithId(camelContextId);
071            }
072    
073            notNull(camelContext, "camelContext");
074            if (poolSize == null || poolSize <= 0) {
075                throw new IllegalArgumentException("PoolSize must be a positive number");
076            }
077    
078            int max = getMaxPoolSize() != null ? getMaxPoolSize() : getPoolSize();
079            RejectedExecutionHandler rejected = null;
080            if (rejectedPolicy != null) {
081                rejected = rejectedPolicy.asRejectedExecutionHandler();
082            }
083    
084            ExecutorService answer = camelContext.getExecutorServiceStrategy().newThreadPool(getId(), getThreadName(), getPoolSize(), max,
085                        getKeepAliveTime(), getTimeUnit(), getMaxQueueSize(), rejected, isDaemon());
086            return answer;
087        }
088    
089        protected abstract CamelContext getCamelContextWithId(String camelContextId);
090    
091        public Class getObjectType() {
092            return ExecutorService.class;
093        }
094    
095        public boolean isSingleton() {
096            return true;
097        }
098    
099        public Integer getPoolSize() {
100            return poolSize;
101        }
102    
103        public void setPoolSize(Integer poolSize) {
104            this.poolSize = poolSize;
105        }
106    
107        public Integer getMaxPoolSize() {
108            return maxPoolSize;
109        }
110    
111        public void setMaxPoolSize(Integer maxPoolSize) {
112            this.maxPoolSize = maxPoolSize;
113        }
114    
115        public Long getKeepAliveTime() {
116            return keepAliveTime;
117        }
118    
119        public void setKeepAliveTime(Long keepAliveTime) {
120            this.keepAliveTime = keepAliveTime;
121        }
122    
123        public TimeUnit getTimeUnit() {
124            return timeUnit;
125        }
126    
127        public void setTimeUnit(TimeUnit timeUnit) {
128            this.timeUnit = timeUnit;
129        }
130    
131        public Integer getMaxQueueSize() {
132            return maxQueueSize;
133        }
134    
135        public void setMaxQueueSize(Integer maxQueueSize) {
136            this.maxQueueSize = maxQueueSize;
137        }
138    
139        public ThreadPoolRejectedPolicy getRejectedPolicy() {
140            return rejectedPolicy;
141        }
142    
143        public void setRejectedPolicy(ThreadPoolRejectedPolicy rejectedPolicy) {
144            this.rejectedPolicy = rejectedPolicy;
145        }
146    
147        public String getThreadName() {
148            return threadName;
149        }
150    
151        public void setThreadName(String threadName) {
152            this.threadName = threadName;
153        }
154    
155        public Boolean isDaemon() {
156            return daemon;
157        }
158    
159        public void setDaemon(Boolean daemon) {
160            this.daemon = daemon;
161        }
162    
163        public String getCamelContextId() {
164            return camelContextId;
165        }
166    
167        public void setCamelContextId(String camelContextId) {
168            this.camelContextId = camelContextId;
169        }
170    
171        public CamelContext getCamelContext() {
172            return camelContext;
173        }
174    
175        public void setCamelContext(CamelContext camelContext) {
176            this.camelContext = camelContext;
177        }
178    
179    }