/** * Create a new {@link CircuitBreaker} and map it to the provided name. * If the CircuitBreaker already exists, then the existing instance is * returned. * @param name the name of the {@link CircuitBreaker} * @param config the {@link CircuitBreakerConfig} with the configuration * values. * @return the created {@link CircuitBreaker} */ public synchronized CircuitBreaker createCircuitBreaker(String name, CircuitBreakerConfig config) { CircuitBreaker circuitBreaker = findCircuitBreaker(name); if (circuitBreaker == null) { circuitBreaker = new CircuitBreaker(name); configureCircuitBreaker(name, circuitBreaker, config); addCircuitBreakerToMap(name, circuitBreaker); } return circuitBreaker; }
/** Default constructor. */ public CircuitBreakerAspect() { circuitBreakerFactory = new CircuitBreakerFactory(); }
/** * Get an integer property override value. * @param name the {@link CircuitBreaker} name. * @param key the property override key. * @return the property override value, or null if it is not found. */ private Integer getIntegerPropertyOverrideValue(String name, String key) { if (properties != null) { String propertyName = getPropertyName(name, key); String propertyOverrideValue = properties.getProperty(propertyName); if (propertyOverrideValue != null) { try { return Integer.parseInt(propertyOverrideValue); } catch (NumberFormatException e) { logger.error("Could not parse property override key={}, value={}", key, propertyOverrideValue); } } } return null; }
circuitBreakerFactory.findCircuitBreaker(name); circuitBreakerFactory.createCircuitBreaker(name, config);
protected void configureCircuitBreaker(String name, CircuitBreaker circuit, CircuitBreakerConfig config) { long resetMillis = config.getResetMillis(); Long resetMillisOverride = getLongPropertyOverrideValue(name, RESETMILLIS_KEY); if (resetMillisOverride != null) { resetMillis = resetMillisOverride; } FailureInterpreter fi = config.getFailureInterpreter(); circuit.setFailureInterpreter(fi); if (resetMillis > 0) { circuit.setResetMillis(resetMillis); } if (fi instanceof DefaultFailureInterpreter) { configureDefaultFailureInterpreter(name, resetMillis, circuit); } else { logger.info( "Created CircuitBreaker '{}', resetMillis={}", new Object[] { name, resetMillis }); } }
private void configureDefaultFailureInterpreter(String name, long resetMillis, CircuitBreaker circuit) { DefaultFailureInterpreter fi = (DefaultFailureInterpreter) circuit.getFailureInterpreter(); Integer limitOverride = getIntegerPropertyOverrideValue(name, LIMIT_KEY); if (limitOverride != null) { fi.setLimit(limitOverride); } Long windowMillisOverride = getLongPropertyOverrideValue(name, WINDOWMILLIS_KEY); if (windowMillisOverride != null) { fi.setWindowMillis(windowMillisOverride); } logger.info( "Created CircuitBreaker '{}', limit={}, windowMillis={}, resetMillis={}", new Object[] { name, fi.getLimit(), fi.getWindowMillis(), resetMillis }); }
/** * Get an {@link Long} property override value. * @param name the {@link CircuitBreaker} name. * @param key the property override key. * @return the property override value, or null if it is not found. */ private Long getLongPropertyOverrideValue(String name, String key) { if (properties != null) { String propertyName = getPropertyName(name, key); String propertyOverrideValue = properties.getProperty(propertyName); if (propertyOverrideValue != null) { try { return Long.parseLong(propertyOverrideValue); } catch (NumberFormatException e) { logger.error("Could not parse property override key={}, value={}", key, propertyOverrideValue); } } } return null; } }