Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 67 additions & 2 deletions examples/src/main/java/io/dapr/examples/workflows/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Run `dapr init` to initialize Dapr in Self-Hosted Mode if it's not already initi

### Running the demo Workflow worker

The first Java class to consider is `DemoWorkflowWorker`. It's job is to register an implementation of `DemoWorkflow` in Dapr's workflow runtime engine. In the `DemoWorkflowWorker.java` file, you will find the `DemoWorkflowWorker` class and the `main` method. See the code snippet below:
The first Java class to consider is `DemoWorkflowWorker`. Its job is to register an implementation of `DemoWorkflow` in Dapr's workflow runtime engine. In the `DemoWorkflowWorker.java` file, you will find the `DemoWorkflowWorker` class and the `main` method. See the code snippet below:

```java
public class DemoWorkflowWorker {
Expand All @@ -65,14 +65,79 @@ This application uses `WorkflowRuntime.getInstance().registerWorkflow()` in orde

The `WorkflowRuntime.getInstance().start()` method will build and start the engine within the Dapr workflow runtime.

The Workflow class `DemoWorkflow` extends from `Workflow` and is responsible for implementing the logic for creating and running workflows.

```java
public abstract class Workflow {
public Workflow(){
}

/**
* Executes the workflow logic.
*
* @return A WorkflowStub.
*/
public abstract WorkflowStub create();

/**
* Executes the workflow logic.
*
* @param ctx provides access to methods for scheduling durable tasks and getting information about the current
* workflow instance.
*/
public void run(WorkflowContext ctx) {
this.create().run(ctx);
}
}

```
The running logic of the `DemoWorkflow` is to run its create method. And the `create()` method can devide into six parts:
- wait for `TimedOutEvent`
- wait for `TestEvent`
- wait for external event and all tasks to finish
- wait for external event and any task to finish
- call activity `DemoWorkflowActivity` which is registered in `DemoWorkflowWorker`
- wait for `restartEvent` (if not received it will call child workflow and return)

The Activity class `DemoWorkflowActivity` is an implementation of `WorkflowActivity` interface, which contains a `run` method. This activity modify the input message, sleeps 5 seconds and then passes the new message as output.

```java
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class DemoWorkflowActivity implements WorkflowActivity {

@Override
public DemoActivityOutput run(WorkflowActivityContext ctx) {
...

var message = ctx.getInput(DemoActivityInput.class).getMessage();
var newMessage = message + " World!, from Activity";
...

try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

...
var output = new DemoActivityOutput(message, newMessage);
...

return output;
}
}
```

The `DemoSubWorkflow` class is a child workflow that prints some metadata and its input message.

Now, execute the following script in order to run DemoWorkflowWorker:
```sh
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
```

### Running the Workflow client

The `DemoWorkflowClient` starts instances of workflows that have been registered with Dapr.
A DaprWorkflowClient can manage Dapr workflow instances. The `DemoWorkflowClient` starts instances of workflows that have been registered with Dapr, raises events the workflow are waiting and finally terminates the instances.

With the DemoWorkflowWorker running, use the follow command to start the workflow with the DemoWorkflowClient:

Expand Down