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 scala.concurrent.Await;
16 import scala.concurrent.Future;
17 import scala.concurrent.duration.Duration;
18 import scala.concurrent.duration.FiniteDuration;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.concurrent.TimeUnit;
25 public class MessageCollectorActor extends UntypedActor {
26 private List<Object> messages = new ArrayList<>();
28 @Override public void onReceive(Object message) throws Exception {
29 if(message instanceof String){
30 if("get-all-messages".equals(message)){
31 getSender().tell(messages, getSelf());
34 messages.add(message);
38 public static List<Object> getAllMessages(ActorRef actor) throws Exception {
39 FiniteDuration operationDuration = Duration.create(5, TimeUnit.SECONDS);
40 Timeout operationTimeout = new Timeout(operationDuration);
41 Future<Object> future = Patterns.ask(actor, "get-all-messages", operationTimeout);
44 return (List<Object>) Await.result(future, operationDuration);
45 } catch (Exception e) {
51 * Get the first message that matches the specified class
56 public static Object getFirstMatching(ActorRef actor, Class clazz) throws Exception {
57 List<Object> allMessages = getAllMessages(actor);
59 for(Object message : allMessages){
60 if(message.getClass().equals(clazz)){