Skip to content

Commit eddbdf5

Browse files
committed
separating Dapr Workflows from Spring Boot
1 parent 405497c commit eddbdf5

File tree

2 files changed

+84
-49
lines changed

2 files changed

+84
-49
lines changed

sdkdocs/java/content/en/java-sdk-docs/spring-boot/_index.md

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ If you already have a Spring Boot application, you can directly add the followin
2424

2525
```
2626
<dependency>
27-
<groupId>io.dapr.spring</groupId>
27+
<groupId>io.dapr.spring</groupId>
2828
<artifactId>dapr-spring-boot-starter</artifactId>
29-
<version>0.16.0</version>
29+
<version>1.16.0</version>
3030
</dependency>
3131
<dependency>
3232
<groupId>io.dapr.spring</groupId>
3333
<artifactId>dapr-spring-boot-starter-test</artifactId>
34-
<version>0.16.0</version>
34+
<version>1.16.0</version>
3535
<scope>test</scope>
3636
</dependency>
3737
```
@@ -292,56 +292,12 @@ public static void setup(){
292292

293293
You can check and run the [full example source code here](https://github.com/salaboy/dapr-spring-boot-docs-examples).
294294

295-
## Using Dapr Workflows with Spring Boot
296-
297-
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.
298-
299-
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.
300-
301-
In order to enable the automatic bean discovery you can annotate your `@SpringBootApplication` with the `@EnableDaprWorkflows` annotation:
302-
303-
```
304-
@SpringBootApplication
305-
@EnableDaprWorkflows
306-
public class MySpringBootApplication {}
307-
```
308-
309-
By adding this annotation, all the `WorkflowActivity`s will be automatically managed by Spring and registered to the workflow engine.
310-
311-
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`:
312-
313-
```
314-
public class MyWorkflowActivity implements WorkflowActivity {
315-
316-
@Autowired
317-
private RestTemplate restTemplate;
318-
```
319-
320-
You can also `@Autowired` the `DaprWorkflowClient` to create new instances of your workflows.
321-
322-
```
323-
@Autowired
324-
private DaprWorkflowClient daprWorkflowClient;
325-
```
326-
327-
This enable applications to schedule new workflow instances and raise events.
328-
329-
```
330-
String instanceId = daprWorkflowClient.scheduleNewWorkflow(MyWorkflow.class, payload);
331-
```
332-
333-
and
334-
335-
```
336-
daprWorkflowClient.raiseEvent(instanceId, "MyEvenet", event);
337-
```
338-
339-
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.
340-
341295

342296
## Next steps
343297

344298
Learn more about the [Dapr Java SDK packages available to add to your Java applications](https://dapr.github.io/java-sdk/).
345299

300+
Check the How To guide for [Dapr Workflows using Spring Boot and Testcontainers for a local workflow development experience](sb-workflows-howto.md).
301+
346302
## Related links
347303
- [Java SDK examples](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
## Using Dapr Workflows with Spring Boot
2+
3+
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.
4+
5+
With Dapr Workflows you define complex orchestrations (workflows) in (Java) code. The Dapr Spring Boot Starter makes your life easier by managing `Workflow`s and `WorkflowActivity`s as Spring beans.
6+
7+
In order to enable the automatic bean discovery you can annotate your `@SpringBootApplication` with the `@EnableDaprWorkflows` annotation:
8+
9+
```
10+
@SpringBootApplication
11+
@EnableDaprWorkflows
12+
public class MySpringBootApplication {
13+
...
14+
}
15+
```
16+
17+
By adding this annotation, all the `Workflow`s and `WorkflowActivity`s beans will be automatically discovered by Spring and registered to the workflow engine.
18+
19+
## Creating Workflows and Activities
20+
21+
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.
22+
23+
```
24+
@Component
25+
public class MyWorkflow implements Workflow {
26+
27+
@Override
28+
public WorkflowStub create() {
29+
return ctx -> {
30+
<WORKFLOW LOGIC>
31+
};
32+
}
33+
34+
}
35+
```
36+
37+
From inside your workflow definitions, you can perform service to service interactions, schedule timers or receive external events.
38+
39+
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`:
40+
41+
```
42+
@Component
43+
public class MyWorkflowActivity implements WorkflowActivity {
44+
45+
@Autowired
46+
private RestTemplate restTemplate;
47+
```
48+
49+
## Creating and interacting with Workflows
50+
51+
52+
To create and interact with Workflow instances you need to use the `DaprWorkflowClient` that you can also `@Autowired`.
53+
54+
```
55+
@Autowired
56+
private DaprWorkflowClient daprWorkflowClient;
57+
```
58+
59+
Applications can now schedule new workflow instances and raise events.
60+
61+
```
62+
String instanceId = daprWorkflowClient.scheduleNewWorkflow(MyWorkflow.class, payload);
63+
```
64+
65+
and
66+
67+
```
68+
daprWorkflowClient.raiseEvent(instanceId, "MyEvenet", event);
69+
```
70+
71+
[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)
72+
73+
74+
75+
## Next Steps & Resources
76+
77+
Check the blog post from [Baeldung covering Dapr Workflows and Dapr Pubsub](https://www.baeldung.com/dapr-workflows-pubsub) with a full working example.
78+
79+
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.

0 commit comments

Comments
 (0)