package com.example; import akka.actor.typed.ActorRef; import akka.actor.typed.Behavior; import akka.actor.typed.javadsl.*; // #greeter public class Greeter extends AbstractBehavior { public interface Message {} public record Response(boolean are_messages_left, Greet msg) implements Message {} public record Greet(String whom, ActorRef replyTo) implements Message {} public record Greeted(String whom, ActorRef replyTo) {} public static Behavior create(ActorRef overseer) { return Behaviors.setup(context -> new Greeter(context, overseer)); } private final ActorRef overseer; private Greeter(ActorContext context, ActorRef overseer) { super(context); this.overseer = overseer; } @Override public Receive createReceive() { return newReceiveBuilder().onMessage(Greet.class, this::onGreet).onMessage(Response.class, this::onResponse).build(); } private Behavior onResponse(Response command) { if (command.are_messages_left()) { getContext().getLog().info("Hello {}!", command.msg().whom()); //#greeter-send-message command.msg().replyTo().tell(new Greeter.Greeted(command.msg().whom(), getContext().getSelf())); //#greeter-send-message } return this; } private Behavior onGreet(Greet command) { overseer.tell(new Overseer.Request(command, getContext().getSelf())); return this; } } // #greeter