Skip to content

Commit 447d5df

Browse files
committed
fixing maven format in logaggregator2
1 parent e9deb6e commit 447d5df

File tree

2 files changed

+53
-58
lines changed

2 files changed

+53
-58
lines changed

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

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
* THE SOFTWARE.
2424
*/
2525
package com.iluwatar.logaggregation;
26+
import java.util.ArrayList;
27+
import java.util.List;
2628
import java.util.concurrent.BlockingQueue;
29+
import java.util.concurrent.CountDownLatch;
30+
import java.util.concurrent.Executors;
2731
import java.util.concurrent.LinkedBlockingQueue;
2832
import java.util.concurrent.ScheduledExecutorService;
29-
import java.util.concurrent.Executors;
3033
import java.util.concurrent.TimeUnit;
3134
import java.util.concurrent.atomic.AtomicInteger;
32-
import java.util.concurrent.CountDownLatch;
33-
import java.util.ArrayList;
34-
import java.util.List;
3535
import lombok.extern.slf4j.Slf4j;
3636
/**
3737
* Responsible for collecting and buffering logs from different services. Once the logs reach a
@@ -45,47 +45,51 @@ public class LogAggregator {
4545
private static final int BUFFER_THRESHOLD = 3;
4646
private static final int FLUSH_INTERVAL_SECONDS = 5;
4747
private static final int SHUTDOWN_TIMEOUT_SECONDS = 10;
48-
48+
4949
private final CentralLogStore centralLogStore;
5050
private final BlockingQueue<LogEntry> buffer = new LinkedBlockingQueue<>();
5151
private final LogLevel minLogLevel;
5252
private final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(1);
5353
private final AtomicInteger logCount = new AtomicInteger(0);
5454
private final CountDownLatch shutdownLatch = new CountDownLatch(1);
5555
private volatile boolean running = true;
56+
5657
/**
5758
* constructor of LogAggregator.
5859
*
5960
* @param centralLogStore central log store implement
6061
* @param minLogLevel min log level to store log
6162
*/
6263
public LogAggregator(CentralLogStore centralLogStore, LogLevel minLogLevel) {
63-
this.centralLogStore = centralLogStore;
64+
this.centralLogStore = centralLogStore;
6465
this.minLogLevel = minLogLevel;
6566
startPeriodicFlusher();
66-
67+
6768
// Add shutdown hook for graceful termination
68-
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
69-
try {
70-
stop();
71-
} catch (InterruptedException e) {
72-
LOGGER.warn("Shutdown interrupted", e);
73-
Thread.currentThread().interrupt();
74-
}
75-
}));
69+
Runtime.getRuntime()
70+
.addShutdownHook(
71+
new Thread(
72+
() -> {
73+
try {
74+
stop();
75+
} catch (InterruptedException e) {
76+
LOGGER.warn("Shutdown interrupted", e);
77+
Thread.currentThread().interrupt();
78+
}
79+
}));
7680
}
7781

