Skip to content

Commit a45ae6b

Browse files
committed
Remove busy-waiting from ServiceExecutor
1 parent ede37bd commit a45ae6b

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

queue-based-load-leveling/src/main/java/com/iluwatar/queue/load/leveling/ServiceExecutor.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,17 @@ public ServiceExecutor(MessageQueue msgQueue) {
4040
}
4141

4242
/** The ServiceExecutor thread will retrieve each message and process it. */
43+
@Override
44+
4345
public void run() {
4446
try {
4547
while (!Thread.currentThread().isInterrupted()) {
46-
var msg = msgQueue.retrieveMsg();
47-
48-
if (null != msg) {
49-
LOGGER.info(msg + " is served.");
50-
} else {
51-
LOGGER.info("Service Executor: Waiting for Messages to serve .. ");
52-
}
53-
54-
Thread.sleep(1000);
48+
var msg = msgQueue.retrieveMsg(); // blocks internally
49+
LOGGER.info(msg + " is served.");
5550
}
5651
} catch (Exception e) {
57-
LOGGER.error(e.getMessage());
52+
Thread.currentThread().interrupt();
53+
LOGGER.error("Error while processing message", e);
5854
}
5955
}
6056
}

0 commit comments

Comments
 (0)