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 */ 017 package org.apache.camel.model; 018 019 import java.util.ArrayList; 020 import java.util.List; 021 022 import javax.xml.bind.annotation.XmlAccessType; 023 import javax.xml.bind.annotation.XmlAccessorType; 024 import javax.xml.bind.annotation.XmlAttribute; 025 import javax.xml.bind.annotation.XmlElement; 026 import javax.xml.bind.annotation.XmlElementRef; 027 import javax.xml.bind.annotation.XmlRootElement; 028 029 import org.apache.camel.Processor; 030 import org.apache.camel.impl.RouteContext; 031 import org.apache.camel.processor.Throttler; 032 033 /** 034 * @version $Revision: 1.1 $ 035 */ 036 @XmlRootElement(name = "throttler") 037 @XmlAccessorType(XmlAccessType.FIELD) 038 public class ThrottlerType extends ProcessorType { 039 @XmlAttribute 040 private Long maximumRequestsPerPeriod; 041 @XmlAttribute 042 private long timePeriodMillis = 1000; 043 @XmlElementRef 044 private List<InterceptorType> interceptors = new ArrayList<InterceptorType>(); 045 @XmlElementRef 046 private List<ProcessorType> outputs = new ArrayList<ProcessorType>(); 047 048 public ThrottlerType() { 049 } 050 051 public ThrottlerType(long maximumRequestsPerPeriod) { 052 this.maximumRequestsPerPeriod = maximumRequestsPerPeriod; 053 } 054 055 @Override 056 public String toString() { 057 return "Throttler[" + getMaximumRequestsPerPeriod() + " request per " + getTimePeriodMillis() 058 + " millis -> " + getOutputs() + "]"; 059 } 060 061 @Override 062 public Processor createProcessor(RouteContext routeContext) throws Exception { 063 Processor childProcessor = routeContext.createProcessor(this); 064 return new Throttler(childProcessor, maximumRequestsPerPeriod, timePeriodMillis); 065 } 066 067 // Fluent API 068 // ------------------------------------------------------------------------- 069 070 /** 071 * Sets the time period during which the maximum request count is valid for 072 */ 073 public ThrottlerType timePeriodMillis(long timePeriodMillis) { 074 this.timePeriodMillis = timePeriodMillis; 075 return this; 076 } 077 078 // Properties 079 // ------------------------------------------------------------------------- 080 081 public Long getMaximumRequestsPerPeriod() { 082 return maximumRequestsPerPeriod; 083 } 084 085 public void setMaximumRequestsPerPeriod(Long maximumRequestsPerPeriod) { 086 this.maximumRequestsPerPeriod = maximumRequestsPerPeriod; 087 } 088 089 public long getTimePeriodMillis() { 090 return timePeriodMillis; 091 } 092 093 public void setTimePeriodMillis(long timePeriodMillis) { 094 this.timePeriodMillis = timePeriodMillis; 095 } 096 097 public List<InterceptorType> getInterceptors() { 098 return interceptors; 099 } 100 101 public void setInterceptors(List<InterceptorType> interceptors) { 102 this.interceptors = interceptors; 103 } 104 105 public List<ProcessorType> getOutputs() { 106 return outputs; 107 } 108 109 public void setOutputs(List<ProcessorType> outputs) { 110 this.outputs = outputs; 111 } 112 }