diff --git a/src/main/java/org/springframework/samples/petclinic/errors/MonitorService.java b/src/main/java/org/springframework/samples/petclinic/errors/MonitorService.java index af0ad68a219..2d209472137 100644 --- a/src/main/java/org/springframework/samples/petclinic/errors/MonitorService.java +++ b/src/main/java/org/springframework/samples/petclinic/errors/MonitorService.java @@ -3,75 +3,78 @@ import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.StatusCode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.SmartLifecycle; import org.springframework.stereotype.Component; -import java.util.InvalidPropertiesFormatException; - @Component public class MonitorService implements SmartLifecycle { - private boolean running = false; - private Thread backgroundThread; - @Autowired - private OpenTelemetry openTelemetry; - - @Override - public void start() { - var otelTracer = openTelemetry.getTracer("MonitorService"); - - running = true; - backgroundThread = new Thread(() -> { - while (running) { - - try { - Thread.sleep(5000); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - Span span = otelTracer.spanBuilder("monitor").startSpan(); - - try { + private static final Logger logger = LoggerFactory.getLogger(MonitorService.class); + private volatile boolean running = false; + private Thread backgroundThread; + @Autowired + private OpenTelemetry openTelemetry; - System.out.println("Background service is running..."); - monitor(); - } catch (Exception e) { - span.recordException(e); - span.setStatus(StatusCode.ERROR); - } finally { - span.end(); - } - } - }); + @Override + public void start() { + var otelTracer = openTelemetry.getTracer("MonitorService"); - // Start the background thread - backgroundThread.start(); - System.out.println("Background service started."); - } + running = true; + backgroundThread = new Thread(() -> { + while (running) { + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + logger.warn("Monitor thread interrupted", e); + Thread.currentThread().interrupt(); + break; + } + Span span = otelTracer.spanBuilder("monitor").startSpan(); - private void monitor() throws InvalidPropertiesFormatException { - Utils.throwException(IllegalStateException.class,"monitor failure"); - } + try { + logger.info("Background service is running..."); + monitor(); + } catch (Exception e) { + logger.error("Error in monitor service", e); + span.recordException(e); + span.setStatus(StatusCode.ERROR); + } finally { + span.end(); + } + } + }); + backgroundThread.start(); + logger.info("Background service started."); + } + private void monitor() { + if (!running) { + logger.warn("Monitor called while service is not running"); + return; + } + logger.debug("Performing monitoring check"); + } - @Override - public void stop() { - // Stop the background task - running = false; - if (backgroundThread != null) { - try { - backgroundThread.join(); // Wait for the thread to finish - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - System.out.println("Background service stopped."); - } + @Override + public void stop() { + running = false; + if (backgroundThread != null) { + try { + backgroundThread.join(); + } catch (InterruptedException e) { + logger.warn("Interrupted while stopping background thread", e); + Thread.currentThread().interrupt(); + } + } + logger.info("Background service stopped."); + } - @Override - public boolean isRunning() { - return false; - } -} + @Override + public boolean isRunning() { + return running; + } +} \ No newline at end of file