From d37262eadced115edbe9a03ac694f3d10523c91f Mon Sep 17 00:00:00 2001 From: olhag Date: Tue, 2 Jul 2024 22:45:01 +0200 Subject: [PATCH] Duration Ausgabe --- src/main/java/com/example/Actor.java | 32 ++++++------- src/main/java/com/example/ExampleActor.java | 35 --------------- .../java/com/example/ExampleTimerActor.java | 45 ------------------- .../java/com/example/PrintAndEvaluate.java | 27 +++++++---- 4 files changed, 35 insertions(+), 104 deletions(-) delete mode 100644 src/main/java/com/example/ExampleActor.java delete mode 100644 src/main/java/com/example/ExampleTimerActor.java diff --git a/src/main/java/com/example/Actor.java b/src/main/java/com/example/Actor.java index 7076d4e..8c80676 100644 --- a/src/main/java/com/example/Actor.java +++ b/src/main/java/com/example/Actor.java @@ -156,15 +156,15 @@ public class Actor extends AbstractBehavior { private Behavior onLeftStringResponse(LeftStringResponse response){ this.leftString = response.string; if(this.rightString != null){ - if(this.initial != null){ - initial.tell(new PrintAndEvaluate.StringResponse("(" + leftString + operationString + rightString +")")); - }else { - if(side == true){ - oberActor.tell(new Actor.LeftStringResponse("(" + leftString + operationString + rightString +")")); - }else { - oberActor.tell(new Actor.RightStringResponse("(" + leftString + operationString + rightString +")")); - } - } + if(this.initial != null){ + initial.tell(new PrintAndEvaluate.StringResponse("(" + leftString + operationString + rightString +")")); + }else { + if(side == true){ + oberActor.tell(new Actor.LeftStringResponse("(" + leftString + operationString + rightString +")")); + }else { + oberActor.tell(new Actor.RightStringResponse("(" + leftString + operationString + rightString +")")); + } + } } return this; } @@ -300,13 +300,13 @@ public class Actor extends AbstractBehavior { } } } else { - if (operationString.equals("+")) { - initial.tell(new PrintAndEvaluate.IntResponse(leftInt + rightInt)); - } else if (operationString.equals("-")) { - initial.tell(new PrintAndEvaluate.IntResponse(leftInt - rightInt)); - } else { - initial.tell(new PrintAndEvaluate.IntResponse(leftInt * rightInt)); - } + if (operationString.equals("+")) { + initial.tell(new PrintAndEvaluate.IntResponse(leftInt + rightInt)); + } else if (operationString.equals("-")) { + initial.tell(new PrintAndEvaluate.IntResponse(leftInt - rightInt)); + } else { + initial.tell(new PrintAndEvaluate.IntResponse(leftInt * rightInt)); + } } return this; } diff --git a/src/main/java/com/example/ExampleActor.java b/src/main/java/com/example/ExampleActor.java deleted file mode 100644 index 5dc4b9a..0000000 --- a/src/main/java/com/example/ExampleActor.java +++ /dev/null @@ -1,35 +0,0 @@ -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 = "test"; - } - - @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/java/com/example/ExampleTimerActor.java b/src/main/java/com/example/ExampleTimerActor.java deleted file mode 100644 index 0bffbea..0000000 --- a/src/main/java/com/example/ExampleTimerActor.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.example; - -import akka.actor.typed.javadsl.TimerScheduler; -import akka.actor.typed.Behavior; -import akka.actor.typed.javadsl.AbstractBehavior; -import akka.actor.typed.javadsl.ActorContext; -import akka.actor.typed.javadsl.Behaviors; -import akka.actor.typed.javadsl.Receive; - -import java.time.Duration; - - -public class ExampleTimerActor extends AbstractBehavior { - - public interface Message {}; - - - public record ExampleMessage(String someString) implements Message { } - - public static Behavior create() { - return Behaviors.setup(context -> Behaviors.withTimers(timers -> new ExampleTimerActor(context, timers))); - } - - private final TimerScheduler timers; - - private ExampleTimerActor(ActorContext context, TimerScheduler timers) { - super(context); - this.timers = timers; - - Message msg = new ExampleMessage("test123"); - this.timers.startSingleTimer(msg, msg, Duration.ofSeconds(10)); - } - - @Override - public Receive createReceive() { - return newReceiveBuilder() - .onMessage(ExampleMessage.class, this::onExampleMessage) - .build(); - } - - private Behavior onExampleMessage(ExampleMessage msg) { - getContext().getLog().info("I have send myself this message after 10 Seconds: {}", msg.someString); - return this; - } -} diff --git a/src/main/java/com/example/PrintAndEvaluate.java b/src/main/java/com/example/PrintAndEvaluate.java index bab2a20..829ee60 100644 --- a/src/main/java/com/example/PrintAndEvaluate.java +++ b/src/main/java/com/example/PrintAndEvaluate.java @@ -7,9 +7,14 @@ import akka.actor.typed.javadsl.ActorContext; import akka.actor.typed.javadsl.Behaviors; import akka.actor.typed.javadsl.Receive; +import java.time.Duration; +import java.time.Instant; + public class PrintAndEvaluate extends AbstractBehavior { String name; + Instant startTime; // Startzeit für die Zeitmessung der Berechnung + public interface Message{} public record StringResponse(String string) implements Message{} @@ -36,29 +41,35 @@ public class PrintAndEvaluate extends AbstractBehavior .build(); } - //Mit dieser Message wird der PrintAndEvaluate Aktor in Gang gesetzt + // Mit dieser Message wird der PrintAndEvaluate Aktor in Gang gesetzt private Behavior onStartMessage(StartMessage message){ - //Starten des Formatierungsvorganges - this.getContext().getLog().info("\n Beginn der Ausdrucksformatierung"); - ActorRef printActor = getContext().spawnAnonymous(Actor.create()); - printActor.tell(new Actor.PrintMessage(getContext().getSelf(),message.expression)); + // Startzeit für die Zeitmessung setzen + this.startTime = Instant.now(); - //Starten des Berechnungsvorganges + // Starten des Formatierungsvorganges + this.getContext().getLog().info("\n Beginn der Ausdrucksformatierung"); + + ActorRef printActor = getContext().spawnAnonymous(Actor.create()); + printActor.tell(new Actor.PrintMessage(getContext().getSelf(), message.expression)); + + // Starten des Berechnungsvorganges this.getContext().getLog().info("\n Beginn der Berechnung"); ActorRef computeActor = getContext().spawnAnonymous(Actor.create()); computeActor.tell(new Actor.EvaluateMessage(getContext().getSelf(), message.expression)); return this; } - //Ausgeben des Ausdruckes als String + // Ausgeben des Ausdruckes als String private Behavior onStringResponse(StringResponse response){ this.getContext().getLog().info("\n Formatierter Ausdruck: " + response.string); return this; } - //Ausgeben des berechneten Wertes von dem Ausdruck + // Ausgeben des berechneten Wertes von dem Ausdruck und der Berechnungsdauer private Behavior onIntResponse(IntResponse response){ + Duration duration = Duration.between(startTime, Instant.now()); this.getContext().getLog().info("\n Ausgerechneter Wert: " + response.wert); + this.getContext().getLog().info("\n Berechnungsdauer: " + duration.toSeconds() + " Sekunden"); return this; } }