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