commit 799e9adcb946c6995f2f5fde80d58522a17ba501 Author: Christoph Stahl Date: Wed May 3 12:02:18 2023 +0200 Blanko projekt diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..52eca03 --- /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.AkkaStart" + +run { + standardInput = System.in +} diff --git a/src/main/java/com/example/AkkaMainSystem.java b/src/main/java/com/example/AkkaMainSystem.java new file mode 100644 index 0000000..7ddda70 --- /dev/null +++ b/src/main/java/com/example/AkkaMainSystem.java @@ -0,0 +1,33 @@ +package com.example; + +import akka.actor.typed.ActorRef; +import akka.actor.typed.Behavior; +import akka.actor.typed.javadsl.*; + +public class AkkaMainSystem extends AbstractBehavior { + + public static class Create { + } + + public static Behavior create() { + return Behaviors.setup(AkkaMainSystem::new); + } + + private AkkaMainSystem(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(ExampleActor.create("Alice"), "alice"); + //#create-actors + + a.tell(new ExampleActor.ExampleMessage(this.getContext().getSelf(),"Test123")); + return this; + } +} diff --git a/src/main/java/com/example/AkkaStart.java b/src/main/java/com/example/AkkaStart.java new file mode 100644 index 0000000..45339c2 --- /dev/null +++ b/src/main/java/com/example/AkkaStart.java @@ -0,0 +1,20 @@ +package com.example; + +import akka.actor.typed.ActorSystem; + +import java.io.IOException; +public class AkkaStart { + public static void main(String[] args) { + final ActorSystem messageMain = ActorSystem.create(AkkaMainSystem.create(), "akkaMainSystem"); + + messageMain.tell(new AkkaMainSystem.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/ExampleActor.java b/src/main/java/com/example/ExampleActor.java new file mode 100644 index 0000000..cf56713 --- /dev/null +++ b/src/main/java/com/example/ExampleActor.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 ExampleActor extends AbstractBehavior { + + public interface Message {}; + + public record ExampleMessage(ActorRef someReference, String someString) implements Message { } + + public static Behavior create(String name) { + return Behaviors.setup(context -> new ExampleActor(context, name)); + } + + private final String name; + + private ExampleActor(ActorContext context, String name) { + super(context); + this.name = name; + } + + @Override + public Receive createReceive() { + return newReceiveBuilder() + .onMessage(ExampleMessage.class, this::onExampleMessage) + .build(); + } + + private Behavior onExampleMessage(ExampleMessage msg) { + getContext().getLog().info("I ({}) got a message: ExampleMessage({},{})", this.name, msg.someReference, msg.someString); + 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 + + + + + + + +