commit c379db77cb35b84a7b4e2ce065b2fbb5ab5e6196 Author: Christoph Stahl Date: Mon Apr 24 10:21:18 2023 +0200 Aufgabe2 diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..0526e9a --- /dev/null +++ b/build.gradle @@ -0,0 +1,22 @@ +apply plugin: 'java' +apply plugin: 'idea' +apply plugin: 'application' + + +repositories { + mavenCentral() + mavenLocal() +} + +dependencies { + implementation 'com.typesafe.akka:akka-actor-typed_2.13:2.8.0' + implementation 'ch.qos.logback:logback-classic:1.2.3' + testImplementation 'com.typesafe.akka:akka-actor-testkit-typed_2.13:2.8.0' + testImplementation 'junit:junit:4.13.1' +} + +mainClassName = "com.example.AkkaQuickstart" + +run { + standardInput = System.in +} diff --git a/src/main/java/com/example/AkkaQuickstart.java b/src/main/java/com/example/AkkaQuickstart.java new file mode 100644 index 0000000..e15f312 --- /dev/null +++ b/src/main/java/com/example/AkkaQuickstart.java @@ -0,0 +1,20 @@ +package com.example; + +import akka.actor.typed.ActorSystem; + +import java.io.IOException; +public class AkkaQuickstart { + public static void main(String[] args) { + final ActorSystem messageMain = ActorSystem.create(MessageMain.create(), "callAround"); + + messageMain.tell(new MessageMain.Create()); + + try { + System.out.println(">>> Press ENTER to exit <<<"); + System.in.read(); + } catch (IOException ignored) { + } finally { + messageMain.terminate(); + } + } +} diff --git a/src/main/java/com/example/Bot.java b/src/main/java/com/example/Bot.java new file mode 100644 index 0000000..d8b85ec --- /dev/null +++ b/src/main/java/com/example/Bot.java @@ -0,0 +1,65 @@ +package com.example; + +import akka.actor.typed.ActorRef; +import akka.actor.typed.Behavior; +import akka.actor.typed.javadsl.*; + +public class Bot extends AbstractBehavior { + + public interface Message {}; + + public static class Start implements Message { + ActorRef next; + ActorRef nextnext; + + public Start(ActorRef next, ActorRef nextnext) { + this.next = next; + this.nextnext = nextnext; + } + } + + public static class Hello implements Message { + ActorRef next; + ActorRef nextnext; + int counter; + + public Hello(ActorRef next, ActorRef nextnext, int counter) { + this.next = next; + this.nextnext = nextnext; + this.counter = counter; + } + } + + public static Behavior create(String name) { + return Behaviors.setup(context -> new Bot(context, name)); + } + + private final String name; + + private Bot(ActorContext context, String name) { + super(context); + this.name = name; + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(Hello.class, this::onHello) + .onMessage(Start.class, this::onStart).build(); + } + + private Behavior onHello(Hello msg) { + getContext().getLog().info("Hello {} for {}", msg.counter, this.name); + if (msg.counter == 15) { + return Behaviors.stopped(); + } else { + msg.next.tell(new Hello(msg.nextnext, this.getContext().getSelf(), msg.counter+1)); + return this; + } + } + + private Behavior onStart(Start msg) { + msg.next.tell(new Hello(msg.nextnext, this.getContext().getSelf(), 1)); + return this; + } +} diff --git a/src/main/java/com/example/MessageMain.java b/src/main/java/com/example/MessageMain.java new file mode 100644 index 0000000..03bb9c1 --- /dev/null +++ b/src/main/java/com/example/MessageMain.java @@ -0,0 +1,35 @@ +package com.example; + +import akka.actor.typed.ActorRef; +import akka.actor.typed.Behavior; +import akka.actor.typed.javadsl.*; + +public class MessageMain extends AbstractBehavior { + + public static class Create { + } + + public static Behavior create() { + return Behaviors.setup(MessageMain::new); + } + + private MessageMain(ActorContext context) { + super(context); + } + + @Override + public Receive createReceive() { + return newReceiveBuilder().onMessage(Create.class, this::onCreate).build(); + } + + private Behavior onCreate(Create command) { + //#create-actors + ActorRef a = this.getContext().spawn(Bot.create("Alice"), "alice"); + ActorRef b = this.getContext().spawn(Bot.create("Bob"), "bob"); + ActorRef c = this.getContext().spawn(Bot.create("Charlie"), "charlie"); + //#create-actors + + a.tell(new Bot.Start(b,c)); + return this; + } +} diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 0000000..203596d --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,21 @@ + + + + + + [%date{ISO8601}] [%level] [%logger] [%thread] [%X{akkaSource}] - %msg%n + + + + + 1024 + true + + + + + + + +