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.concurrent.ForkJoinPool; 020 021import javax.xml.bind.annotation.XmlAccessType; 022import javax.xml.bind.annotation.XmlAccessorType; 023import javax.xml.bind.annotation.XmlAttribute; 024 025import org.apache.camel.spi.Metadata; 026 027@XmlAccessorType(XmlAccessType.FIELD) 028public class Resilience4jConfigurationCommon extends IdentifiedType { 029 030 @XmlAttribute 031 @Metadata(label = "circuitbreaker") 032 private String circuitBreakerRef; 033 @XmlAttribute 034 @Metadata(label = "circuitbreaker") 035 private String configRef; 036 @XmlAttribute 037 @Metadata(label = "circuitbreaker", defaultValue = "50", javaType = "java.lang.Float") 038 private String failureRateThreshold; 039 @XmlAttribute 040 @Metadata(label = "circuitbreaker", defaultValue = "10", javaType = "java.lang.Integer") 041 private String permittedNumberOfCallsInHalfOpenState; 042 @XmlAttribute 043 @Metadata(label = "circuitbreaker", defaultValue = "100", javaType = "java.lang.Integer") 044 private String slidingWindowSize; 045 @XmlAttribute 046 @Metadata(label = "circuitbreaker", defaultValue = "COUNT_BASED", enums = "TIME_BASED,COUNT_BASED") 047 private String slidingWindowType; 048 @XmlAttribute 049 @Metadata(label = "circuitbreaker", defaultValue = "100", javaType = "java.lang.Integer") 050 private String minimumNumberOfCalls; 051 @XmlAttribute 052 @Metadata(label = "circuitbreaker", defaultValue = "true", javaType = "java.lang.Boolean") 053 private String writableStackTraceEnabled; 054 @XmlAttribute 055 @Metadata(label = "circuitbreaker", defaultValue = "60", javaType = "java.lang.Integer") 056 private String waitDurationInOpenState; 057 @XmlAttribute 058 @Metadata(label = "circuitbreaker", defaultValue = "false", javaType = "java.lang.Boolean") 059 private String automaticTransitionFromOpenToHalfOpenEnabled; 060 @XmlAttribute 061 @Metadata(label = "circuitbreaker", defaultValue = "100", javaType = "java.lang.Float") 062 private String slowCallRateThreshold; 063 @XmlAttribute 064 @Metadata(label = "circuitbreaker", defaultValue = "60", javaType = "java.lang.Integer") 065 private String slowCallDurationThreshold; 066 @Metadata(label = "bulkhead", defaultValue = "false", javaType = "java.lang.Boolean") 067 private String bulkheadEnabled; 068 @Metadata(label = "bulkhead", defaultValue = "25", javaType = "java.lang.Integer") 069 private String bulkheadMaxConcurrentCalls; 070 @Metadata(label = "bulkhead", defaultValue = "0", javaType = "java.lang.Integer") 071 private String bulkheadMaxWaitDuration; 072 @Metadata(label = "timeout", defaultValue = "false", javaType = "java.lang.Boolean") 073 private String timeoutEnabled; 074 @Metadata(label = "timeout") 075 private String timeoutExecutorServiceRef; 076 @Metadata(label = "timeout", defaultValue = "1000", javaType = "java.lang.Integer") 077 private String timeoutDuration; 078 @Metadata(label = "timeout", defaultValue = "true", javaType = "java.lang.Boolean") 079 private String timeoutCancelRunningFuture; 080 081 // Getter/Setter 082 // ------------------------------------------------------------------------- 083 084 public String getCircuitBreakerRef() { 085 return circuitBreakerRef; 086 } 087 088 /** 089 * Refers to an existing io.github.resilience4j.circuitbreaker.CircuitBreaker instance 090 * to lookup and use from the registry. When using this, then any other circuit breaker options 091 * are not in use. 092 */ 093 public void setCircuitBreakerRef(String circuitBreakerRef) { 094 this.circuitBreakerRef = circuitBreakerRef; 095 } 096 097 public String getConfigRef() { 098 return configRef; 099 } 100 101 /** 102 * Refers to an existing io.github.resilience4j.circuitbreaker.CircuitBreakerConfig instance 103 * to lookup and use from the registry. 104 */ 105 public void setConfigRef(String configRef) { 106 this.configRef = configRef; 107 } 108 109 public String getFailureRateThreshold() { 110 return failureRateThreshold; 111 } 112 113 /** 114 * Configures the failure rate threshold in percentage. 115 * If the failure rate is equal or greater than the threshold the CircuitBreaker transitions to open and starts short-circuiting calls. 116 * <p> 117 * The threshold must be greater than 0 and not greater than 100. Default value is 50 percentage. 118 */ 119 public void setFailureRateThreshold(String failureRateThreshold) { 120 this.failureRateThreshold = failureRateThreshold; 121 } 122 123 public String getPermittedNumberOfCallsInHalfOpenState() { 124 return permittedNumberOfCallsInHalfOpenState; 125 } 126 127 /** 128 * Configures the number of permitted calls when the CircuitBreaker is half open. 129 * <p> 130 * The size must be greater than 0. Default size is 10. 131 */ 132 public void setPermittedNumberOfCallsInHalfOpenState(String permittedNumberOfCallsInHalfOpenState) { 133 this.permittedNumberOfCallsInHalfOpenState = permittedNumberOfCallsInHalfOpenState; 134 } 135 136 public String getSlidingWindowSize() { 137 return slidingWindowSize; 138 } 139 140 /** 141 * Configures the size of the sliding window which is used to record the outcome of calls when the CircuitBreaker is closed. 142 * {@code slidingWindowSize} configures the size of the sliding window. Sliding window can either be count-based or time-based. 143 * 144 * If {@code slidingWindowType} is COUNT_BASED, the last {@code slidingWindowSize} calls are recorded and aggregated. 145 * If {@code slidingWindowType} is TIME_BASED, the calls of the last {@code slidingWindowSize} seconds are recorded and aggregated. 146 * <p> 147 * The {@code slidingWindowSize} must be greater than 0. 148 * The {@code minimumNumberOfCalls} must be greater than 0. 149 * If the slidingWindowType is COUNT_BASED, the {@code minimumNumberOfCalls} cannot be greater than {@code slidingWindowSize}. 150 * If the slidingWindowType is TIME_BASED, you can pick whatever you want. 151 * 152 * Default slidingWindowSize is 100. 153 */ 154 public void setSlidingWindowSize(String slidingWindowSize) { 155 this.slidingWindowSize = slidingWindowSize; 156 } 157 158 public String getSlidingWindowType() { 159 return slidingWindowType; 160 } 161 162 /** 163 * Configures the type of the sliding window which is used to record the outcome of calls when the CircuitBreaker is closed. 164 * Sliding window can either be count-based or time-based. 165 * 166 * If {@code slidingWindowType} is COUNT_BASED, the last {@code slidingWindowSize} calls are recorded and aggregated. 167 * If {@code slidingWindowType} is TIME_BASED, the calls of the last {@code slidingWindowSize} seconds are recorded and aggregated. 168 * 169 * Default slidingWindowType is COUNT_BASED. 170 */ 171 public void setSlidingWindowType(String slidingWindowType) { 172 this.slidingWindowType = slidingWindowType; 173 } 174 175 public String getMinimumNumberOfCalls() { 176 return minimumNumberOfCalls; 177 } 178 179 /** 180 * Configures configures the minimum number of calls which are required (per sliding window period) before the CircuitBreaker can calculate the error rate. 181 * For example, if {@code minimumNumberOfCalls} is 10, then at least 10 calls must be recorded, before the failure rate can be calculated. 182 * If only 9 calls have been recorded the CircuitBreaker will not transition to open even if all 9 calls have failed. 183 * 184 * Default minimumNumberOfCalls is 100 185 */ 186 public void setMinimumNumberOfCalls(String minimumNumberOfCalls) { 187 this.minimumNumberOfCalls = minimumNumberOfCalls; 188 } 189 190 public String getWritableStackTraceEnabled() { 191 return writableStackTraceEnabled; 192 } 193 194 /** 195 * Enables writable stack traces. When set to false, Exception.getStackTrace returns a zero length array. 196 * This may be used to reduce log spam when the circuit breaker is open as the cause of the exceptions is already known (the circuit breaker is short-circuiting calls). 197 */ 198 public void setWritableStackTraceEnabled(String writableStackTraceEnabled) { 199 this.writableStackTraceEnabled = writableStackTraceEnabled; 200 } 201 202 public String getWaitDurationInOpenState() { 203 return waitDurationInOpenState; 204 } 205 206 /** 207 * Configures the wait duration (in seconds) which specifies how long the CircuitBreaker should stay open, before it switches to half open. 208 * Default value is 60 seconds. 209 */ 210 public void setWaitDurationInOpenState(String waitDurationInOpenState) { 211 this.waitDurationInOpenState = waitDurationInOpenState; 212 } 213 214 public String getAutomaticTransitionFromOpenToHalfOpenEnabled() { 215 return automaticTransitionFromOpenToHalfOpenEnabled; 216 } 217 218 /** 219 * Enables automatic transition from OPEN to HALF_OPEN state once the waitDurationInOpenState has passed. 220 */ 221 public void setAutomaticTransitionFromOpenToHalfOpenEnabled(String automaticTransitionFromOpenToHalfOpenEnabled) { 222 this.automaticTransitionFromOpenToHalfOpenEnabled = automaticTransitionFromOpenToHalfOpenEnabled; 223 } 224 225 public String getSlowCallRateThreshold() { 226 return slowCallRateThreshold; 227 } 228 229 /** 230 * Configures a threshold in percentage. The CircuitBreaker considers a call as slow when the call duration is greater than slowCallDurationThreshold(Duration. 231 * When the percentage of slow calls is equal or greater the threshold, the CircuitBreaker transitions to open and starts short-circuiting calls. 232 * <p> 233 * The threshold must be greater than 0 and not greater than 100. 234 * Default value is 100 percentage which means that all recorded calls must be slower than slowCallDurationThreshold. 235 */ 236 public void setSlowCallRateThreshold(String slowCallRateThreshold) { 237 this.slowCallRateThreshold = slowCallRateThreshold; 238 } 239 240 public String getSlowCallDurationThreshold() { 241 return slowCallDurationThreshold; 242 } 243 244 /** 245 * Configures the duration threshold (seconds) above which calls are considered as slow and increase the slow calls percentage. 246 * Default value is 60 seconds. 247 */ 248 public void setSlowCallDurationThreshold(String slowCallDurationThreshold) { 249 this.slowCallDurationThreshold = slowCallDurationThreshold; 250 } 251 252 public String getBulkheadEnabled() { 253 return bulkheadEnabled; 254 } 255 256 /** 257 * Whether bulkhead is enabled or not on the circuit breaker. 258 * Default is false. 259 */ 260 public void setBulkheadEnabled(String bulkheadEnabled) { 261 this.bulkheadEnabled = bulkheadEnabled; 262 } 263 264 public String getBulkheadMaxConcurrentCalls() { 265 return bulkheadMaxConcurrentCalls; 266 } 267 268 /** 269 * Configures the max amount of concurrent calls the bulkhead will support. 270 */ 271 public void setBulkheadMaxConcurrentCalls(String bulkheadMaxConcurrentCalls) { 272 this.bulkheadMaxConcurrentCalls = bulkheadMaxConcurrentCalls; 273 } 274 275 public String getBulkheadMaxWaitDuration() { 276 return bulkheadMaxWaitDuration; 277 } 278 279 /** 280 * Configures a maximum amount of time which the calling thread will wait to enter the bulkhead. If bulkhead has space available, entry 281 * is guaranteed and immediate. If bulkhead is full, calling threads will contest for space, if it becomes available. maxWaitDuration can be set to 0. 282 * <p> 283 * Note: for threads running on an event-loop or equivalent (rx computation pool, etc), setting maxWaitDuration to 0 is highly recommended. Blocking 284 * an event-loop thread will most likely have a negative effect on application throughput. 285 */ 286 public void setBulkheadMaxWaitDuration(String bulkheadMaxWaitDuration) { 287 this.bulkheadMaxWaitDuration = bulkheadMaxWaitDuration; 288 } 289 290 public String getTimeoutEnabled() { 291 return timeoutEnabled; 292 } 293 294 /** 295 * Whether timeout is enabled or not on the circuit breaker. 296 * Default is false. 297 */ 298 public void setTimeoutEnabled(String timeoutEnabled) { 299 this.timeoutEnabled = timeoutEnabled; 300 } 301 302 public String getTimeoutExecutorServiceRef() { 303 return timeoutExecutorServiceRef; 304 } 305 306 /** 307 * References to a custom thread pool to use when timeout is enabled (uses {@link ForkJoinPool#commonPool()} by default) 308 */ 309 public void setTimeoutExecutorServiceRef(String timeoutExecutorServiceRef) { 310 this.timeoutExecutorServiceRef = timeoutExecutorServiceRef; 311 } 312 313 public String getTimeoutDuration() { 314 return timeoutDuration; 315 } 316 317 /** 318 * Configures the thread execution timeout. 319 * Default value is 1 second. 320 */ 321 public void setTimeoutDuration(String timeoutDuration) { 322 this.timeoutDuration = timeoutDuration; 323 } 324 325 public String getTimeoutCancelRunningFuture() { 326 return timeoutCancelRunningFuture; 327 } 328 329 /** 330 * Configures whether cancel is called on the running future. 331 * Defaults to true. 332 */ 333 public void setTimeoutCancelRunningFuture(String timeoutCancelRunningFuture) { 334 this.timeoutCancelRunningFuture = timeoutCancelRunningFuture; 335 } 336}