mirror of https://github.com/dapr/java-sdk.git
cross app ex
Signed-off-by: Cassandra Coyle <cassie@diagrid.io>
This commit is contained in:
parent
a160717c91
commit
8f453d6809
|
|
@ -53,6 +53,7 @@ Those examples contain the following workflow patterns:
|
|||
4. [External Event Pattern](#external-event-pattern)
|
||||
5. [Child-workflow Pattern](#child-workflow-pattern)
|
||||
6. [Compensation Pattern](#compensation-pattern)
|
||||
7. [Cross-App Pattern](#cross-app-pattern)
|
||||
|
||||
### Chaining Pattern
|
||||
In the chaining pattern, a sequence of activities executes in a specific order.
|
||||
|
|
@ -681,6 +682,212 @@ Key Points:
|
|||
4. Each activity simulates work with a short delay for demonstration purposes
|
||||
|
||||
|
||||
### 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, service mesh scenarios, or multi-tenant applications where activities are isolated by app ID.
|
||||
|
||||
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());
|
||||
|
||||
String input = ctx.getInput(String.class);
|
||||
ctx.getLogger().info("CrossAppWorkflow received input: " + input);
|
||||
System.out.println("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 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);
|
||||
};
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The `App2TransformActivity` class defines an activity in app2 that transforms the input string. See the code snippet below:
|
||||
```java
|
||||
public class App2TransformActivity implements WorkflowActivity {
|
||||
@Override
|
||||
public Object run(WorkflowActivityContext ctx) {
|
||||
System.out.println("=== App2: TransformActivity called ===");
|
||||
String input = ctx.getInput(String.class);
|
||||
System.out.println("Input: " + input);
|
||||
|
||||
// Transform the input
|
||||
String result = input.toUpperCase() + " [TRANSFORMED BY APP2]";
|
||||
|
||||
System.out.println("Output: " + result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The `App3FinalizeActivity` class defines an activity in app3 that finalizes the processing. See the code snippet below:
|
||||
```java
|
||||
public class App3FinalizeActivity implements WorkflowActivity {
|
||||
@Override
|
||||
public Object run(WorkflowActivityContext ctx) {
|
||||
System.out.println("=== App3: FinalizeActivity called ===");
|
||||
String input = ctx.getInput(String.class);
|
||||
System.out.println("Input: " + input);
|
||||
|
||||
// Finalize the processing
|
||||
String result = input + " [FINALIZED BY APP3]";
|
||||
|
||||
System.out.println("Output: " + result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**WorkflowTaskOptions Constructors for Cross-App Calls:**
|
||||
|
||||
The `WorkflowTaskOptions` class supports several constructors for cross-app calls:
|
||||
|
||||
```java
|
||||
// App ID only
|
||||
new WorkflowTaskOptions("app2")
|
||||
|
||||
// Retry policy + app ID
|
||||
new WorkflowTaskOptions(retryPolicy, "app2")
|
||||
|
||||
// Retry handler + app ID
|
||||
new WorkflowTaskOptions(retryHandler, "app2")
|
||||
|
||||
// All parameters
|
||||
new WorkflowTaskOptions(retryPolicy, retryHandler, "app2")
|
||||
```
|
||||
|
||||
**Key Features:**
|
||||
- **Cross-app activity calls**: Call activities in different Dapr applications specifying the appID in the WorkflowTaskOptions
|
||||
- **WorkflowTaskOptions with appId**: Specify which app should handle the activity
|
||||
- **Combined with retry policies**: Use app ID along with retry policies and handlers
|
||||
- **Error handling**: Works the same as local activity calls
|
||||
|
||||
**Requirements:**
|
||||
- Multiple Dapr applications running with different app IDs
|
||||
- Activities registered in the target applications
|
||||
- Proper Dapr workflow runtime configuration
|
||||
|
||||
**Important Limitations:**
|
||||
- **Cross-app calls are currently supported for activities only**
|
||||
- **Child workflow cross-app calls (suborchestration) are NOT supported**
|
||||
- The app ID must match the Dapr application ID of the target service
|
||||
|
||||
**Running the Cross-App Example:**
|
||||
|
||||
This example requires running multiple Dapr applications simultaneously. You'll need to run the following commands in separate terminals:
|
||||
|
||||
1. **Start the main workflow worker (crossapp-worker):**
|
||||
```sh
|
||||
dapr run --app-id crossapp-worker --resources-path ./components/workflows --dapr-grpc-port 50001 --log-level=debug -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.crossapp.CrossAppWorker
|
||||
```
|
||||
|
||||
2. **Start app2 worker (handles App2TransformActivity):**
|
||||
```sh
|
||||
dapr run --app-id app2 --resources-path ./components/workflows --dapr-grpc-port 50002 --log-level=debug -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.crossapp.App2Worker
|
||||
```
|
||||
|
||||
3. **Start app3 worker (handles App3FinalizeActivity):**
|
||||
```sh
|
||||
dapr run --app-id app3 --resources-path ./components/workflows --dapr-grpc-port 50003 --log-level=debug -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.crossapp.App3Worker
|
||||
```
|
||||
|
||||
4. **Run the workflow client:**
|
||||
```sh
|
||||
java -Djava.util.logging.ConsoleHandler.level=FINE -Dio.dapr.durabletask.level=FINE -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.crossapp.CrossAppWorkflowClient "Hello World"
|
||||
```
|
||||
|
||||
<!-- STEP
|
||||
name: Run Cross-App Pattern workflow
|
||||
match_order: none
|
||||
output_match_mode: substring
|
||||
expected_stdout_lines:
|
||||
- "=== Starting Cross-App Workflow Client ==="
|
||||
- "Input: Hello World"
|
||||
- "Created DaprWorkflowClient successfully"
|
||||
- "Attempting to start new workflow..."
|
||||
- "Started a new cross-app workflow with instance ID:"
|
||||
- "Waiting for workflow completion..."
|
||||
- "Workflow instance with ID:"
|
||||
- "completed with result: HELLO WORLD [TRANSFORMED BY APP2] [FINALIZED BY APP3]"
|
||||
- "=== WORKFLOW STARTING ==="
|
||||
- "Starting CrossAppWorkflow:"
|
||||
- "Workflow name:"
|
||||
- "Workflow instance ID:"
|
||||
- "CrossAppWorkflow received input: Hello World"
|
||||
- "Workflow input: Hello World"
|
||||
- "Calling cross-app activity in 'app2'..."
|
||||
- "About to call cross-app activity in app2..."
|
||||
- "=== App2: TransformActivity called ==="
|
||||
- "Input: Hello World"
|
||||
- "Output: HELLO WORLD [TRANSFORMED BY APP2]"
|
||||
- "Calling cross-app activity in 'app3'..."
|
||||
- "About to call cross-app activity in app3..."
|
||||
- "=== App3: FinalizeActivity called ==="
|
||||
- "Input: HELLO WORLD [TRANSFORMED BY APP2]"
|
||||
- "Output: HELLO WORLD [TRANSFORMED BY APP2] [FINALIZED BY APP3]"
|
||||
- "Final cross-app activity result:"
|
||||
- "CrossAppWorkflow finished with:"
|
||||
- "=== WORKFLOW COMPLETING WITH:"
|
||||
background: true
|
||||
sleep: 60
|
||||
timeout_seconds: 60
|
||||
-->
|
||||
|
||||
<!-- END_STEP -->
|
||||
|
||||
**Expected Output:**
|
||||
|
||||
The client will show:
|
||||
```text
|
||||
=== Starting Cross-App Workflow Client ===
|
||||
Input: Hello World
|
||||
Created DaprWorkflowClient successfully
|
||||
Attempting to start new workflow...
|
||||
Started a new cross-app workflow with instance ID: 001113f3-b9d9-438c-932a-a9a9b70b9460
|
||||
Waiting for workflow completion...
|
||||
Workflow instance with ID: 001113f3-b9d9-438c-932a-a9a9b70b9460 completed with result: HELLO WORLD [TRANSFORMED BY APP2] [FINALIZED BY APP3]
|
||||
```
|
||||
|
||||
The workflow demonstrates:
|
||||
1. The workflow starts in the main worker (crossapp-worker)
|
||||
2. Calls an activity in 'app2' using cross-app functionality
|
||||
3. Calls an activity in 'app3' using cross-app functionality
|
||||
4. The workflow completes with the final result from all activities
|
||||
|
||||
This pattern is particularly useful for:
|
||||
- Microservices architectures where activities are distributed across multiple services
|
||||
- Service mesh scenarios where different apps handle different types of activities
|
||||
- Multi-tenant applications where activities are isolated by app ID
|
||||
|
||||
### Suspend/Resume Pattern
|
||||
|
||||
Workflow instances can be suspended and resumed. This example shows how to use the suspend and resume commands.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright 2025 The Dapr Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package io.dapr.examples.workflows.crossapp;
|
||||
|
||||
import io.dapr.workflows.WorkflowActivity;
|
||||
import io.dapr.workflows.WorkflowActivityContext;
|
||||
|
||||
/**
|
||||
* TransformActivity for App2 - transforms input to uppercase.
|
||||
* This activity is called cross-app from the main workflow.
|
||||
*/
|
||||
public class App2TransformActivity implements WorkflowActivity {
|
||||
@Override
|
||||
public Object run(WorkflowActivityContext context) {
|
||||
String input = context.getInput(String.class);
|
||||
System.out.println("=== App2: TransformActivity called ===");
|
||||
System.out.println("Input: " + input);
|
||||
String result = input.toUpperCase() + " [TRANSFORMED BY APP2]";
|
||||
System.out.println("Output: " + result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright 2025 The Dapr Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package io.dapr.examples.workflows.crossapp;
|
||||
|
||||
import io.dapr.workflows.runtime.WorkflowRuntime;
|
||||
import io.dapr.workflows.runtime.WorkflowRuntimeBuilder;
|
||||
|
||||
/**
|
||||
* App2 Worker - registers only the TransformActivity.
|
||||
* This app will handle cross-app activity calls from the main workflow.
|
||||
*/
|
||||
public class App2Worker {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.out.println("=== Starting App2Worker ===");
|
||||
// Register the Workflow with the builder
|
||||
WorkflowRuntimeBuilder builder = new WorkflowRuntimeBuilder()
|
||||
.registerActivity(App2TransformActivity.class);
|
||||
|
||||
// Build and start the workflow runtime
|
||||
try (WorkflowRuntime runtime = builder.build()) {
|
||||
System.out.println("App2 is ready to receive cross-app activity calls...");
|
||||
runtime.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright 2025 The Dapr Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package io.dapr.examples.workflows.crossapp;
|
||||
|
||||
import io.dapr.workflows.WorkflowActivity;
|
||||
import io.dapr.workflows.WorkflowActivityContext;
|
||||
|
||||
/**
|
||||
* FinalizeActivity for App3 - adds final processing.
|
||||
* This activity is called cross-app from the main workflow.
|
||||
*/
|
||||
public class App3FinalizeActivity implements WorkflowActivity {
|
||||
@Override
|
||||
public Object run(WorkflowActivityContext context) {
|
||||
String input = context.getInput(String.class);
|
||||
System.out.println("=== App3: FinalizeActivity called ===");
|
||||
System.out.println("Input: " + input);
|
||||
String result = input + " [FINALIZED BY APP3]";
|
||||
System.out.println("Output: " + result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright 2025 The Dapr Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package io.dapr.examples.workflows.crossapp;
|
||||
|
||||
import io.dapr.workflows.runtime.WorkflowRuntime;
|
||||
import io.dapr.workflows.runtime.WorkflowRuntimeBuilder;
|
||||
|
||||
/**
|
||||
* App3 Worker - registers only the FinalizeActivity.
|
||||
* This app will handle cross-app activity calls from the main workflow.
|
||||
*/
|
||||
public class App3Worker {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.out.println("=== Starting App3Worker ===");
|
||||
// Register the Workflow with the builder
|
||||
WorkflowRuntimeBuilder builder = new WorkflowRuntimeBuilder()
|
||||
.registerActivity(App3FinalizeActivity.class);
|
||||
|
||||
// Build and start the workflow runtime
|
||||
try (WorkflowRuntime runtime = builder.build()) {
|
||||
System.out.println("App3 is ready to receive cross-app activity calls...");
|
||||
runtime.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Copyright 2025 The Dapr Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package io.dapr.examples.workflows.crossapp;
|
||||
|
||||
import io.dapr.workflows.runtime.WorkflowRuntime;
|
||||
import io.dapr.workflows.runtime.WorkflowRuntimeBuilder;
|
||||
|
||||
public class CrossAppWorker {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// Register the Workflow with the builder
|
||||
WorkflowRuntimeBuilder builder = new WorkflowRuntimeBuilder()
|
||||
.registerWorkflow(CrossAppWorkflow.class);
|
||||
|
||||
// Build and start the workflow runtime
|
||||
try (WorkflowRuntime runtime = builder.build()) {
|
||||
System.out.println("CrossAppWorker started - registered CrossAppWorkflow only");
|
||||
runtime.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* Copyright 2025 The Dapr Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package io.dapr.examples.workflows.crossapp;
|
||||
|
||||
import io.dapr.workflows.Workflow;
|
||||
import io.dapr.workflows.WorkflowStub;
|
||||
import io.dapr.workflows.WorkflowTaskOptions;
|
||||
|
||||
/**
|
||||
* Example workflow that demonstrates cross-app activity calls.
|
||||
* This workflow calls activities in different apps using the appId parameter.
|
||||
*/
|
||||
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());
|
||||
|
||||
String input = ctx.getInput(String.class);
|
||||
ctx.getLogger().info("CrossAppWorkflow received input: " + input);
|
||||
System.out.println("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 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);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright 2025 The Dapr Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package io.dapr.examples.workflows.crossapp;
|
||||
|
||||
import io.dapr.workflows.client.DaprWorkflowClient;
|
||||
import io.dapr.workflows.client.WorkflowInstanceStatus;
|
||||
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
/**
|
||||
* Cross-App Workflow Client - starts and monitors workflows.
|
||||
*
|
||||
* 1. Create a workflow client
|
||||
* 2. Start a new workflow instance
|
||||
* 3. Wait for completion and get results
|
||||
*/
|
||||
public class CrossAppWorkflowClient {
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (args.length < 1) {
|
||||
System.out.println("Usage: CrossAppWorkflowClientExample <input>");
|
||||
System.out.println("Example: CrossAppWorkflowClientExample \"Hello World\"");
|
||||
return;
|
||||
}
|
||||
|
||||
String input = args[0];
|
||||
System.out.println("=== Starting Cross-App Workflow Client ===");
|
||||
System.out.println("Input: " + input);
|
||||
|
||||
try (DaprWorkflowClient client = new DaprWorkflowClient()) {
|
||||
System.out.println("Created DaprWorkflowClient successfully");
|
||||
|
||||
// Start a new workflow instance
|
||||
System.out.println("Attempting to start new workflow...");
|
||||
String instanceId = client.scheduleNewWorkflow(CrossAppWorkflow.class, input);
|
||||
System.out.printf("Started a new cross-app workflow with instance ID: %s%n", instanceId);
|
||||
|
||||
// Wait for the workflow to complete
|
||||
System.out.println("Waiting for workflow completion...");
|
||||
WorkflowInstanceStatus workflowInstanceStatus =
|
||||
client.waitForInstanceCompletion(instanceId, null, true);
|
||||
|
||||
// Get the result
|
||||
String result = workflowInstanceStatus.readOutputAs(String.class);
|
||||
System.out.printf("Workflow instance with ID: %s completed with result: %s%n", instanceId, result);
|
||||
|
||||
} catch (TimeoutException | InterruptedException e) {
|
||||
System.err.println("Error waiting for workflow completion: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error creating workflow client or starting workflow: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -17,18 +17,48 @@ public class WorkflowTaskOptions {
|
|||
|
||||
private final WorkflowTaskRetryPolicy retryPolicy;
|
||||
private final WorkflowTaskRetryHandler retryHandler;
|
||||
private final String appId;
|
||||
|
||||
public WorkflowTaskOptions(WorkflowTaskRetryPolicy retryPolicy, WorkflowTaskRetryHandler retryHandler) {
|
||||
this.retryPolicy = retryPolicy;
|
||||
this.retryHandler = retryHandler;
|
||||
this(retryPolicy, retryHandler, null);
|
||||
}
|
||||
|
||||
public WorkflowTaskOptions(WorkflowTaskRetryPolicy retryPolicy) {
|
||||
this(retryPolicy, null);
|
||||
this(retryPolicy, null, null);
|
||||
}
|
||||
|
||||
public WorkflowTaskOptions(WorkflowTaskRetryHandler retryHandler) {
|
||||
this(null, retryHandler);
|
||||
this(null, retryHandler, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for WorkflowTaskOptions with app ID for cross-app calls.
|
||||
*
|
||||
* @param appId the ID of the app to call the activity in
|
||||
*/
|
||||
public WorkflowTaskOptions(String appId) {
|
||||
this(null, null, appId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for WorkflowTaskOptions with retry policy, retry handler, and app ID.
|
||||
*
|
||||
* @param retryPolicy the retry policy
|
||||
* @param retryHandler the retry handler
|
||||
* @param appId the app ID for cross-app activity calls
|
||||
*/
|
||||
public WorkflowTaskOptions(WorkflowTaskRetryPolicy retryPolicy, WorkflowTaskRetryHandler retryHandler, String appId) {
|
||||
this.retryPolicy = retryPolicy;
|
||||
this.retryHandler = retryHandler;
|
||||
this.appId = appId;
|
||||
}
|
||||
|
||||
public WorkflowTaskOptions(WorkflowTaskRetryPolicy retryPolicy, String appId) {
|
||||
this(retryPolicy, null, appId);
|
||||
}
|
||||
|
||||
public WorkflowTaskOptions(WorkflowTaskRetryHandler retryHandler, String appId) {
|
||||
this(null, retryHandler, appId);
|
||||
}
|
||||
|
||||
public WorkflowTaskRetryPolicy getRetryPolicy() {
|
||||
|
|
@ -39,4 +69,8 @@ public class WorkflowTaskOptions {
|
|||
return retryHandler;
|
||||
}
|
||||
|
||||
public String getAppId() {
|
||||
return appId;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -246,7 +246,7 @@ public class DefaultWorkflowContext implements WorkflowContext {
|
|||
RetryPolicy retryPolicy = toRetryPolicy(options.getRetryPolicy());
|
||||
RetryHandler retryHandler = toRetryHandler(options.getRetryHandler());
|
||||
|
||||
return new TaskOptions(retryPolicy, retryHandler);
|
||||
return new TaskOptions(retryPolicy, retryHandler, options.getAppId());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue