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 import java.util.concurrent.TimeUnit;
022
023 import javax.xml.bind.annotation.XmlAccessType;
024 import javax.xml.bind.annotation.XmlAccessorType;
025 import javax.xml.bind.annotation.XmlAttribute;
026 import javax.xml.bind.annotation.XmlElementRef;
027 import javax.xml.bind.annotation.XmlRootElement;
028 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
029
030 import org.apache.camel.Processor;
031 import org.apache.camel.builder.xml.TimeUnitAdapter;
032 import org.apache.camel.processor.SamplingThrottler;
033 import org.apache.camel.spi.RouteContext;
034
035 /**
036 * Represents an XML <sample/> element
037 *
038 * @version $Revision: 883614 $
039 */
040 @XmlRootElement(name = "sample")
041 @XmlAccessorType(XmlAccessType.FIELD)
042 @SuppressWarnings("unchecked")
043 public class SamplingDefinition extends ProcessorDefinition<ProcessorDefinition> {
044
045 // use Long to let it be optional in JAXB so when using XML the default is 1 second
046
047 @XmlAttribute()
048 private Long samplePeriod = 1L;
049
050 @XmlAttribute()
051 @XmlJavaTypeAdapter(TimeUnitAdapter.class)
052 private TimeUnit units = TimeUnit.SECONDS;
053
054 @XmlElementRef
055 private List<ProcessorDefinition> outputs = new ArrayList<ProcessorDefinition>();
056
057 public SamplingDefinition() {
058 }
059
060 public SamplingDefinition(long samplePeriod, TimeUnit units) {
061 this.samplePeriod = samplePeriod;
062 this.units = units;
063 }
064
065 @Override
066 public String toString() {
067 return "Sample[1 Exchange per " + samplePeriod + " " + units.toString().toLowerCase() + " -> " + getOutputs() + "]";
068 }
069
070 @Override
071 public String getShortName() {
072 return "sample";
073 }
074
075 @Override
076 public String getLabel() {
077 return "sample[1 Exchange per " + samplePeriod + " " + units.toString().toLowerCase() + "]";
078 }
079
080 @Override
081 public Processor createProcessor(RouteContext routeContext) throws Exception {
082 Processor childProcessor = routeContext.createProcessor(this);
083 return new SamplingThrottler(childProcessor, samplePeriod, units);
084 }
085
086 // Fluent API
087 // -------------------------------------------------------------------------
088
089 /**
090 * Sets the sample period during which only a single {@link org.apache.camel.Exchange} will pass through.
091 *
092 * @param samplePeriod the period
093 * @return the builder
094 */
095 public SamplingDefinition samplePeriod(long samplePeriod) {
096 setSamplePeriod(samplePeriod);
097 return this;
098 }
099
100 /**
101 * Sets the time units for the sample period, defaulting to seconds.
102 *
103 * @param units the time unit of the sample period.
104 * @return the builder
105 */
106 public SamplingDefinition timeUnits(TimeUnit units) {
107 setUnits(units);
108 return this;
109 }
110
111 // Properties
112 // -------------------------------------------------------------------------
113
114 public List<ProcessorDefinition> getOutputs() {
115 return outputs;
116 }
117
118 public void setOutputs(List<ProcessorDefinition> outputs) {
119 this.outputs = outputs;
120 }
121
122 public long getSamplePeriod() {
123 return samplePeriod;
124 }
125
126 public void setSamplePeriod(long samplePeriod) {
127 this.samplePeriod = samplePeriod;
128 }
129
130 public void setUnits(String units) {
131 this.units = TimeUnit.valueOf(units);
132 }
133
134 public void setUnits(TimeUnit units) {
135 this.units = units;
136 }
137
138 }