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; 018 019import java.util.concurrent.ScheduledExecutorService; 020 021import org.apache.camel.Expression; 022import org.apache.camel.Processor; 023import org.apache.camel.model.ProcessorDefinition; 024import org.apache.camel.model.ProcessorDefinitionHelper; 025import org.apache.camel.model.ThrottleDefinition; 026import org.apache.camel.model.language.ExpressionDefinition; 027import org.apache.camel.processor.Throttler; 028import org.apache.camel.spi.RouteContext; 029 030public class ThrottleReifier extends ExpressionReifier<ThrottleDefinition> { 031 032 public ThrottleReifier(ProcessorDefinition<?> definition) { 033 super((ThrottleDefinition)definition); 034 } 035 036 @Override 037 public Processor createProcessor(RouteContext routeContext) throws Exception { 038 boolean async = definition.getAsyncDelayed() != null && definition.getAsyncDelayed(); 039 boolean shutdownThreadPool = ProcessorDefinitionHelper.willCreateNewThreadPool(routeContext, definition, true); 040 ScheduledExecutorService threadPool = ProcessorDefinitionHelper.getConfiguredScheduledExecutorService(routeContext, "Throttle", definition, true); 041 042 // should be default 1000 millis 043 long period = definition.getTimePeriodMillis() != null ? definition.getTimePeriodMillis() : 1000L; 044 045 // max requests per period is mandatory 046 Expression maxRequestsExpression = createMaxRequestsPerPeriodExpression(routeContext); 047 if (maxRequestsExpression == null) { 048 throw new IllegalArgumentException("MaxRequestsPerPeriod expression must be provided on " + this); 049 } 050 051 Expression correlation = null; 052 if (definition.getCorrelationExpression() != null) { 053 correlation = definition.getCorrelationExpression().createExpression(routeContext); 054 } 055 056 boolean reject = definition.getRejectExecution() != null && definition.getRejectExecution(); 057 Throttler answer = new Throttler(routeContext.getCamelContext(), maxRequestsExpression, period, threadPool, shutdownThreadPool, reject, correlation); 058 059 answer.setAsyncDelayed(async); 060 if (definition.getCallerRunsWhenRejected() == null) { 061 // should be true by default 062 answer.setCallerRunsWhenRejected(true); 063 } else { 064 answer.setCallerRunsWhenRejected(definition.getCallerRunsWhenRejected()); 065 } 066 067 return answer; 068 } 069 070 private Expression createMaxRequestsPerPeriodExpression(RouteContext routeContext) { 071 ExpressionDefinition expr = definition.getExpression(); 072 if (expr != null) { 073 return expr.createExpression(routeContext); 074 } 075 return null; 076 } 077 078}