2 * Copyright (c) 2013 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
9 package org.opendaylight.controller.cluster.raft.utils;
11 import akka.actor.ActorRef;
12 import akka.actor.UntypedActor;
13 import akka.pattern.Patterns;
14 import akka.util.Timeout;
15 import com.google.common.collect.Lists;
16 import com.google.common.util.concurrent.Uninterruptibles;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.concurrent.TimeUnit;
20 import scala.concurrent.Await;
21 import scala.concurrent.Future;
22 import scala.concurrent.duration.Duration;
23 import scala.concurrent.duration.FiniteDuration;
26 public class MessageCollectorActor extends UntypedActor {
27 private final List<Object> messages = new ArrayList<>();
29 @Override public void onReceive(Object message) throws Exception {
30 if(message instanceof String){
31 if("get-all-messages".equals(message)){
32 getSender().tell(new ArrayList(messages), getSelf());
35 messages.add(message);
43 public static List<Object> getAllMessages(ActorRef actor) throws Exception {
44 FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
45 Timeout operationTimeout = new Timeout(operationDuration);
46 Future<Object> future = Patterns.ask(actor, "get-all-messages", operationTimeout);
49 return (List<Object>) Await.result(future, operationDuration);
50 } catch (Exception e) {
56 * Get the first message that matches the specified class
61 public static <T> T getFirstMatching(ActorRef actor, Class<T> clazz) throws Exception {
62 for(int i = 0; i < 50; i++) {
63 List<Object> allMessages = getAllMessages(actor);
65 for(Object message : allMessages){
66 if(message.getClass().equals(clazz)){
71 Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
77 public static List<Object> getAllMatching(ActorRef actor, Class<?> clazz) throws Exception {
78 List<Object> allMessages = getAllMessages(actor);
80 List<Object> output = Lists.newArrayList();
82 for(Object message : allMessages){
83 if(message.getClass().equals(clazz)){