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.model;
018
019import java.time.Duration;
020import java.util.Locale;
021import java.util.concurrent.TimeUnit;
022
023import javax.xml.bind.annotation.XmlAccessType;
024import javax.xml.bind.annotation.XmlAccessorType;
025import javax.xml.bind.annotation.XmlAttribute;
026import javax.xml.bind.annotation.XmlRootElement;
027
028import org.apache.camel.spi.Metadata;
029import org.apache.camel.util.TimeUtils;
030
031/**
032 * Extract a sample of the messages passing through a route
033 */
034@Metadata(label = "eip,routing")
035@XmlRootElement(name = "sample")
036@XmlAccessorType(XmlAccessType.FIELD)
037public class SamplingDefinition extends NoOutputDefinition<SamplingDefinition> {
038
039    // use Long to let it be optional in JAXB so when using XML the default is 1
040    // second
041
042    @XmlAttribute
043    @Metadata(defaultValue = "1s", javaType = "java.time.Duration")
044    private String samplePeriod;
045    @XmlAttribute
046    @Metadata(javaType = "java.lang.Long")
047    private String messageFrequency;
048    @XmlAttribute
049    @Metadata(defaultValue = "SECONDS", enums = "NANOSECONDS,MICROSECONDS,MILLISECONDS,SECONDS,MINUTES,HOURS,DAYS",
050              javaType = "java.util.concurrent.TimeUnit", deprecationNote = "Use samplePeriod extended syntax instead")
051    @Deprecated
052    private String units;
053
054    public SamplingDefinition() {
055    }
056
057    public SamplingDefinition(Duration period) {
058        this.samplePeriod = TimeUtils.printDuration(period);
059        this.units = TimeUnit.MILLISECONDS.name();
060    }
061
062    public SamplingDefinition(long samplePeriod, TimeUnit units) {
063        this(Duration.ofMillis(units.toMillis(samplePeriod)));
064    }
065
066    public SamplingDefinition(long messageFrequency) {
067        this.messageFrequency = Long.toString(messageFrequency);
068    }
069
070    @Override
071    public String getShortName() {
072        return "sample";
073    }
074
075    @Override
076    public String toString() {
077        return "Sample[" + description() + " -> " + getOutputs() + "]";
078    }
079
080    protected String description() {
081        if (messageFrequency != null) {
082            return "1 Exchange per " + getMessageFrequency() + " messages received";
083        } else {
084            return "1 Exchange per " + TimeUtils.printDuration(TimeUtils.toDuration(samplePeriod));
085        }
086    }
087
088    @Override
089    public String getLabel() {
090        return "sample[" + description() + "]";
091    }
092
093    // Fluent API
094    // -------------------------------------------------------------------------
095
096    /**
097     * Sets the sample message count which only a single
098     * {@link org.apache.camel.Exchange} will pass through after this many
099     * received.
100     *
101     * @param messageFrequency
102     * @return the builder
103     */
104    public SamplingDefinition sampleMessageFrequency(long messageFrequency) {
105        setMessageFrequency(messageFrequency);
106        return this;
107    }
108
109    /**
110     * Sets the sample period during which only a single
111     * {@link org.apache.camel.Exchange} will pass through.
112     *
113     * @param samplePeriod the period
114     * @return the builder
115     */
116    public SamplingDefinition samplePeriod(Duration samplePeriod) {
117        setSamplePeriod(samplePeriod);
118        return this;
119    }
120
121    /**
122     * Sets the sample period during which only a single
123     * {@link org.apache.camel.Exchange} will pass through.
124     *
125     * @param samplePeriod the period
126     * @return the builder
127     */
128    public SamplingDefinition samplePeriod(long samplePeriod) {
129        setSamplePeriod(samplePeriod);
130        return this;
131    }
132
133    /**
134     * Sets the time units for the sample period, defaulting to seconds.
135     *
136     * @param units the time unit of the sample period.
137     * @return the builder
138     */
139    @Deprecated
140    public SamplingDefinition timeUnits(TimeUnit units) {
141        setUnits(units);
142        return this;
143    }
144
145    // Properties
146    // -------------------------------------------------------------------------
147
148    public String getSamplePeriod() {
149        return samplePeriod;
150    }
151
152    /**
153     * Sets the sample period during which only a single Exchange will pass
154     * through.
155     */
156    public void setSamplePeriod(String samplePeriod) {
157        this.samplePeriod = samplePeriod;
158    }
159
160    public void setSamplePeriod(long samplePeriod) {
161        setSamplePeriod(Duration.ofMillis(samplePeriod));
162    }
163
164    public void setSamplePeriod(Duration samplePeriod) {
165        this.samplePeriod = TimeUtils.printDuration(samplePeriod);
166    }
167
168    public String getMessageFrequency() {
169        return messageFrequency;
170    }
171
172    /**
173     * Sets the sample message count which only a single Exchange will pass
174     * through after this many received.
175     */
176    public void setMessageFrequency(String messageFrequency) {
177        this.messageFrequency = messageFrequency;
178    }
179
180    public void setMessageFrequency(long messageFrequency) {
181        this.messageFrequency = Long.toString(messageFrequency);
182    }
183
184    /**
185     * Sets the time units for the sample period, defaulting to seconds.
186     */
187    @Deprecated
188    public void setUnits(String units) {
189        this.units = units;
190    }
191
192    /**
193     * Sets the time units for the sample period, defaulting to seconds.
194     */
195    @Deprecated
196    public void setUnits(TimeUnit units) {
197        this.units = units.name();
198    }
199
200    @Deprecated
201    public String getUnits() {
202        return units;
203    }
204}