-
Notifications
You must be signed in to change notification settings - Fork 230
Add ActorTimer and related #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 Duration 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<Object, Void> getAsyncCallback(); | ||
|
|
||
| /** | ||
| * | ||
| * @return Gets state containing information to be used by the callback method, or null. | ||
| */ | ||
| Object getState(); | ||
| } |
88 changes: 88 additions & 0 deletions
88
sdk/src/main/java/io/dapr/actors/runtime/ActorTimerImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| 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; | ||
| private String name; | ||
| private Function<Object, Void> asyncCallback; | ||
| private Object state; | ||
| 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<Object, Void> 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; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the name of the Timer. The name is unique per actor. | ||
| * @return The name of the timer. | ||
| */ | ||
| public String getName() { | ||
| return this.name; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the time when timer is first due. | ||
| * @return Time as Duration when timer is first due. | ||
| */ | ||
| public Duration getDueTime() { | ||
| return this.dueTime; | ||
| } | ||
|
|
||
| /** | ||
| * @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. | ||
| */ | ||
| public Function<Object, Void> 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() | ||
| { | ||
|
artursouza marked this conversation as resolved.
|
||
| JSONObject j = new JSONObject(); | ||
| j.put("dueTime", ConverterUtils.ConvertDurationToDaprFormat(this.getDueTime())); | ||
| j.put("period", ConverterUtils.ConvertDurationToDaprFormat(this.getPeriod())); | ||
| return j.toString(); | ||
| } | ||
| } | ||
57 changes: 57 additions & 0 deletions
57
sdk/src/test/java/io/dapr/actors/runtime/ActorTimerImplTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.