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 static final long serialVersionUID = 1L; 035 private final CamelContext camelContext; 036 private final Object source; 037 private final String name; 038 039 public CamelThreadPoolTaskScheduler(CamelContext camelContext, Object source, String name) { 040 this.camelContext = camelContext; 041 this.source = source; 042 this.name = name; 043 } 044 045 @Override 046 protected ScheduledExecutorService createExecutor(int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) { 047 return camelContext.getExecutorServiceManager().newScheduledThreadPool(source, name, poolSize); 048 } 049 050 @Override 051 public void shutdown() { 052 camelContext.getExecutorServiceManager().shutdownNow(getScheduledExecutor()); 053 } 054}