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.reifier.errorhandler; 018 019import java.util.concurrent.ScheduledExecutorService; 020 021import org.apache.camel.CamelContext; 022import org.apache.camel.ErrorHandlerFactory; 023import org.apache.camel.Processor; 024import org.apache.camel.builder.DefaultErrorHandlerBuilder; 025import org.apache.camel.processor.errorhandler.DefaultErrorHandler; 026import org.apache.camel.spi.ExecutorServiceManager; 027import org.apache.camel.spi.RouteContext; 028import org.apache.camel.spi.ThreadPoolProfile; 029 030public class DefaultErrorHandlerReifier<T extends DefaultErrorHandlerBuilder> extends ErrorHandlerReifier<T> { 031 032 public DefaultErrorHandlerReifier(ErrorHandlerFactory definition) { 033 super((T)definition); 034 } 035 036 @Override 037 public Processor createErrorHandler(RouteContext routeContext, Processor processor) throws Exception { 038 DefaultErrorHandler answer = new DefaultErrorHandler(routeContext.getCamelContext(), processor, definition.getLogger(), definition.getOnRedelivery(), 039 definition.getRedeliveryPolicy(), definition.getExceptionPolicyStrategy(), 040 definition.getRetryWhilePolicy(routeContext.getCamelContext()), getExecutorService(routeContext.getCamelContext()), 041 definition.getOnPrepareFailure(), definition.getOnExceptionOccurred()); 042 // configure error handler before we can use it 043 configure(routeContext, answer); 044 return answer; 045 } 046 047 protected synchronized ScheduledExecutorService getExecutorService(CamelContext camelContext) { 048 ScheduledExecutorService executorService = definition.getExecutorService(); 049 String executorServiceRef = definition.getExecutorServiceRef(); 050 if (executorService == null || executorService.isShutdown()) { 051 // camel context will shutdown the executor when it shutdown so no 052 // need to shut it down when stopping 053 if (executorServiceRef != null) { 054 executorService = camelContext.getRegistry().lookupByNameAndType(executorServiceRef, ScheduledExecutorService.class); 055 if (executorService == null) { 056 ExecutorServiceManager manager = camelContext.getExecutorServiceManager(); 057 ThreadPoolProfile profile = manager.getThreadPoolProfile(executorServiceRef); 058 executorService = manager.newScheduledThreadPool(this, executorServiceRef, profile); 059 } 060 if (executorService == null) { 061 throw new IllegalArgumentException("ExecutorServiceRef " + executorServiceRef + " not found in registry."); 062 } 063 } else { 064 // no explicit configured thread pool, so leave it up to the 065 // error handler to decide if it need 066 // a default thread pool from 067 // CamelContext#getErrorHandlerExecutorService 068 executorService = null; 069 } 070 // TODO: ErrorHandler: no modification to the model should be done 071 definition.setExecutorService(executorService); 072 } 073 return executorService; 074 } 075 076}