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.model;
018    
019    import java.util.concurrent.ExecutorService;
020    import javax.xml.bind.annotation.XmlAccessType;
021    import javax.xml.bind.annotation.XmlAccessorType;
022    import javax.xml.bind.annotation.XmlAttribute;
023    import javax.xml.bind.annotation.XmlRootElement;
024    import javax.xml.bind.annotation.XmlTransient;
025    
026    import org.apache.camel.Processor;
027    import org.apache.camel.WaitForTaskToComplete;
028    import org.apache.camel.processor.ThreadsProcessor;
029    import org.apache.camel.processor.UnitOfWorkProcessor;
030    import org.apache.camel.spi.RouteContext;
031    import org.apache.camel.util.concurrent.ExecutorServiceHelper;
032    
033    /**
034     * Represents an XML <threads/> element
035     *
036     * @version $Revision: 890241 $
037     */
038    @XmlRootElement(name = "threads")
039    @XmlAccessorType(XmlAccessType.FIELD)
040    public class ThreadsDefinition extends OutputDefinition<ProcessorDefinition> {
041    
042        @XmlTransient
043        private ExecutorService executorService;
044        @XmlAttribute(required = false)
045        private String executorServiceRef;
046        @XmlAttribute(required = false)
047        private Integer poolSize;
048        @XmlAttribute(required = false)
049        private WaitForTaskToComplete waitForTaskToComplete = WaitForTaskToComplete.IfReplyExpected;
050    
051        @Override
052        public Processor createProcessor(RouteContext routeContext) throws Exception {
053            if (executorServiceRef != null) {
054                executorService = routeContext.lookup(executorServiceRef, ExecutorService.class);
055            }
056            if (executorService == null && poolSize != null) {
057                executorService = ExecutorServiceHelper.newScheduledThreadPool(poolSize, "Threads", true);
058            }
059            Processor childProcessor = routeContext.createProcessor(this);
060    
061            // wrap it in a unit of work so the route that comes next is also done in a unit of work
062            UnitOfWorkProcessor uow = new UnitOfWorkProcessor(routeContext, childProcessor);
063    
064            return new ThreadsProcessor(uow, executorService, waitForTaskToComplete);
065        }
066    
067        @Override
068        public String getLabel() {
069            return "threads";
070        }
071    
072        @Override
073        public String getShortName() {
074            return "threads";
075        }
076    
077        @Override
078        public String toString() {
079            return "Threads[" + getOutputs() + "]";
080        }
081    
082        /**
083         * Setting the executor service for the thread pool
084         *
085         * @return the builder
086         */
087        public ThreadsDefinition executorService(ExecutorService executorService) {
088            setExecutorService(executorService);
089            return this;
090        }
091    
092        /**
093         * Setting the executor service for the thread pool
094         *
095         * @return the builder
096         */
097        public ThreadsDefinition executorServiceRef(String executorServiceRef) {
098            setExecutorServiceRef(executorServiceRef);
099            return this;
100        }
101    
102        /**
103         * Setting the core pool size for the underlying {@link java.util.concurrent.ExecutorService}.
104         *
105         * @return the builder
106         */
107        public ThreadsDefinition poolSize(int poolSize) {
108            setPoolSize(poolSize);
109            return this;
110        }
111    
112        /**
113         * Setting to whether to wait for async tasks to be complete before continuing original route.
114         * <p/>
115         * Is default <tt>IfReplyExpected</tt>
116         *
117         * @param wait the wait option
118         * @return the builder
119         */
120        public ThreadsDefinition waitForTaskToComplete(WaitForTaskToComplete wait) {
121            setWaitForTaskToComplete(wait);
122            return this;
123        }
124    
125        public ExecutorService getExecutorService() {
126            return executorService;
127        }
128    
129        public void setExecutorService(ExecutorService executorService) {
130            this.executorService = executorService;
131        }
132    
133        public String getExecutorServiceRef() {
134            return executorServiceRef;
135        }
136    
137        public void setExecutorServiceRef(String executorServiceRef) {
138            this.executorServiceRef = executorServiceRef;
139        }
140    
141        public Integer getPoolSize() {
142            return poolSize;
143        }
144    
145        public void setPoolSize(Integer poolSize) {
146            this.poolSize = poolSize;
147        }
148    
149        public WaitForTaskToComplete getWaitForTaskToComplete() {
150            return waitForTaskToComplete;
151        }
152    
153        public void setWaitForTaskToComplete(WaitForTaskToComplete waitForTaskToComplete) {
154            this.waitForTaskToComplete = waitForTaskToComplete;
155        }
156    }