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") 043 private Long samplePeriod; 044 @XmlAttribute 045 private Long messageFrequency; 046 @XmlAttribute 047 @XmlJavaTypeAdapter(TimeUnitAdapter.class) 048 @Metadata(defaultValue = "SECONDS") 049 private TimeUnit units; 050 051 public SamplingDefinition() { 052 } 053 054 public SamplingDefinition(long samplePeriod, TimeUnit units) { 055 this.samplePeriod = samplePeriod; 056 this.units = units; 057 } 058 059 public SamplingDefinition(long messageFrequency) { 060 this.messageFrequency = messageFrequency; 061 } 062 063 @Override 064 public String getShortName() { 065 return "sample"; 066 } 067 068 @Override 069 public String toString() { 070 return "Sample[" + description() + " -> " + getOutputs() + "]"; 071 } 072 073 protected String description() { 074 if (messageFrequency != null) { 075 return "1 Exchange per " + getMessageFrequency() + " messages received"; 076 } else { 077 TimeUnit tu = getUnits() != null ? getUnits() : TimeUnit.SECONDS; 078 return "1 Exchange per " + getSamplePeriod() + " " + tu.toString().toLowerCase(Locale.ENGLISH); 079 } 080 } 081 082 @Override 083 public String getLabel() { 084 return "sample[" + description() + "]"; 085 } 086 087 // Fluent API 088 // ------------------------------------------------------------------------- 089 090 /** 091 * Sets the sample message count which only a single 092 * {@link org.apache.camel.Exchange} will pass through after this many 093 * received. 094 * 095 * @param messageFrequency 096 * @return the builder 097 */ 098 public SamplingDefinition sampleMessageFrequency(long messageFrequency) { 099 setMessageFrequency(messageFrequency); 100 return this; 101 } 102 103 /** 104 * Sets the sample period during which only a single 105 * {@link org.apache.camel.Exchange} will pass through. 106 * 107 * @param samplePeriod the period 108 * @return the builder 109 */ 110 public SamplingDefinition samplePeriod(long samplePeriod) { 111 setSamplePeriod(samplePeriod); 112 return this; 113 } 114 115 /** 116 * Sets the time units for the sample period, defaulting to seconds. 117 * 118 * @param units the time unit of the sample period. 119 * @return the builder 120 */ 121 public SamplingDefinition timeUnits(TimeUnit units) { 122 setUnits(units); 123 return this; 124 } 125 126 // Properties 127 // ------------------------------------------------------------------------- 128 129 public Long getSamplePeriod() { 130 return samplePeriod; 131 } 132 133 /** 134 * Sets the sample period during which only a single Exchange will pass 135 * through. 136 */ 137 public void setSamplePeriod(Long samplePeriod) { 138 this.samplePeriod = samplePeriod; 139 } 140 141 public Long getMessageFrequency() { 142 return messageFrequency; 143 } 144 145 /** 146 * Sets the sample message count which only a single Exchange will pass 147 * through after this many received. 148 */ 149 public void setMessageFrequency(Long messageFrequency) { 150 this.messageFrequency = messageFrequency; 151 } 152 153 /** 154 * Sets the time units for the sample period, defaulting to seconds. 155 */ 156 public void setUnits(String units) { 157 this.units = TimeUnit.valueOf(units); 158 } 159 160 /** 161 * Sets the time units for the sample period, defaulting to seconds. 162 */ 163 public void setUnits(TimeUnit units) { 164 this.units = units; 165 } 166 167 public TimeUnit getUnits() { 168 return units; 169 } 170}