From c60e7f0e0ac2b7ae1371d0cf370bb996e425f841 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Fri, 15 Oct 2021 12:52:02 +0200 Subject: [PATCH] Cleanup LogGenerator We are getting flagged in SonarCloud for this example, clean it up. Change-Id: I9665e444857f6fb5b67a1bcb064d10b0ada8069b Signed-off-by: Robert Varga --- .../cluster/example/LogGenerator.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/LogGenerator.java b/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/LogGenerator.java index 96712eef41..6ef8a07d9d 100644 --- a/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/LogGenerator.java +++ b/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/LogGenerator.java @@ -5,7 +5,6 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.controller.cluster.example; import akka.actor.ActorRef; @@ -13,20 +12,24 @@ import java.util.HashMap; import java.util.Map; import java.util.Random; import org.opendaylight.controller.cluster.example.messages.KeyValue; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Created by kramesha on 7/16/14. */ public class LogGenerator { + private static final Logger LOG = LoggerFactory.getLogger(LogGenerator.class); + private final Map clientToLoggingThread = new HashMap<>(); - public void startLoggingForClient(ActorRef client) { + public void startLoggingForClient(final ActorRef client) { LoggingThread lt = new LoggingThread(client); clientToLoggingThread.put(client, lt); new Thread(lt).start(); } - public void stopLoggingForClient(ActorRef client) { + public void stopLoggingForClient(final ActorRef client) { clientToLoggingThread.get(client).stopLogging(); clientToLoggingThread.remove(client); } @@ -36,26 +39,26 @@ public class LogGenerator { private final ActorRef clientActor; private volatile boolean stopLogging = false; - public LoggingThread(ActorRef clientActor) { + public LoggingThread(final ActorRef clientActor) { this.clientActor = clientActor; } @Override - @SuppressWarnings("checkstyle:RegexpSingleLineJava") public void run() { Random random = new Random(); while (true) { if (stopLogging) { - System.out.println("Logging stopped for client:" + clientActor.path()); + LOG.info("Logging stopped for client: {}", clientActor.path()); break; } String key = clientActor.path().name(); int randomInt = random.nextInt(100); clientActor.tell(new KeyValue(key + "-key-" + randomInt, "value-" + randomInt), null); + try { Thread.sleep(randomInt % 10 * 1000L); } catch (InterruptedException e) { - e.printStackTrace(); + LOG.info("Interrupted while sleeping", e); } } } -- 2.36.6