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 */ 017package org.apache.camel.spring.util; 018 019import java.util.concurrent.RejectedExecutionHandler; 020import java.util.concurrent.ScheduledExecutorService; 021import java.util.concurrent.ThreadFactory; 022 023import org.apache.camel.CamelContext; 024import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; 025 026/** 027 * A Camel extension of Spring's {@link ThreadPoolTaskScheduler} which uses the 028 * {@link org.apache.camel.spi.ExecutorServiceManager} to create and destroy the 029 * thread pool, which ensures the thread pool is also managed and consistent with 030 * other usages of thread pools in Camel. 031 */ 032public class CamelThreadPoolTaskScheduler extends ThreadPoolTaskScheduler { 033 034 private final CamelContext camelContext; 035 private final Object source; 036 private final String name; 037 038 public CamelThreadPoolTaskScheduler(CamelContext camelContext, Object source, String name) { 039 this.camelContext = camelContext; 040 this.source = source; 041 this.name = name; 042 } 043 044 @Override 045 protected ScheduledExecutorService createExecutor(int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) { 046 return camelContext.getExecutorServiceManager().newScheduledThreadPool(source, name, poolSize); 047 } 048 049 @Override 050 public void shutdown() { 051 camelContext.getExecutorServiceManager().shutdownNow(getScheduledExecutor()); 052 } 053}