如何在Java SDK中创作和管理Dapr工作流

如何使用 Dapr Java SDK 启动和运行工作流

注意

Dapr工作流目前处于beta阶段。 查看已知限制 1.14.1.

让我们创建一个 Dapr 工作流,并使用控制台调用它。 通过提供的工作流示例,您将:

这个示例使用dapr init中的默认配置,在自托管模式下。

前期准备

设置环境

克隆Java SDK存储库并进入该存储库。

  1. git clone https://github.com/dapr/java-sdk.git
  2. cd java-sdk

运行以下命令使用 Dapr Java SDK 安装运行此工作流示例所需的依赖项。

  1. mvn clean install

从Java SDK根目录中,导航到Dapr Workflow示例。

  1. cd examples

运行DemoWorkflowWorker

DemoWorkflowWorker 类在 Dapr 的工作流运行时引擎中注册了 DemoWorkflow 的实现。 在DemoWorkflowWorker.java文件中,您可以找到DemoWorkflowWorker类和main方法:

  1. public class DemoWorkflowWorker {
  2. public static void main(String[] args) throws Exception {
  3. // Register the Workflow with the runtime.
  4. WorkflowRuntime.getInstance().registerWorkflow(DemoWorkflow.class);
  5. System.out.println("Start workflow runtime");
  6. WorkflowRuntime.getInstance().startAndBlock();
  7. System.exit(0);
  8. }
  9. }

在上面的代码中:

  • WorkflowRuntime.getInstance().registerWorkflow() 在 Dapr Workflow 运行时中将 DemoWorkflow 注册为一个工作流。
  • WorkflowRuntime.getInstance().start() 构建并启动 Dapr Workflow 运行时内的引擎。

在终端中执行以下命令来启动 DemoWorkflowWorker

  1. dapr run --app-id demoworkflowworker --resources-path ./components/workflows --dapr-grpc-port 50001 -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.DemoWorkflowWorker

预期输出

  1. You're up and running! Both Dapr and your app logs will appear here.
  2. ...
  3. == APP == Start workflow runtime
  4. == APP == Sep 13, 2023 9:02:03 AM com.microsoft.durabletask.DurableTaskGrpcWorker startAndBlock
  5. == APP == INFO: Durable Task worker is connecting to sidecar at 127.0.0.1:50001.

运行 DemoWorkflowClient

DemoWorkflowClient 启动已在 Dapr 中注册的工作流实例。

  1. public class DemoWorkflowClient {
  2. // ...
  3. public static void main(String[] args) throws InterruptedException {
  4. DaprWorkflowClient client = new DaprWorkflowClient();
  5. try (client) {
  6. String separatorStr = "*******";
  7. System.out.println(separatorStr);
  8. String instanceId = client.scheduleNewWorkflow(DemoWorkflow.class, "input data");
  9. System.out.printf("Started new workflow instance with random ID: %s%n", instanceId);
  10. System.out.println(separatorStr);
  11. System.out.println("**GetInstanceMetadata:Running Workflow**");
  12. WorkflowInstanceStatus workflowMetadata = client.getInstanceState(instanceId, true);
  13. System.out.printf("Result: %s%n", workflowMetadata);
  14. System.out.println(separatorStr);
  15. System.out.println("**WaitForInstanceStart**");
  16. try {
  17. WorkflowInstanceStatus waitForInstanceStartResult =
  18. client.waitForInstanceStart(instanceId, Duration.ofSeconds(60), true);
  19. System.out.printf("Result: %s%n", waitForInstanceStartResult);
  20. } catch (TimeoutException ex) {
  21. System.out.printf("waitForInstanceStart has an exception:%s%n", ex);
  22. }
  23. System.out.println(separatorStr);
  24. System.out.println("**SendExternalMessage**");
  25. client.raiseEvent(instanceId, "TestEvent", "TestEventPayload");
  26. System.out.println(separatorStr);
  27. System.out.println("** Registering parallel Events to be captured by allOf(t1,t2,t3) **");
  28. client.raiseEvent(instanceId, "event1", "TestEvent 1 Payload");
  29. client.raiseEvent(instanceId, "event2", "TestEvent 2 Payload");
  30. client.raiseEvent(instanceId, "event3", "TestEvent 3 Payload");
  31. System.out.printf("Events raised for workflow with instanceId: %s\n", instanceId);
  32. System.out.println(separatorStr);
  33. System.out.println("** Registering Event to be captured by anyOf(t1,t2,t3) **");
  34. client.raiseEvent(instanceId, "e2", "event 2 Payload");
  35. System.out.printf("Event raised for workflow with instanceId: %s\n", instanceId);
  36. System.out.println(separatorStr);
  37. System.out.println("**WaitForInstanceCompletion**");
  38. try {
  39. WorkflowInstanceStatus waitForInstanceCompletionResult =
  40. client.waitForInstanceCompletion(instanceId, Duration.ofSeconds(60), true);
  41. System.out.printf("Result: %s%n", waitForInstanceCompletionResult);
  42. } catch (TimeoutException ex) {
  43. System.out.printf("waitForInstanceCompletion has an exception:%s%n", ex);
  44. }
  45. System.out.println(separatorStr);
  46. System.out.println("**purgeInstance**");
  47. boolean purgeResult = client.purgeInstance(instanceId);
  48. System.out.printf("purgeResult: %s%n", purgeResult);
  49. System.out.println(separatorStr);
  50. System.out.println("**raiseEvent**");
  51. String eventInstanceId = client.scheduleNewWorkflow(DemoWorkflow.class);
  52. System.out.printf("Started new workflow instance with random ID: %s%n", eventInstanceId);
  53. client.raiseEvent(eventInstanceId, "TestException", null);
  54. System.out.printf("Event raised for workflow with instanceId: %s\n", eventInstanceId);
  55. System.out.println(separatorStr);
  56. String instanceToTerminateId = "terminateMe";
  57. client.scheduleNewWorkflow(DemoWorkflow.class, null, instanceToTerminateId);
  58. System.out.printf("Started new workflow instance with specified ID: %s%n", instanceToTerminateId);
  59. TimeUnit.SECONDS.sleep(5);
  60. System.out.println("Terminate this workflow instance manually before the timeout is reached");
  61. client.terminateWorkflow(instanceToTerminateId, null);
  62. System.out.println(separatorStr);
  63. String restartingInstanceId = "restarting";
  64. client.scheduleNewWorkflow(DemoWorkflow.class, null, restartingInstanceId);
  65. System.out.printf("Started new workflow instance with ID: %s%n", restartingInstanceId);
  66. System.out.println("Sleeping 30 seconds to restart the workflow");
  67. TimeUnit.SECONDS.sleep(30);
  68. System.out.println("**SendExternalMessage: RestartEvent**");
  69. client.raiseEvent(restartingInstanceId, "RestartEvent", "RestartEventPayload");
  70. System.out.println("Sleeping 30 seconds to terminate the eternal workflow");
  71. TimeUnit.SECONDS.sleep(30);
  72. client.terminateWorkflow(restartingInstanceId, null);
  73. }
  74. System.out.println("Exiting DemoWorkflowClient.");
  75. System.exit(0);
  76. }
  77. }

