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.impl;
018    
019    import java.util.List;
020    import java.util.concurrent.ExecutorService;
021    import java.util.concurrent.RejectedExecutionHandler;
022    import java.util.concurrent.ScheduledExecutorService;
023    import java.util.concurrent.ThreadPoolExecutor;
024    import java.util.concurrent.TimeUnit;
025    
026    import org.apache.camel.CamelContext;
027    import org.apache.camel.spi.ExecutorServiceStrategy;
028    import org.apache.camel.spi.ThreadPoolProfile;
029    import org.apache.camel.support.ServiceSupport;
030    import org.apache.camel.util.concurrent.SynchronousExecutorService;
031    
032    /**
033     * @deprecated use {@link org.apache.camel.spi.ExecutorServiceManager} instead, will be removed in a future Camel release
034     */
035    @Deprecated
036    public class DefaultExecutorServiceStrategy extends ServiceSupport implements ExecutorServiceStrategy {
037    
038        // delegate to ExecutorServiceManager
039    
040        private final CamelContext camelContext;
041    
042        public DefaultExecutorServiceStrategy(CamelContext camelContext) {
043            this.camelContext = camelContext;
044        }
045    
046        public void registerThreadPoolProfile(ThreadPoolProfile profile) {
047            camelContext.getExecutorServiceManager().registerThreadPoolProfile(profile);
048        }
049    
050        public ThreadPoolProfile getThreadPoolProfile(String id) {
051            return camelContext.getExecutorServiceManager().getThreadPoolProfile(id);
052        }
053    
054        public ThreadPoolProfile getDefaultThreadPoolProfile() {
055            return camelContext.getExecutorServiceManager().getDefaultThreadPoolProfile();
056        }
057    
058        public void setDefaultThreadPoolProfile(ThreadPoolProfile defaultThreadPoolProfile) {
059            camelContext.getExecutorServiceManager().setDefaultThreadPoolProfile(defaultThreadPoolProfile);
060        }
061    
062        public String getThreadName(String name) {
063            return camelContext.getExecutorServiceManager().resolveThreadName(name);
064        }
065    
066        public String getThreadNamePattern() {
067            return camelContext.getExecutorServiceManager().getThreadNamePattern();
068        }
069    
070        public void setThreadNamePattern(String pattern) throws IllegalArgumentException {
071            camelContext.getExecutorServiceManager().setThreadNamePattern(pattern);
072        }
073    
074        public ExecutorService lookup(Object source, String name, String executorServiceRef) {
075            ExecutorService answer = camelContext.getRegistry().lookupByNameAndType(executorServiceRef, ExecutorService.class);
076            if (answer == null) {
077                // try to see if we got a thread pool profile with that id
078                answer = newThreadPool(source, name, executorServiceRef);
079            }
080            return answer;
081        }
082    
083        public ScheduledExecutorService lookupScheduled(Object source, String name, String executorServiceRef) {
084            ScheduledExecutorService answer = camelContext.getRegistry().lookupByNameAndType(executorServiceRef, ScheduledExecutorService.class);
085            if (answer == null) {
086                ThreadPoolProfile profile = getThreadPoolProfile(executorServiceRef);
087                if (profile != null) {
088                    Integer poolSize = profile.getPoolSize();
089                    if (poolSize == null) {
090                        poolSize = getDefaultThreadPoolProfile().getPoolSize();
091                    }
092                    answer = newScheduledThreadPool(source, name, poolSize);
093                }
094            }
095            return answer;
096        }
097    
098        public ExecutorService newDefaultThreadPool(Object source, String name) {
099            return camelContext.getExecutorServiceManager().newDefaultThreadPool(source, name);
100        }
101    
102        public ExecutorService newThreadPool(Object source, String name, String threadPoolProfileId) {
103            return camelContext.getExecutorServiceManager().newThreadPool(source, name, threadPoolProfileId);
104        }
105    
106        public ExecutorService newCachedThreadPool(Object source, String name) {
107            return camelContext.getExecutorServiceManager().newCachedThreadPool(source, name);
108        }
109    
110        public ScheduledExecutorService newScheduledThreadPool(Object source, String name, int poolSize) {
111            return camelContext.getExecutorServiceManager().newScheduledThreadPool(source, name, poolSize);
112        }
113    
114        public ScheduledExecutorService newScheduledThreadPool(Object source, String name) {
115            return camelContext.getExecutorServiceManager().newDefaultScheduledThreadPool(source, name);
116        }
117    
118        public ExecutorService newFixedThreadPool(Object source, String name, int poolSize) {
119            return camelContext.getExecutorServiceManager().newFixedThreadPool(source, name, poolSize);
120        }
121    
122        public ExecutorService newSingleThreadExecutor(Object source, String name) {
123            return camelContext.getExecutorServiceManager().newSingleThreadExecutor(source, name);
124        }
125    
126        public ExecutorService newSynchronousThreadPool(Object source, String name) {
127            return new SynchronousExecutorService();
128        }
129    
130        public ExecutorService newThreadPool(Object source, String name, int corePoolSize, int maxPoolSize) {
131            return camelContext.getExecutorServiceManager().newThreadPool(source, name, corePoolSize, maxPoolSize);
132        }
133    
134        public ExecutorService newThreadPool(Object source, String name, int corePoolSize, int maxPoolSize, int maxQueueSize) {
135            // use a profile with the settings
136            ThreadPoolProfile profile = new ThreadPoolProfile();
137            profile.setPoolSize(corePoolSize);
138            profile.setMaxPoolSize(maxPoolSize);
139            profile.setMaxQueueSize(maxQueueSize);
140    
141            return camelContext.getExecutorServiceManager().newThreadPool(source, name, profile);
142        }
143    
144        public ExecutorService newThreadPool(Object source, String name, int corePoolSize, int maxPoolSize,
145                                             long keepAliveTime, TimeUnit timeUnit, int maxQueueSize,
146                                             RejectedExecutionHandler rejectedExecutionHandler, boolean daemon) {
147            // use a profile with the settings
148            ThreadPoolProfile profile = new ThreadPoolProfile();
149            profile.setPoolSize(corePoolSize);
150            profile.setMaxPoolSize(maxPoolSize);
151            profile.setMaxQueueSize(maxQueueSize);
152            profile.setKeepAliveTime(keepAliveTime);
153            profile.setTimeUnit(timeUnit);
154    
155            // must cast to ThreadPoolExecutor to be able to set the rejected execution handler
156            ThreadPoolExecutor answer = (ThreadPoolExecutor) camelContext.getExecutorServiceManager().newThreadPool(source, name, profile);
157            answer.setRejectedExecutionHandler(rejectedExecutionHandler);
158            return answer;
159        }
160    
161        public void shutdown(ExecutorService executorService) {
162            camelContext.getExecutorServiceManager().shutdown(executorService);
163        }
164    
165        public List<Runnable> shutdownNow(ExecutorService executorService) {
166            return camelContext.getExecutorServiceManager().shutdownNow(executorService);
167        }
168    
169        protected void doStart() throws Exception {
170            // noop
171        }
172    
173        protected void doStop() throws Exception {
174            // noop
175        }
176    }