From 88a109b3d2110e5b256045506cf219189039bc60 Mon Sep 17 00:00:00 2001 From: Hannah Hunter <94493363+hhunter-ms@users.noreply.github.com> Date: Wed, 4 Oct 2023 18:59:29 -0400 Subject: [PATCH] add java monitor pattern example (#3774) Signed-off-by: Hannah Hunter Co-authored-by: Mark Fussell --- .../workflow/workflow-patterns.md | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/daprdocs/content/en/developing-applications/building-blocks/workflow/workflow-patterns.md b/daprdocs/content/en/developing-applications/building-blocks/workflow/workflow-patterns.md index 49b5fc2db..9d23a6406 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/workflow/workflow-patterns.md +++ b/daprdocs/content/en/developing-applications/building-blocks/workflow/workflow-patterns.md @@ -464,7 +464,41 @@ public override async Task RunAsync(WorkflowContext context, MyEntitySta ```java -todo +public class MonitorWorkflow extends Workflow { + + @Override + public WorkflowStub create() { + return ctx -> { + + Duration nextSleepInterval; + + var status = ctx.callActivity(DemoWorkflowStatusActivity.class.getName(), DemoStatusActivityOutput.class).await(); + var isHealthy = status.getIsHealthy(); + + if (isHealthy) { + // Check less frequently when in a healthy state + nextSleepInterval = Duration.ofMinutes(60); + } else { + + ctx.callActivity(DemoWorkflowAlertActivity.class.getName()).await(); + + // Check more frequently when in an unhealthy state + nextSleepInterval = Duration.ofMinutes(5); + } + + // Put the workflow to sleep until the determined time + // Note: ctx.createTimer() method is not supported in the Java SDK yet + try { + TimeUnit.SECONDS.sleep(nextSleepInterval.getSeconds()); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + // Restart from the beginning with the updated state + ctx.continueAsNew(); + } + } +} ``` {{% /codetab %}}