From b99b31f3c8b8a1871e4df28b474803dc746739f3 Mon Sep 17 00:00:00 2001 From: LM Date: Mon, 16 Dec 2019 20:02:01 -0800 Subject: [PATCH 1/2] Add ActorTimer and related --- .../io/dapr/actors/runtime/ActorTimer.java | 41 +++++++++++++ .../dapr/actors/runtime/ActorTimerImpl.java | 53 +++++++++++++++++ .../actors/runtime/ActorTimerImplTest.java | 57 +++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 sdk/src/main/java/io/dapr/actors/runtime/ActorTimer.java create mode 100644 sdk/src/main/java/io/dapr/actors/runtime/ActorTimerImpl.java create mode 100644 sdk/src/test/java/io/dapr/actors/runtime/ActorTimerImplTest.java diff --git a/sdk/src/main/java/io/dapr/actors/runtime/ActorTimer.java b/sdk/src/main/java/io/dapr/actors/runtime/ActorTimer.java new file mode 100644 index 0000000000..f102ea2bdc --- /dev/null +++ b/sdk/src/main/java/io/dapr/actors/runtime/ActorTimer.java @@ -0,0 +1,41 @@ +package io.dapr.actors.runtime; + +import java.time.Duration; +import java.util.function.Function; + +/** + * Represents the timer set on an Actor. + */ +public interface ActorTimer { + + /** + * Gets the time when timer is first due. + * @return Time as Duration when timer is first due. + */ + Duration getDueTime(); + + /** + * Gets the periodic time when timer will be invoked. + * @return Periodic time as when timer will be invoked. + */ + Duration getPeriod(); + + /** + * Gets the name of the Timer. The name is unique per actor. + * @return The name of the timer. + */ + String getName(); + + /** + * + * @return Gets a delegate that specifies a method to be called when the timer fires. + * It has one parameter: the state object passed to RegisterTimer. + */ + Function getAsyncCallback(); + + /** + * + * @return Gets state containing information to be used by the callback method, or null. + */ + Object getState(); +} diff --git a/sdk/src/main/java/io/dapr/actors/runtime/ActorTimerImpl.java b/sdk/src/main/java/io/dapr/actors/runtime/ActorTimerImpl.java new file mode 100644 index 0000000000..6b6f8ff3cd --- /dev/null +++ b/sdk/src/main/java/io/dapr/actors/runtime/ActorTimerImpl.java @@ -0,0 +1,53 @@ +package io.dapr.actors.runtime; + +import org.json.JSONObject; + +import java.time.Duration; +import java.util.function.Function; + +class ActorTimerImpl implements ActorTimer { + + private final AbstractActor owner; + private String name; + private Function asyncCallback; + private Object state; + private Duration dueTime; + private Duration period; + + public ActorTimerImpl(AbstractActor owner, String timerName, Function asyncCallback, Object state, Duration dueTime, Duration period) { + this.owner = owner; + this.name = timerName; + this.asyncCallback = asyncCallback; + this.state = state; + this.dueTime = dueTime; + this.period = period; + } + + public String getName() { + return this.name; + } + + public Duration getDueTime() { + return this.dueTime; + } + + public Function getAsyncCallback() { + return this.asyncCallback; + } + + public Duration getPeriod() { + return this.period; + } + + public Object getState() { + return this.state; + } + + String serialize() + { + JSONObject j = new JSONObject(); + j.put("dueTime", ConverterUtils.ConvertDurationToDaprFormat(this.getDueTime())); + j.put("period", ConverterUtils.ConvertDurationToDaprFormat(this.getPeriod())); + return j.toString(); + } +} diff --git a/sdk/src/test/java/io/dapr/actors/runtime/ActorTimerImplTest.java b/sdk/src/test/java/io/dapr/actors/runtime/ActorTimerImplTest.java new file mode 100644 index 0000000000..52bdc5c437 --- /dev/null +++ b/sdk/src/test/java/io/dapr/actors/runtime/ActorTimerImplTest.java @@ -0,0 +1,57 @@ +package io.dapr.actors.runtime; + +import org.junit.Assert; +import org.junit.Test; + +import java.time.Duration; + +public class ActorTimerImplTest { + + @Test + public void serialize() { + Duration dueTime = Duration.ZERO + .plusMinutes(7) + .plusSeconds(17); + + Duration period = Duration.ZERO + .plusHours(1) + .plusSeconds(3); + + ActorTimerImpl timer = new ActorTimerImpl( + null, + "testTimer", + null, + null, + dueTime, + period); + String s = timer.serialize(); + + String expected = "{\"period\":\"1h0m3s0ms\",\"dueTime\":\"0h7m17s0ms\"}"; + Assert.assertEquals(expected, s); + } + + @Test + public void serializeWithOneTimePeriod() { + Duration dueTime = Duration.ZERO + .plusMinutes(7) + .plusSeconds(17); + + // this is intentionally negative + Duration period = Duration.ZERO + .minusHours(1) + .minusMinutes(3); + + ActorTimerImpl timer = new ActorTimerImpl( + null, + "testTimer", + null, + null, + dueTime, + period); + String s = timer.serialize(); + + // A negative period will be serialized to an empty string which is interpreted by Dapr to mean fire once only. + String expected = "{\"period\":\"\",\"dueTime\":\"0h7m17s0ms\"}"; + Assert.assertEquals(expected, s); + } +} From 112e02d2daad83011057d585f1ba6ab64821022e Mon Sep 17 00:00:00 2001 From: LM Date: Tue, 17 Dec 2019 11:31:33 -0800 Subject: [PATCH 2/2] cr --- .../io/dapr/actors/runtime/ActorTimer.java | 2 +- .../dapr/actors/runtime/ActorTimerImpl.java | 37 ++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/sdk/src/main/java/io/dapr/actors/runtime/ActorTimer.java b/sdk/src/main/java/io/dapr/actors/runtime/ActorTimer.java index f102ea2bdc..c1c5f36b17 100644 --- a/sdk/src/main/java/io/dapr/actors/runtime/ActorTimer.java +++ b/sdk/src/main/java/io/dapr/actors/runtime/ActorTimer.java @@ -16,7 +16,7 @@ public interface ActorTimer { /** * Gets the periodic time when timer will be invoked. - * @return Periodic time as when timer will be invoked. + * @return Periodic time as Duration when timer will be invoked. */ Duration getPeriod(); diff --git a/sdk/src/main/java/io/dapr/actors/runtime/ActorTimerImpl.java b/sdk/src/main/java/io/dapr/actors/runtime/ActorTimerImpl.java index 6b6f8ff3cd..ebd1f9d155 100644 --- a/sdk/src/main/java/io/dapr/actors/runtime/ActorTimerImpl.java +++ b/sdk/src/main/java/io/dapr/actors/runtime/ActorTimerImpl.java @@ -1,10 +1,12 @@ package io.dapr.actors.runtime; import org.json.JSONObject; - import java.time.Duration; import java.util.function.Function; +/** + * Represents the timer set on an Actor. + */ class ActorTimerImpl implements ActorTimer { private final AbstractActor owner; @@ -14,6 +16,15 @@ class ActorTimerImpl implements ActorTimer { private Duration dueTime; private Duration period; + /** + * + * @param owner The Actor that owns this timer. The timer callback will be fired for this Actor. + * @param timerName The name of the timer. + * @param asyncCallback The callback to invoke when the timer fires. + * @param state information to be used by the callback method + * @param dueTime the time when timer is first due. + * @param period the periodic time when timer will be invoked. + */ public ActorTimerImpl(AbstractActor owner, String timerName, Function asyncCallback, Object state, Duration dueTime, Duration period) { this.owner = owner; this.name = timerName; @@ -23,26 +34,50 @@ public ActorTimerImpl(AbstractActor owner, String timerName, Function getAsyncCallback() { return this.asyncCallback; } + /** + * Gets the periodic time when timer will be invoked. + * @return Periodic time as Duration when timer will be invoked. + */ public Duration getPeriod() { return this.period; } + /** + * + * @return Gets state containing information to be used by the callback method, or null. + */ public Object getState() { return this.state; } + /** + * + * @return + */ String serialize() { JSONObject j = new JSONObject();