diff --git a/sdkdocs/java/content/en/java-sdk-docs/spring-boot/_index.md b/sdkdocs/java/content/en/java-sdk-docs/spring-boot/_index.md index fcfaacd1a6b..506ddcb7bad 100644 --- a/sdkdocs/java/content/en/java-sdk-docs/spring-boot/_index.md +++ b/sdkdocs/java/content/en/java-sdk-docs/spring-boot/_index.md @@ -24,14 +24,14 @@ If you already have a Spring Boot application, you can directly add the followin ``` - io.dapr.spring + io.dapr.spring dapr-spring-boot-starter - 0.16.0 + 1.16.0 io.dapr.spring dapr-spring-boot-starter-test - 0.16.0 + 1.16.0 test ``` @@ -292,56 +292,12 @@ public static void setup(){ You can check and run the [full example source code here](https://github.com/salaboy/dapr-spring-boot-docs-examples). -## Using Dapr Workflows with Spring Boot - -Following the same approach that we used for Spring Data and Spring Messaging, the `dapr-spring-boot-starter` brings Dapr Workflow integration for Spring Boot users. - -To work with Dapr Workflows you need to define and implement your workflows using code. The Dapr Spring Boot Starter makes your life easier by managing `Workflow`s and `WorkflowActivity`s as Spring beans. - -In order to enable the automatic bean discovery you can annotate your `@SpringBootApplication` with the `@EnableDaprWorkflows` annotation: - -``` -@SpringBootApplication -@EnableDaprWorkflows -public class MySpringBootApplication {} -``` - -By adding this annotation, all the `WorkflowActivity`s will be automatically managed by Spring and registered to the workflow engine. - -By having all `WorkflowActivity`s as managed beans we can use Spring `@Autowired` mechanism to inject any bean that our workflow activity might need to implement its functionality, for example the `@RestTemplate`: - -``` -public class MyWorkflowActivity implements WorkflowActivity { - - @Autowired - private RestTemplate restTemplate; -``` - -You can also `@Autowired` the `DaprWorkflowClient` to create new instances of your workflows. - -``` -@Autowired -private DaprWorkflowClient daprWorkflowClient; -``` - -This enable applications to schedule new workflow instances and raise events. - -``` -String instanceId = daprWorkflowClient.scheduleNewWorkflow(MyWorkflow.class, payload); -``` - -and - -``` -daprWorkflowClient.raiseEvent(instanceId, "MyEvenet", event); -``` - -Check the [Dapr Workflow documentation](https://docs.dapr.io/developing-applications/building-blocks/workflow/workflow-overview/) for more information about how to work with Dapr Workflows. - ## Next steps Learn more about the [Dapr Java SDK packages available to add to your Java applications](https://dapr.github.io/java-sdk/). +Check the How To guide for [Dapr Workflows using Spring Boot and Testcontainers for a local workflow development experience](sb-workflows-howto.md). + ## Related links - [Java SDK examples](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples) diff --git a/sdkdocs/java/content/en/java-sdk-docs/spring-boot/sb-workflows-howto.md b/sdkdocs/java/content/en/java-sdk-docs/spring-boot/sb-workflows-howto.md new file mode 100644 index 00000000000..4fcd973db2b --- /dev/null +++ b/sdkdocs/java/content/en/java-sdk-docs/spring-boot/sb-workflows-howto.md @@ -0,0 +1,86 @@ +--- +type: docs +title: "How to: Author and manage Dapr Workflow with Spring Boot" +linkTitle: "How to: Author and manage workflows with Spring Boot" +weight: 40000 +description: How to get up and running with workflows using the Spring Boot integration +--- + + +Following the same approach that we used for Spring Data and Spring Messaging, the [`dapr-spring-boot-starter`](_index.md) brings Dapr Workflow integration for Spring Boot users. + +With Dapr Workflows you define complex orchestrations (workflows) in Java code. The Dapr Spring Boot Starter makes your development easier by managing `Workflow`s and `WorkflowActivity`s as Spring Beans. + +In order to enable the automatic bean discovery you annotate your `@SpringBootApplication` with the `@EnableDaprWorkflows` annotation: + +``` +@SpringBootApplication +@EnableDaprWorkflows +public class MySpringBootApplication { + ... +} +``` + +By adding this annotation, all the `Workflow`s and `WorkflowActivity`s beans are automatically discovered by Spring and registered to the workflow engine. + +## Creating Workflows and Activities + +Inside your Spring Boot application you can define as many workflows as you want. You do that by creating new implementations of the `Workflow` interface. + +``` +@Component +public class MyWorkflow implements Workflow { + + @Override + public WorkflowStub create() { + return ctx -> { + + }; + } + +} +``` + +From inside your workflow definitions, you can perform service to service interactions, schedule timers or receive external events. + +By having all `WorkflowActivity`s as managed beans you can use the Spring `@Autowired` mechanism to inject any bean that the workflow activity might need to implement its functionality. For example the `@RestTemplate`: + +``` +@Component +public class MyWorkflowActivity implements WorkflowActivity { + + @Autowired + private RestTemplate restTemplate; +``` + +## Creating and interacting with Workflows + + +To create and interact with Workflow instances you use the `DaprWorkflowClient` that you can also `@Autowired`. + +``` +@Autowired +private DaprWorkflowClient daprWorkflowClient; +``` + +Applications can now schedule new workflow instances and raise events. + +``` +String instanceId = daprWorkflowClient.scheduleNewWorkflow(MyWorkflow.class, payload); +``` + +and + +``` +daprWorkflowClient.raiseEvent(instanceId, "MyEvenet", event); +``` + +[Check a full example here](https://github.com/dapr/java-sdk/blob/master/spring-boot-examples/workflows/patterns/src/main/java/io/dapr/springboot/examples/wfp/chain/ChainWorkflow.java) + + + +## Next Steps & Resources + +Check the blog post from [Baeldung covering Dapr Workflows and Dapr Pubsub](https://www.baeldung.com/dapr-workflows-pubsub) with a full working example. + +Check the [Dapr Workflow documentation](https://docs.dapr.io/developing-applications/building-blocks/workflow/workflow-overview/) for more information about how to work with Dapr Workflows. \ No newline at end of file