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 javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlRootElement;
022import javax.xml.bind.annotation.XmlTransient;
023
024import org.apache.camel.spi.Configurer;
025import org.apache.camel.spi.Metadata;
026
027/**
028 * MicroProfile Fault Tolerance Circuit Breaker EIP configuration
029 */
030@Metadata(label = "eip,routing,circuitbreaker")
031@XmlRootElement(name = "faultToleranceConfiguration")
032@XmlAccessorType(XmlAccessType.FIELD)
033@Configurer
034public class FaultToleranceConfigurationDefinition extends FaultToleranceConfigurationCommon {
035
036    @XmlTransient
037    private CircuitBreakerDefinition parent;
038
039    public FaultToleranceConfigurationDefinition() {
040    }
041
042    public FaultToleranceConfigurationDefinition(CircuitBreakerDefinition parent) {
043        this.parent = parent;
044    }
045
046    // Fluent API
047    // -------------------------------------------------------------------------
048
049    /**
050     * Refers to an existing io.github.resilience4j.circuitbreaker.CircuitBreaker instance
051     * to lookup and use from the registry. When using this, then any other circuit breaker options
052     * are not in use.
053     */
054    public FaultToleranceConfigurationDefinition circuitBreakerRef(String circuitBreakerRef) {
055        setCircuitBreakerRef(circuitBreakerRef);
056        return this;
057    }
058
059    /**
060     * Control how long the circuit breaker stays open. The value are in seconds and the default is 60 seconds.
061     */
062    public FaultToleranceConfigurationDefinition delay(long delay) {
063        setDelay(Long.toString(delay));
064        return this;
065    }
066
067    /**
068     * Controls the number of trial calls which are allowed when the circuit breaker is half-open
069     */
070    public FaultToleranceConfigurationDefinition successThreshold(int successThreshold) {
071        setSuccessThreshold(Integer.toString(successThreshold));
072        return this;
073    }
074
075    /**
076     * Controls the size of the rolling window used when the circuit breaker is closed
077     */
078    public FaultToleranceConfigurationDefinition requestVolumeThreshold(int requestVolumeThreshold) {
079        setRequestVolumeThreshold(Integer.toString(requestVolumeThreshold));
080        return this;
081    }
082
083    /**
084     * Configures the failure rate threshold in percentage.
085     * If the failure rate is equal or greater than the threshold the CircuitBreaker transitions to open and starts short-circuiting calls.
086     * <p>
087     * The threshold must be greater than 0 and not greater than 100. Default value is 50 percentage.
088     */
089    public FaultToleranceConfigurationDefinition failureRatio(int failureRatio) {
090        setFailureRatio(Integer.toString(failureRatio));
091        return this;
092    }
093
094    /**
095     * Whether timeout is enabled or not on the circuit breaker.
096     * Default is false.
097     */
098    public FaultToleranceConfigurationDefinition timeoutEnabled(boolean timeoutEnabled) {
099        setTimeoutEnabled(Boolean.toString(timeoutEnabled));
100        return this;
101    }
102
103    /**
104     * Configures the thread execution timeout (millis).
105     * Default value is 1000 millis (1 second).
106     */
107    public FaultToleranceConfigurationDefinition timeoutDuration(long timeoutDuration) {
108        setTimeoutDuration(Long.toString(timeoutDuration));
109        return this;
110    }
111
112    /**
113     * Configures the pool size of the thread pool when timeout is enabled.
114     * Default value is 10.
115     */
116    public FaultToleranceConfigurationDefinition timeoutPoolSize(int poolSize) {
117        setTimeoutPoolSize(Integer.toString(poolSize));
118        return this;
119    }
120
121    /**
122     * References to a custom thread pool to use when timeout is enabled
123     */
124    public FaultToleranceConfigurationDefinition timeoutScheduledExecutorServiceRef(String executorServiceRef) {
125        setTimeoutScheduledExecutorServiceRef(executorServiceRef);
126        return this;
127    }
128
129    /**
130     * Whether bulkhead is enabled or not on the circuit breaker.
131     * Default is false.
132     */
133    public FaultToleranceConfigurationDefinition bulkheadEnabled(boolean bulkheadEnabled) {
134        setBulkheadEnabled(Boolean.toString(bulkheadEnabled));
135        return this;
136    }
137
138    /**
139     * Configures the max amount of concurrent calls the bulkhead will support.
140     */
141    public FaultToleranceConfigurationDefinition bulkheadMaxConcurrentCalls(int bulkheadMaxConcurrentCalls) {
142        setBulkheadMaxConcurrentCalls(Integer.toString(bulkheadMaxConcurrentCalls));
143        return this;
144    }
145
146    /**
147     * Configures the task queue size for holding waiting tasks to be processed by the bulkhead
148     */
149    public FaultToleranceConfigurationDefinition bulkheadWaitingTaskQueue(int bulkheadWaitingTaskQueue) {
150        setBulkheadWaitingTaskQueue(Integer.toString(bulkheadWaitingTaskQueue));
151        return this;
152    }
153
154    /**
155     * References to a custom thread pool to use when bulkhead is enabled
156     */
157    public FaultToleranceConfigurationDefinition bulkheadExecutorServiceRef(String executorServiceRef) {
158        setBulkheadExecutorServiceRef(executorServiceRef);
159        return this;
160    }
161
162    /**
163     * End of configuration.
164     */
165    public CircuitBreakerDefinition end() {
166        return parent;
167    }
168
169}