package com.example; import akka.actor.typed.ActorRef; import akka.actor.typed.Behavior; import akka.actor.typed.javadsl.*; import java.util.Objects; // #greeter public class Greeter extends AbstractBehavior { public static final class Greet { public final String whom; public final ActorRef replyTo; public Greet(String whom, ActorRef replyTo) { this.whom = whom; this.replyTo = replyTo; } } public static final class Greeted { public final String whom; public final ActorRef from; public Greeted(String whom, ActorRef from) { this.whom = whom; this.from = from; } // #greeter @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Greeted greeted = (Greeted) o; return Objects.equals(whom, greeted.whom) && Objects.equals(from, greeted.from); } @Override public int hashCode() { return Objects.hash(whom, from); } @Override public String toString() { return "Greeted{" + "whom='" + whom + '\'' + ", from=" + from + '}'; } // #greeter } public static Behavior create() { return Behaviors.setup(Greeter::new); } private Greeter(ActorContext context) { super(context); } @Override public Receive createReceive() { return newReceiveBuilder().onMessage(Greet.class, this::onGreet).build(); } private Behavior onGreet(Greet command) { getContext().getLog().info("Hello {}!", command.whom); //#greeter-send-message command.replyTo.tell(new Greeted(command.whom, getContext().getSelf())); //#greeter-send-message return this; } } // #greeter