在第二个终端窗口中,通过运行以下命令来启动工作流:

  1. java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.DemoWorkflowClient

预期输出

  1. *******
  2. Started new workflow instance with random ID: 0b4cc0d5-413a-4c1c-816a-a71fa24740d4
  3. *******
  4. **GetInstanceMetadata:Running Workflow**
  5. Result: [Name: 'io.dapr.examples.workflows.DemoWorkflow', ID: '0b4cc0d5-413a-4c1c-816a-a71fa24740d4', RuntimeStatus: RUNNING, CreatedAt: 2023-09-13T13:02:30.547Z, LastUpdatedAt: 2023-09-13T13:02:30.699Z, Input: '"input data"', Output: '']
  6. *******
  7. **WaitForInstanceStart**
  8. Result: [Name: 'io.dapr.examples.workflows.DemoWorkflow', ID: '0b4cc0d5-413a-4c1c-816a-a71fa24740d4', RuntimeStatus: RUNNING, CreatedAt: 2023-09-13T13:02:30.547Z, LastUpdatedAt: 2023-09-13T13:02:30.699Z, Input: '"input data"', Output: '']
  9. *******
  10. **SendExternalMessage**
  11. *******
  12. ** Registering parallel Events to be captured by allOf(t1,t2,t3) **
  13. Events raised for workflow with instanceId: 0b4cc0d5-413a-4c1c-816a-a71fa24740d4
  14. *******
  15. ** Registering Event to be captured by anyOf(t1,t2,t3) **
  16. Event raised for workflow with instanceId: 0b4cc0d5-413a-4c1c-816a-a71fa24740d4
  17. *******
  18. **WaitForInstanceCompletion**
  19. Result: [Name: 'io.dapr.examples.workflows.DemoWorkflow', ID: '0b4cc0d5-413a-4c1c-816a-a71fa24740d4', RuntimeStatus: FAILED, CreatedAt: 2023-09-13T13:02:30.547Z, LastUpdatedAt: 2023-09-13T13:02:55.054Z, Input: '"input data"', Output: '']
  20. *******
  21. **purgeInstance**
  22. purgeResult: true
  23. *******
  24. **raiseEvent**
  25. Started new workflow instance with random ID: 7707d141-ebd0-4e54-816e-703cb7a52747
  26. Event raised for workflow with instanceId: 7707d141-ebd0-4e54-816e-703cb7a52747
  27. *******
  28. Started new workflow instance with specified ID: terminateMe
  29. Terminate this workflow instance manually before the timeout is reached
  30. *******
  31. Started new workflow instance with ID: restarting
  32. Sleeping 30 seconds to restart the workflow
  33. **SendExternalMessage: RestartEvent**
  34. Sleeping 30 seconds to terminate the eternal workflow
  35. Exiting DemoWorkflowClient.

发生了什么?

  1. 当你运行 dapr run 时,工作流工作者会将工作流(DemoWorkflow)及其活动注册到 Dapr 工作流引擎中。
  2. 当你运行java时,工作流客户端使用以下活动启动了工作流实例。 您可以在您运行 dapr run 的终端中跟踪输出。
    1. 工作流程已启动,创建三个并行任务,并等待它们完成。
    2. 工作流客户端调用该活动,并将”Hello Activity”消息发送到控制台。
    3. 工作流超时并被清除。
    4. 工作流客户端使用一个随机ID启动一个新的工作流实例,使用另一个名为terminateMe的工作流实例终止它,并重新启动一个名为restarting的工作流实例。
    5. 然后退出工作流客户端。

下一步