Skip to content

Instantly share code, notes, and snippets.

@skaveesh
Last active January 3, 2021 04:37
Show Gist options
  • Save skaveesh/41c9006bd6a27e90b0e6fd451e8e368b to your computer and use it in GitHub Desktop.
Save skaveesh/41c9006bd6a27e90b0e6fd451e8e368b to your computer and use it in GitHub Desktop.
How I Decoupled Circuit Breaker from the Code with AOP in Spring Boot for Better Code Maintenance
//code is omitted for brevity
@Service
public class DownstreamService {
//code is omitted for brevity
/**
* Response from this API call will not have a delay
*
* @return the json object
*/
@EnableCircuitBreakerScan
public JSONObject callTest() {
JSONObject result;
try {
String serviceUrl = "http://localhost:3000/test";
RestTemplate restTemplate = new RestTemplate();
result = restTemplate.getForObject(serviceUrl, JSONObject.class);
} catch (HttpClientErrorException ex) {
throw new RuntimeException("external API error");
} catch (Exception ex) {
throw new RuntimeException("server error");
}
JSONObject payloadJSONObject = new JSONObject();
payloadJSONObject.putAll(result);
return payloadJSONObject;
}
/**
* Response from this API call will have over 2 seconds of a delay
*
* @return the json object
*/
@EnableCircuitBreakerScan
public JSONObject callTestSlow() {
//code is omitted for brevity
}
/**
* Response from this API call will have an error
*
* @return the json object
*/
@EnableCircuitBreakerScan
public JSONObject callTestError() {
//code is omitted for brevity
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment