Skip to content

Commit 3156d06

Browse files
committed
Remove busy waiting in ServiceExecutor and improve message processing
1 parent 5b89975 commit 3156d06

10 files changed

Lines changed: 536 additions & 510 deletions

File tree

commander/src/main/java/com/iluwatar/commander/Retry.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import java.util.ArrayList;
2929
import java.util.Arrays;
3030
import java.util.List;
31+
import java.util.concurrent.Executors;
32+
import java.util.concurrent.ScheduledExecutorService;
33+
import java.util.concurrent.TimeUnit;
3134
import java.util.concurrent.atomic.AtomicInteger;
3235
import java.util.function.Predicate;
3336

@@ -53,6 +56,7 @@ public interface HandleErrorIssue<T> {
5356
}
5457

5558
private static final SecureRandom RANDOM = new SecureRandom();
59+
private static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
5660

5761
private final Operation op;
5862
private final HandleErrorIssue<T> handleError;
@@ -98,9 +102,23 @@ public void perform(List<Exception> list, T obj) {
98102
long testDelay =
99103
(long) Math.pow(2, this.attempts.intValue()) * 1000 + RANDOM.nextInt(1000);
100104
long delay = Math.min(testDelay, this.maxDelay);
101-
Thread.sleep(delay);
102-
} catch (InterruptedException f) {
103-
// ignore
105+
scheduler.schedule(() -> perform(list, obj), delay, TimeUnit.MILLISECONDS);
106+
return;
107+
} catch (Exception j) {
108+
this.errors.add(j);
109+
110+
if (this.attempts.incrementAndGet() >= this.maxAttempts || !this.test.test(j)) {
111+
this.handleError.handleIssue(obj, j);
112+
return;
113+
}
114+
115+
long testDelay =
116+
(long) Math.pow(2, this.attempts.intValue()) * 1000 + RANDOM.nextInt(1000);
117+
118+
long delay = Math.min(testDelay, this.maxDelay);
119+
120+
scheduler.schedule(() -> perform(list, obj), delay, TimeUnit.MILLISECONDS);
121+
return;
104122
}
105123
}
106124
} while (true);

microservices-log-aggregation/src/main/java/com/iluwatar/logaggregation/LogAggregator.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
package com.iluwatar.logaggregation;
2626

2727
import java.util.concurrent.ConcurrentLinkedQueue;
28-
import java.util.concurrent.ExecutorService;
28+
import java.util.concurrent.ScheduledExecutorService;
2929
import java.util.concurrent.Executors;
3030
import java.util.concurrent.TimeUnit;
3131
import java.util.concurrent.atomic.AtomicInteger;
@@ -44,7 +44,7 @@ public class LogAggregator {
4444
private final CentralLogStore centralLogStore;
4545
private final ConcurrentLinkedQueue<LogEntry> buffer = new ConcurrentLinkedQueue<>();
4646
private final LogLevel minLogLevel;
47-
private final ExecutorService executorService = Executors.newSingleThreadExecutor();
47+
private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
4848
private final AtomicInteger logCount = new AtomicInteger(0);
4949

5050
/**
@@ -104,16 +104,11 @@ private void flushBuffer() {
104104
}
105105

106106
private void startBufferFlusher() {
107-
executorService.execute(
108-
() -> {
109-
while (!Thread.currentThread().isInterrupted()) {
110-
try {
111-
Thread.sleep(5000); // Flush every 5 seconds.
112-
flushBuffer();
113-
} catch (InterruptedException e) {
114-
Thread.currentThread().interrupt();
115-
}
116-
}
117-
});
107+
executorService.scheduleAtFixedRate(
108+
this::flushBuffer,
109+
5,
110+
5,
111+
TimeUnit.SECONDS
112+
);
118113
}
119114
}

microservices-self-registration/eurekaserver/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@
4949
<groupId>org.apache.maven.plugins</groupId>
5050
<artifactId>maven-compiler-plugin</artifactId>
5151
</plugin>
52+
<plugin>
53+
<groupId>com.diffplug.spotless</groupId>
54+
<artifactId>spotless-maven-plugin</artifactId>
55+
<version>2.44.4</version>
56+
<configuration>
57+
<java>
58+
<googleJavaFormat>
59+
<version>1.17.0</version>
60+
</googleJavaFormat>
61+
</java>
62+
</configuration>
63+
</plugin>
5264
</plugins>
5365
</build>
5466

microservices-self-registration/eurekaserver/src/main/java/com/learning/eurekaserver/EurekaserverApplication.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
@EnableEurekaServer
99
public class EurekaserverApplication {
1010

11-
public static void main(String[] args) {
12-
SpringApplication.run(EurekaserverApplication.class, args);
13-
}
14-
11+
public static void main(String[] args) {
12+
SpringApplication.run(EurekaserverApplication.class, args);
13+
}
1514
}

microservices-self-registration/eurekaserver/src/test/java/com/learning/eurekaserver/EurekaserverApplicationTests.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
@SpringBootTest
77
class EurekaserverApplicationTests {
88

9-
@Test
10-
void contextLoads() {
11-
// This is a basic integration test that checks if the Spring Application Context loads successfully.
9+
@Test
10+
void contextLoads() {
11+
// This is a basic integration test that checks if the Spring Application Context loads
12+
// successfully.
1213
// If the context loads without any exceptions, the test is considered passing.
1314
// It is often left empty as the act of loading the context is the primary verification.
14-
// You can add specific assertions here if you want to verify the presence or state of certain beans.
15-
}
16-
15+
// You can add specific assertions here if you want to verify the presence or state of certain
16+
// beans.
17+
}
1718
}

0 commit comments

Comments
 (0)