- if(message instanceof String){
- if("get-all-messages".equals(message)){
- getSender().tell(new ArrayList<>(messages), getSelf());
+ @SuppressWarnings({"unchecked", "checkstyle:illegalCatch"})
+ public static List<Object> getAllMessages(final ActorRef actor) {
+ FiniteDuration operationDuration = FiniteDuration.create(5, TimeUnit.SECONDS);
+ Timeout operationTimeout = new Timeout(operationDuration);
+ Future<Object> future = Patterns.ask(actor, GET_ALL_MESSAGES, operationTimeout);
+
+ try {
+ return (List<Object>) Await.result(future, operationDuration);
+ } catch (RuntimeException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static void clearMessages(final ActorRef actor) {
+ actor.tell(CLEAR_MESSAGES, ActorRef.noSender());
+ }
+
+ /**
+ * Get the first message that matches the specified class.
+ *
+ * @param actor the MessageCollectorActor reference
+ * @param clazz the class to match
+ * @return the first matching message
+ */
+ public static <T> T getFirstMatching(final ActorRef actor, final Class<T> clazz) {
+ List<Object> allMessages = getAllMessages(actor);
+
+ for (Object message : allMessages) {
+ if (message.getClass().equals(clazz)) {
+ return clazz.cast(message);