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