X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft-example%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fexample%2FExampleRoleChangeListener.java;h=6b4c17addc4b202cf262f6bc30999158e7d606f7;hp=1676a41c56d7f9c2cfe81a7fc9e46c6828c8d721;hb=614e6974b6e79c0eb21f4b114139ad5d07e5c96c;hpb=608760751ce7fcf4e84e86a8b33d43bc1d9984d6 diff --git a/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/ExampleRoleChangeListener.java b/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/ExampleRoleChangeListener.java index 1676a41c56..6b4c17addc 100644 --- a/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/ExampleRoleChangeListener.java +++ b/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/ExampleRoleChangeListener.java @@ -1,8 +1,16 @@ +/* + * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * 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; import akka.actor.Cancellable; import akka.actor.Props; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -15,31 +23,32 @@ import scala.concurrent.Await; import scala.concurrent.duration.FiniteDuration; /** - * This is a sample implementation of a Role Change Listener which is an actor, which registers itself to the ClusterRoleChangeNotifier - *

+ * This is a sample implementation of a Role Change Listener which is an actor, which registers itself + * to the ClusterRoleChangeNotifier. + * + *

* The Role Change listener receives a SetNotifiers message with the notifiers to register itself with. - *

- * It kicks of a scheduler which sents registration messages to the notifiers, till it gets a RegisterRoleChangeListenerReply - *

+ * + *

+ * It kicks of a scheduler which sends registration messages to the notifiers, till it gets a + * RegisterRoleChangeListenerReply. + * + *

* If all the notifiers have been regsitered with, then it cancels the scheduler. * It starts the scheduler again when it receives a new registration - * */ -public class ExampleRoleChangeListener extends AbstractUntypedActor implements AutoCloseable{ +public class ExampleRoleChangeListener extends AbstractUntypedActor implements AutoCloseable { // the akka url should be set to the notifiers actor-system and domain. - private static final String NOTIFIER_AKKA_URL = "akka.tcp://raft-test@127.0.0.1:2550/user/"; + private static final String NOTIFIER_AKKA_URL = "akka://raft-test@127.0.0.1:2550/user/"; - private Map notifierRegistrationStatus = new HashMap<>(); + private final Map notifierRegistrationStatus = new HashMap<>(); private Cancellable registrationSchedule = null; - private static final FiniteDuration duration = new FiniteDuration(100, TimeUnit.MILLISECONDS); - private static final FiniteDuration schedulerDuration = new FiniteDuration(1, TimeUnit.SECONDS); - private final String memberName; - private static final String[] shardsToMonitor = new String[] {"example"}; - - public ExampleRoleChangeListener(String memberName) { - super(); - scheduleRegistrationListener(schedulerDuration); - this.memberName = memberName; + private static final FiniteDuration DURATION = new FiniteDuration(100, TimeUnit.MILLISECONDS); + private static final FiniteDuration SCHEDULER_DURATION = new FiniteDuration(1, TimeUnit.SECONDS); + private static final String[] SHARDS_TO_MONITOR = new String[] {"example"}; + + public ExampleRoleChangeListener(final String memberName) { + scheduleRegistrationListener(SCHEDULER_DURATION); populateRegistry(memberName); } @@ -48,7 +57,7 @@ public class ExampleRoleChangeListener extends AbstractUntypedActor implements A } @Override - protected void handleReceive(Object message) throws Exception { + protected void handleReceive(final Object message) { if (message instanceof RegisterListener) { // called by the scheduler at intervals to register any unregistered notifiers sendRegistrationRequests(); @@ -70,7 +79,7 @@ public class ExampleRoleChangeListener extends AbstractUntypedActor implements A } } - private void scheduleRegistrationListener(FiniteDuration interval) { + private void scheduleRegistrationListener(final FiniteDuration interval) { LOG.debug("--->scheduleRegistrationListener called."); registrationSchedule = getContext().system().scheduler().schedule( interval, interval, getSelf(), new RegisterListener(), @@ -78,40 +87,39 @@ public class ExampleRoleChangeListener extends AbstractUntypedActor implements A } - private void populateRegistry(String memberName) { - - for (String shard: shardsToMonitor) { - String notifier =(new StringBuilder()).append(NOTIFIER_AKKA_URL).append(memberName) + private void populateRegistry(final String memberName) { + String notifier = new StringBuilder().append(NOTIFIER_AKKA_URL).append(memberName) .append("/").append(memberName).append("-notifier").toString(); - if (!notifierRegistrationStatus.containsKey(notifier)) { - notifierRegistrationStatus.put(notifier, false); - } + if (!notifierRegistrationStatus.containsKey(notifier)) { + notifierRegistrationStatus.put(notifier, false); } if (!registrationSchedule.isCancelled()) { - scheduleRegistrationListener(schedulerDuration); + scheduleRegistrationListener(SCHEDULER_DURATION); } } + @SuppressWarnings("checkstyle:IllegalCatch") + @SuppressFBWarnings("REC_CATCH_EXCEPTION") private void sendRegistrationRequests() { for (Map.Entry entry : notifierRegistrationStatus.entrySet()) { if (!entry.getValue()) { try { LOG.debug("{} registering with {}", getSelf().path().toString(), entry.getKey()); ActorRef notifier = Await.result( - getContext().actorSelection(entry.getKey()).resolveOne(duration), duration); + getContext().actorSelection(entry.getKey()).resolveOne(DURATION), DURATION); notifier.tell(new RegisterRoleChangeListener(), getSelf()); } catch (Exception e) { - LOG.error("ERROR!! Unable to send registration request to notifier {}", entry.getKey()); + LOG.error("ERROR!! Unable to send registration request to notifier {}", entry.getKey(), e); } } } } - private void handleRegisterRoleChangeListenerReply(String senderId) { + private void handleRegisterRoleChangeListenerReply(final String senderId) { if (notifierRegistrationStatus.containsKey(senderId)) { notifierRegistrationStatus.put(senderId, true); @@ -126,14 +134,15 @@ public class ExampleRoleChangeListener extends AbstractUntypedActor implements A } } } else { - LOG.info("Unexpected, RegisterRoleChangeListenerReply received from notifier which is not known to Listener:{}", + LOG.info( + "Unexpected, RegisterRoleChangeListenerReply received from notifier which is not known to Listener:{}", senderId); } } @Override - public void close() throws Exception { + public void close() { registrationSchedule.cancel(); } }