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 scala.concurrent.Await;
17 import scala.concurrent.Future;
18 import scala.concurrent.duration.Duration;
19 import scala.concurrent.duration.FiniteDuration;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.concurrent.TimeUnit;
26 public class MessageCollectorActor extends UntypedActor {
27 private 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(messages, getSelf());
35 messages.add(message);
39 public static List<Object> getAllMessages(ActorRef actor) throws Exception {
40 FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
41 Timeout operationTimeout = new Timeout(operationDuration);
42 Future<Object> future = Patterns.ask(actor, "get-all-messages", operationTimeout);
45 return (List<Object>) Await.result(future, operationDuration);
46 } catch (Exception e) {
52 * Get the first message that matches the specified class
57 public static Object getFirstMatching(ActorRef actor, Class<?> clazz) throws Exception {
58 List<Object> allMessages = getAllMessages(actor);
60 for(Object message : allMessages){
61 if(message.getClass().equals(clazz)){
69 public static List<Object> getAllMatching(ActorRef actor, Class<?> clazz) throws Exception {
70 List<Object> allMessages = getAllMessages(actor);
72 List<Object> output = Lists.newArrayList();
74 for(Object message : allMessages){
75 if(message.getClass().equals(clazz)){