This commit is contained in:
Christoph Stahl 2023-04-24 10:21:18 +02:00
commit c379db77cb
5 changed files with 163 additions and 0 deletions

22
build.gradle Normal file
View File

@ -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
}

View File

@ -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.Create> 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();
}
}
}

View File

@ -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<Bot.Message> {
public interface Message {};
public static class Start implements Message {
ActorRef<Bot.Message> next;
ActorRef<Bot.Message> nextnext;
public Start(ActorRef<Bot.Message> next, ActorRef<Bot.Message> nextnext) {
this.next = next;
this.nextnext = nextnext;
}
}
public static class Hello implements Message {
ActorRef<Bot.Message> next;
ActorRef<Bot.Message> nextnext;
int counter;
public Hello(ActorRef<Bot.Message> next, ActorRef<Bot.Message> nextnext, int counter) {
this.next = next;
this.nextnext = nextnext;
this.counter = counter;
}
}
public static Behavior<Message> create(String name) {
return Behaviors.setup(context -> new Bot(context, name));
}
private final String name;
private Bot(ActorContext<Message> context, String name) {
super(context);
this.name = name;
}
@Override
public Receive<Message> createReceive() {
return newReceiveBuilder()
.onMessage(Hello.class, this::onHello)
.onMessage(Start.class, this::onStart).build();
}
private Behavior<Message> 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<Message> onStart(Start msg) {
msg.next.tell(new Hello(msg.nextnext, this.getContext().getSelf(), 1));
return this;
}
}

View File

@ -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<MessageMain.Create> {
public static class Create {
}
public static Behavior<Create> create() {
return Behaviors.setup(MessageMain::new);
}
private MessageMain(ActorContext<Create> context) {
super(context);
}
@Override
public Receive<Create> createReceive() {
return newReceiveBuilder().onMessage(Create.class, this::onCreate).build();
}
private Behavior<Create> onCreate(Create command) {
//#create-actors
ActorRef<Bot.Message> a = this.getContext().spawn(Bot.create("Alice"), "alice");
ActorRef<Bot.Message> b = this.getContext().spawn(Bot.create("Bob"), "bob");
ActorRef<Bot.Message> c = this.getContext().spawn(Bot.create("Charlie"), "charlie");
//#create-actors
a.tell(new Bot.Start(b,c));
return this;
}
}

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- This is a development logging configuration that logs to standard out, for an example of a production
logging config, see the Akka docs: https://doc.akka.io/docs/akka/2.6/typed/logging.html#logback -->
<appender name="STDOUT" target="System.out" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%date{ISO8601}] [%level] [%logger] [%thread] [%X{akkaSource}] - %msg%n</pattern>
</encoder>
</appender>
<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<queueSize>1024</queueSize>
<neverBlock>true</neverBlock>
<appender-ref ref="STDOUT" />
</appender>
<root level="INFO">
<appender-ref ref="ASYNC"/>
</root>
</configuration>