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 thread pool, which ensures the thread 029 * pool is also managed and consistent with other usages of thread pools in Camel. 030 */ 031public class CamelThreadPoolTaskScheduler extends ThreadPoolTaskScheduler { 032 033 private static final long serialVersionUID = 1L; 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( 046 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}