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