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