2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.example;
10 import akka.actor.ActorRef;
11 import java.util.HashMap;
13 import java.util.Random;
14 import org.opendaylight.controller.cluster.example.messages.KeyValue;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
19 * Created by kramesha on 7/16/14.
21 public class LogGenerator {
22 private static final Logger LOG = LoggerFactory.getLogger(LogGenerator.class);
24 private final Map<ActorRef, LoggingThread> clientToLoggingThread = new HashMap<>();
26 public void startLoggingForClient(final ActorRef client) {
27 LoggingThread lt = new LoggingThread(client);
28 clientToLoggingThread.put(client, lt);
29 new Thread(lt).start();
32 public void stopLoggingForClient(final ActorRef client) {
33 clientToLoggingThread.get(client).stopLogging();
34 clientToLoggingThread.remove(client);
37 public static class LoggingThread implements Runnable {
38 private final Random random = new Random();
39 private final ActorRef clientActor;
41 private volatile boolean stopLogging = false;
43 public LoggingThread(final ActorRef clientActor) {
44 this.clientActor = clientActor;
51 LOG.info("Logging stopped for client: {}", clientActor.path());
54 String key = clientActor.path().name();
55 int randomInt = random.nextInt(100);
56 clientActor.tell(new KeyValue(key + "-key-" + randomInt, "value-" + randomInt), null);
59 Thread.sleep(randomInt % 10 * 1000L);
60 } catch (InterruptedException e) {
61 LOG.info("Interrupted while sleeping", e);
66 public void stopLogging() {
70 public void startLogging() {