7882
/**
7983
* Collects a given log entry, and filters it by the defined log level.
8084
*
8185
* @param logEntry The log entry to collect.
8286
*/
83-
public void collectLog(LogEntry logEntry) {
87+
public void collectLog(LogEntry logEntry) {
8488
if (!running) {
8589
LOGGER.warn("LogAggregator is shutting down. Skipping log entry.");
8690
return;
8791
}
88-
92+
8993
if (logEntry.getLevel() == null || minLogLevel == null) {
9094
LOGGER.warn("Log level or threshold level is null. Skipping.");
9195
return;
@@ -115,19 +119,19 @@ public void collectLog(LogEntry logEntry) {
115119
*
116120
* @throws InterruptedException If any thread has interrupted the current thread.
117121
*/
118-
public void stop() throws InterruptedException {
122+
public void stop() throws InterruptedException {
119123
LOGGER.info("Stopping LogAggregator...");
120124
running = false;
121-
125+
122126
// Shutdown the scheduler gracefully
123127
scheduledExecutor.shutdown();
124-
128+
125129
try {
126130
// Wait for scheduled tasks to complete
127131
if (!scheduledExecutor.awaitTermination(SHUTDOWN_TIMEOUT_SECONDS, TimeUnit.SECONDS)) {
128132
LOGGER.warn("Scheduler did not terminate gracefully, forcing shutdown");
129133
scheduledExecutor.shutdownNow();
130-
134+
131135
// Wait a bit more for tasks to respond to interruption
132136
if (!scheduledExecutor.awaitTermination(2, TimeUnit.SECONDS)) {
133137
LOGGER.error("Scheduler did not terminate after forced shutdown");
@@ -141,57 +145,53 @@ public void stop() throws InterruptedException {
141145
}
142146
}
143147

144-
145-
146148
/**
147-
* Waits for the LogAggregator to complete shutdown.
148-
* Useful for testing or controlled shutdown scenarios.
149+
* Waits for the LogAggregator to complete shutdown. Useful for testing or controlled shutdown
150+
* scenarios.
149151
*
150152
* @throws InterruptedException If any thread has interrupted the current thread.
151153
*/
152-
public void awaitShutdown() throws InterruptedException {
154+
public void awaitShutdown() throws InterruptedException {
153155
shutdownLatch.await();
154156
}
155157

156-
157158
private void flushBuffer() {
158159
if (!running && buffer.isEmpty()) {
159160
return;
160161
}
161-
162+
162163
try {
163164
List<LogEntry> batch = new ArrayList<>();
164165
int drained = 0;
165-
166+
166167
// Drain up to a reasonable batch size for efficiency
167168
LogEntry logEntry;
168169
while ((logEntry = buffer.poll()) != null && drained < 100) {
169170
batch.add(logEntry);
170171
drained++;
171172
}
172-
173+
173174
if (!batch.isEmpty()) {
174175
LOGGER.debug("Flushing {} log entries to central store", batch.size());
175-
176+
176177
// Process the batch
177178
for (LogEntry entry : batch) {
178179
centralLogStore.storeLog(entry);
179180
logCount.decrementAndGet();
180181
}
181-
182+
182183
LOGGER.debug("Successfully flushed {} log entries", batch.size());
183184
}
184185
} catch (Exception e) {
185186
LOGGER.error("Error occurred while flushing buffer", e);
186187
}
187188
}
188189

189-
190190
/**
191-
* Starts the periodic buffer flusher using ScheduledExecutorService.
192-
* This eliminates the busy-waiting loop with Thread.sleep().
191+
* Starts the periodic buffer flusher using ScheduledExecutorService. This eliminates the
192+
* busy-waiting loop with Thread.sleep().
193193
*/
194-
private void startPeriodicFlusher() {
194+
private void startPeriodicFlusher() {
195195
scheduledExecutor.scheduleAtFixedRate(
196196
() -> {
197197
if (running) {
@@ -202,16 +202,16 @@ private void startPeriodicFlusher() {
202202
}
203203
}
204204
},
205-
FLUSH_INTERVAL_SECONDS, // Initial delay
206-
FLUSH_INTERVAL_SECONDS, // Period
207-
TimeUnit.SECONDS
208-
);
209-
210-
LOGGER.info("Periodic log flusher started with interval of {} seconds", FLUSH_INTERVAL_SECONDS);
205+
FLUSH_INTERVAL_SECONDS, // Initial delay
206+
FLUSH_INTERVAL_SECONDS, // Period
207+
TimeUnit.SECONDS);
208+
209+
LOGGER.info(
210+
"Periodic log flusher started with interval of {} seconds", FLUSH_INTERVAL_SECONDS);
211211
}
212+
212213
/**
213-
* Gets the current number of buffered log entries.
214-
* Useful for monitoring and testing.
214+
* Gets the current number of buffered log entries. Useful for monitoring and testing.
215215
*
216216
* @return Current buffer size
217217
*/
@@ -220,8 +220,7 @@ public int getBufferSize() {
220220
}
221221

222222
/**
223-
* Gets the current log count.
224-
* Useful for monitoring and testing.
223+
* Gets the current log count. Useful for monitoring and testing.
225224
*
226225
* @return Current log count
227226
*/

server-session/src/test/java/com/iluwatar/sessionserver/LoginHandlerTest.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,20 @@
2222
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
* THE SOFTWARE.
2424
*/
25-
package com.iluwatar.sessionserver;
25+
package com.iluwatar.logaggregation;
2626

27-
import static org.junit.jupiter.api.Assertions.assertEquals;
28-
import static org.mockito.Mockito.when;
27+
import static org.mockito.ArgumentMatchers.any;
28+
import static org.mockito.Mockito.times;
29+
import static org.mockito.Mockito.verify;
2930

30-
import com.sun.net.httpserver.Headers;
31-
import com.sun.net.httpserver.HttpExchange;
32-
import java.io.ByteArrayOutputStream;
33-
import java.time.Instant;
34-
import java.util.HashMap;
35-
import java.util.Map;
31+
import java.time.LocalDateTime;
3632
import org.junit.jupiter.api.BeforeEach;
3733
import org.junit.jupiter.api.Test;
34+
import org.junit.jupiter.api.extension.ExtendWith;
3835
import org.mockito.Mock;
39-
import org.mockito.MockitoAnnotations;
36+
import org.mockito.junit.jupiter.MockitoExtension;
4037

41-
/** LoginHandlerTest. */
38+
@ExtendWith(MockitoExtension.class)
4239
public class LoginHandlerTest {
4340

4441
private LoginHandler loginHandler;
@@ -47,7 +44,6 @@ public class LoginHandlerTest {
4744
private Map<String, Instant> sessionCreationTimes;
4845

4946
@Mock private HttpExchange exchange;
50-
5147
/** Setup tests. */
5248
@BeforeEach
5349
public void setUp() {

0 commit comments

Comments
 (0)