You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pekko.apache.org by he...@apache.org on 2024/06/14 08:14:59 UTC

(pekko) branch main updated: feat: Add UntypedAbstractActorWithTimers (#1361)

This is an automated email from the ASF dual-hosted git repository.

hepin pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pekko.git


The following commit(s) were added to refs/heads/main by this push:
     new a9584ec520 feat: Add UntypedAbstractActorWithTimers (#1361)
a9584ec520 is described below

commit a9584ec520a3110dcf37f4cf32194ea19ddd77a7
Author: He-Pin(kerr) <he...@gmail.com>
AuthorDate: Fri Jun 14 16:14:55 2024 +0800

    feat: Add UntypedAbstractActorWithTimers (#1361)
---
 .../main/scala/org/apache/pekko/actor/Timers.scala | 14 +++++++++++++
 .../test/java/jdocs/testkit/TestKitDocTest.java    | 24 +++++++++++++++++++++-
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/actor/src/main/scala/org/apache/pekko/actor/Timers.scala b/actor/src/main/scala/org/apache/pekko/actor/Timers.scala
index ee1f0413d4..33649c45b1 100644
--- a/actor/src/main/scala/org/apache/pekko/actor/Timers.scala
+++ b/actor/src/main/scala/org/apache/pekko/actor/Timers.scala
@@ -83,6 +83,20 @@ abstract class AbstractActorWithTimers extends AbstractActor with Timers {
   final def getTimers: TimerScheduler = timers
 }
 
+/**
+ * Java API: Support for scheduled `self` messages via [[TimerScheduler]].
+ *
+ * Timers are bound to the lifecycle of the actor that owns it,
+ * and thus are cancelled automatically when it is restarted or stopped.
+ */
+abstract class UntypedAbstractActorWithTimers extends UntypedAbstractActor with Timers {
+
+  /**
+   * Start and cancel timers via the enclosed `TimerScheduler`.
+   */
+  final def getTimers: TimerScheduler = timers
+}
+
 /**
  * Support for scheduled `self` messages in an actor.
  * It is used by mixing in trait `Timers` in Scala or extending `AbstractActorWithTimers`
diff --git a/docs/src/test/java/jdocs/testkit/TestKitDocTest.java b/docs/src/test/java/jdocs/testkit/TestKitDocTest.java
index ffc4297fde..e2750baa40 100644
--- a/docs/src/test/java/jdocs/testkit/TestKitDocTest.java
+++ b/docs/src/test/java/jdocs/testkit/TestKitDocTest.java
@@ -40,6 +40,7 @@ import org.apache.pekko.actor.PoisonPill;
 import org.apache.pekko.actor.Props;
 import org.apache.pekko.actor.Terminated;
 import org.apache.pekko.actor.AbstractActor;
+import org.apache.pekko.actor.UntypedAbstractActorWithTimers;
 import org.apache.pekko.testkit.TestActor.AutoPilot;
 
 import java.util.List;
@@ -92,7 +93,7 @@ public class TestKitDocTest extends AbstractJavaTest {
 
   // #timer
   static class TestTimerActor extends AbstractActorWithTimers {
-    private static Object SCHED_KEY = "SchedKey";
+    private static String SCHED_KEY = "SchedKey";
 
     static final class TriggerScheduling {}
 
@@ -107,6 +108,27 @@ public class TestKitDocTest extends AbstractJavaTest {
       getTimers().startSingleTimer(SCHED_KEY, new ScheduledMessage(), Duration.ofMillis(500));
     }
   }
+
+  static class TestTimerUntypedActor extends UntypedAbstractActorWithTimers {
+    private static String SCHED_KEY = "SchedKey";
+
+    static final class TriggerScheduling {}
+
+    static final class ScheduledMessage {}
+
+    @Override
+    public void onReceive(Object message) throws Throwable {
+      if (message instanceof TriggerScheduling) {
+        triggerScheduling();
+      } else {
+        unhandled(message);
+      }
+    }
+
+    void triggerScheduling() {
+      getTimers().startSingleTimer(SCHED_KEY, new ScheduledMessage(), Duration.ofMillis(500));
+    }
+  }
   // #timer
 
   @Test


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pekko.apache.org
For additional commands, e-mail: commits-help@pekko.apache.org