Fix for issue #413: Circuit Breaker Pattern#986
Conversation
I see. I did build it locally but I use Java 8 and didn't run up against this error, and I didn't go through the Travis CI logs (😅 ). Any idea on why this could be happening? It does seem like this is a common problem on Java 11+ builds though, as written here, and I'll try to fix it as such. Do you have any other changes in mind? |
|
Actually, I see that many modules don't seem to use the javadocs plugin, and it's not in the parent pom, so is it necessary or should I remove that from the |
|
@PalAditya yes using that plugin is not mandatory so it is ok to remove |
|
I believe the jacoco issue was fixed in #996. Please merge master into this branch. |
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> |
There was a problem hiding this comment.
The plugins are already defined in parent pom.xml and not needed here
| MonitoringService obj = new MonitoringService(); | ||
| //Set the circuit Breaker parameters | ||
| CircuitBreaker circuitBreaker = new CircuitBreaker(3000, 1, 2000 * 1000 * 1000); | ||
| long serverStartTime = System.nanoTime(); |
There was a problem hiding this comment.
You can use local-variable type inference here (var)
| long timeout; | ||
| long retryTimePeriod; | ||
| long lastFailureTime; | ||
| int failureCount; | ||
| private final int failureThreshold; | ||
| private State state; |
There was a problem hiding this comment.
Use final where member variables don't change
| DelayedService delayedService = new DelayedService(20); | ||
| String response = delayedService.response(serverStartTime); |
There was a problem hiding this comment.
Use local-variable type inference (var)
| this.timeout = timeout; | ||
| this.retryTimePeriod = retryTimePeriod; | ||
| //An absurd amount of time in future which basically indicates the last failure never happened | ||
| this.lastFailureTime = System.nanoTime() + 1000 * 1000 * 1000 * 1000; |
There was a problem hiding this comment.
Could it be constant? Looks a bit cryptic
| */ | ||
| public String response(long serverStartTime) { | ||
| long currentTime = System.nanoTime(); | ||
| if ((currentTime - serverStartTime) * 1.0 / (1000 * 1000 * 1000) < delay) { |
There was a problem hiding this comment.
The calculation here is hard to understand. We need at least comment there.
| //Set time as current time as initially server fails | ||
| long serverStartTime = System.nanoTime(); | ||
| String response = monitoringService.remoteResourceResponse(circuitBreaker, serverStartTime); | ||
| assertEquals(response, "Remote service not responding"); |
There was a problem hiding this comment.
Also in tests var can be used extensively
|
Thanks for the feedback! I implemented the changes as requested, but the Sonar Integration seems to have broken something else. A local build with |
|
I wonder why the sonar analysis worked in my branch and master, but not here. To remedy this, I now tried to set the sonar host property in 47d92bb Could you merge from the master again so we can see if it helped? |
|
After merging, I get this. It seems like it's an permission issue on your end 😅 |
|
Funny thing: I decided to play around with the config file and made a temporary copy of the project on my SonarQube account, and guess what, it passed (with this minimal yml on another branch)! language: java
dist: bionic
jdk:
- openjdk11
sudo: required
services:
- xvfb
addons:
sonarcloud:
organization: "paladitya"
script:
# the following command line builds the project, runs the tests with coverage and then execute the SonarCloud analysis
- mvn clean verify sonar:sonar -Dsonar.projectKey=PalAditya_java-design-patterns -Dsonar.host.url=https://sonarcloud.io
notifications:
email:
- adityapal.nghss@gmail.com
webhooks:
on_success: change # options: [always|never|change] default: always
on_failure: always # options: [always|never|change] default: always
on_start: never # options: [always|never|change] default: alwaysI can only guess that either the encrypted key is not being read properly for PRs (I actually set the value (Sorry if my guesses are unhelpful, but I have never worked with SonarQube before, and I find this prospect of a bug/misconfiguration quite interesting 😅 ) |
|
https://docs.travis-ci.com/user/sonarcloud/#analysis-of-internal-pull-requests |
I see. So, what should be done next from my end? 🤔 |
iluwatar
left a comment
There was a problem hiding this comment.
The code in README.md explanation is not in sync with the implementation
Oh, do you mean that I should change them to use var and final too or that I shouldn't remove the comments that are in the actual code? I am going to assume the former since I don't see a heavy usage of comments in the other READMEs and I have written the explanation too. Plus, they could always see the actual code for full understanding. |
|
@PalAditya can you please merge master again? It shouldn't run the sonar analysis for this PR now, if everything goes fine. |
|
@iluwatar This seems to have done the trick 😄 |
|
Wonderful, thanks for your help @PalAditya 👍 |
|
Thanks for the great pattern @PalAditya, well done |

Fixes issue #413 by adding an implementation of circuit breaker pattern
An example output:
Code Coverage Report:

Please mention whether something else needs to be done for this PR 😄