From 8a8b36e9a921c79d5c6ebd1edd8aab638145bd00 Mon Sep 17 00:00:00 2001 From: Cassandra Coyle Date: Wed, 13 Aug 2025 10:49:02 -0500 Subject: [PATCH] sysout -> ctx.logger Signed-off-by: Cassandra Coyle --- .../java/io/dapr/examples/workflows/README.md | 69 ++++++++++--------- .../workflows/crossapp/CrossAppWorkflow.java | 16 ++--- 2 files changed, 43 insertions(+), 42 deletions(-) diff --git a/examples/src/main/java/io/dapr/examples/workflows/README.md b/examples/src/main/java/io/dapr/examples/workflows/README.md index a7754b2b9..7052391c6 100644 --- a/examples/src/main/java/io/dapr/examples/workflows/README.md +++ b/examples/src/main/java/io/dapr/examples/workflows/README.md @@ -684,51 +684,52 @@ Key Points: ### Cross-App Pattern -The cross-app pattern allows workflows to call activities that are hosted in different Dapr applications. This is useful for microservices architectures where activities are distributed across multiple services, or multi-tenant applications where activities are isolated by app ID. +The cross-app pattern allows workflows to call activities that are hosted in different Dapr applications. This is useful for microservices architectures allowing multiple applications to host activities that can be orchestrated by Dapr Workflows. The `CrossAppWorkflow` class defines the workflow. It demonstrates calling activities in different apps using the `appId` parameter in `WorkflowTaskOptions`. See the code snippet below: ```java public class CrossAppWorkflow implements Workflow { @Override public WorkflowStub create() { - return ctx -> { - System.out.println("=== WORKFLOW STARTING ==="); - ctx.getLogger().info("Starting CrossAppWorkflow: " + ctx.getName()); - System.out.println("Workflow name: " + ctx.getName()); - System.out.println("Workflow instance ID: " + ctx.getInstanceId()); + return ctx -> { + ctx.getLogger().info("=== WORKFLOW STARTING ==="); + ctx.getLogger().info("Starting CrossAppWorkflow: " + ctx.getName()); + ctx.getLogger().info("Workflow name: " + ctx.getName()); + ctx.getLogger().info("Workflow instance ID: " + ctx.getInstanceId()); - String input = ctx.getInput(String.class); - ctx.getLogger().info("CrossAppWorkflow received input: " + input); - System.out.println("Workflow input: " + input); + String input = ctx.getInput(String.class); + ctx.getLogger().info("CrossAppWorkflow received input: " + input); + ctx.getLogger().info("Workflow input: " + input); - // Call an activity in another app by passing in an active appID to the WorkflowTaskOptions - ctx.getLogger().info("Calling cross-app activity in 'app2'..."); - System.out.println("About to call cross-app activity in app2..."); - String crossAppResult = ctx.callActivity( - App2TransformActivity.class.getName(), - input, - new WorkflowTaskOptions("app2"), - String.class - ).await(); + // Call an activity in another app by passing in an active appID to the WorkflowTaskOptions + ctx.getLogger().info("Calling cross-app activity in 'app2'..."); + ctx.getLogger().info("About to call cross-app activity in app2..."); + String crossAppResult = ctx.callActivity( + App2TransformActivity.class.getName(), + input, + new WorkflowTaskOptions("app2"), + String.class + ).await(); - // Call another activity in a different app - ctx.getLogger().info("Calling cross-app activity in 'app3'..."); - System.out.println("About to call cross-app activity in app3..."); - String finalResult = ctx.callActivity( - App3FinalizeActivity.class.getName(), - crossAppResult, - new WorkflowTaskOptions("app3"), - String.class - ).await(); - ctx.getLogger().info("Final cross-app activity result: " + finalResult); - System.out.println("Final cross-app activity result: " + finalResult); - - ctx.getLogger().info("CrossAppWorkflow finished with: " + finalResult); - System.out.println("=== WORKFLOW COMPLETING WITH: " + finalResult + " ==="); - ctx.complete(finalResult); - }; + // Call another activity in a different app + ctx.getLogger().info("Calling cross-app activity in 'app3'..."); + ctx.getLogger().info("About to call cross-app activity in app3..."); + String finalResult = ctx.callActivity( + App3FinalizeActivity.class.getName(), + crossAppResult, + new WorkflowTaskOptions("app3"), + String.class + ).await(); + ctx.getLogger().info("Final cross-app activity result: " + finalResult); + ctx.getLogger().info("Final cross-app activity result: " + finalResult); + + ctx.getLogger().info("CrossAppWorkflow finished with: " + finalResult); + ctx.getLogger().info("=== WORKFLOW COMPLETING WITH: " + finalResult + " ==="); + ctx.complete(finalResult); + }; } } + ``` The `App2TransformActivity` class defines an activity in app2 that transforms the input string. See the code snippet below: diff --git a/examples/src/main/java/io/dapr/examples/workflows/crossapp/CrossAppWorkflow.java b/examples/src/main/java/io/dapr/examples/workflows/crossapp/CrossAppWorkflow.java index 61e27e8e8..32a02d7a8 100644 --- a/examples/src/main/java/io/dapr/examples/workflows/crossapp/CrossAppWorkflow.java +++ b/examples/src/main/java/io/dapr/examples/workflows/crossapp/CrossAppWorkflow.java @@ -25,18 +25,18 @@ public class CrossAppWorkflow implements Workflow { @Override public WorkflowStub create() { return ctx -> { - System.out.println("=== WORKFLOW STARTING ==="); + ctx.getLogger().info("=== WORKFLOW STARTING ==="); ctx.getLogger().info("Starting CrossAppWorkflow: " + ctx.getName()); - System.out.println("Workflow name: " + ctx.getName()); - System.out.println("Workflow instance ID: " + ctx.getInstanceId()); + ctx.getLogger().info("Workflow name: " + ctx.getName()); + ctx.getLogger().info("Workflow instance ID: " + ctx.getInstanceId()); String input = ctx.getInput(String.class); ctx.getLogger().info("CrossAppWorkflow received input: " + input); - System.out.println("Workflow input: " + input); + ctx.getLogger().info("Workflow input: " + input); // Call an activity in another app by passing in an active appID to the WorkflowTaskOptions ctx.getLogger().info("Calling cross-app activity in 'app2'..."); - System.out.println("About to call cross-app activity in app2..."); + ctx.getLogger().info("About to call cross-app activity in app2..."); String crossAppResult = ctx.callActivity( App2TransformActivity.class.getName(), input, @@ -46,7 +46,7 @@ public class CrossAppWorkflow implements Workflow { // Call another activity in a different app ctx.getLogger().info("Calling cross-app activity in 'app3'..."); - System.out.println("About to call cross-app activity in app3..."); + ctx.getLogger().info("About to call cross-app activity in app3..."); String finalResult = ctx.callActivity( App3FinalizeActivity.class.getName(), crossAppResult, @@ -54,10 +54,10 @@ public class CrossAppWorkflow implements Workflow { String.class ).await(); ctx.getLogger().info("Final cross-app activity result: " + finalResult); - System.out.println("Final cross-app activity result: " + finalResult); + ctx.getLogger().info("Final cross-app activity result: " + finalResult); ctx.getLogger().info("CrossAppWorkflow finished with: " + finalResult); - System.out.println("=== WORKFLOW COMPLETING WITH: " + finalResult + " ==="); + ctx.getLogger().info("=== WORKFLOW COMPLETING WITH: " + finalResult + " ==="); ctx.complete(finalResult); }; }