Skip to content

Commit

Permalink
Merge pull request #3776 from hhunter-ms/java_features
Browse files Browse the repository at this point in the history
[Workflow] Java features and concept addition
  • Loading branch information
hhunter-ms authored Oct 5, 2023
2 parents 88a109b + e93b654 commit 79e6312
Showing 1 changed file with 100 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ APIs that generate random numbers, random UUIDs, or the current date are _non-de

For example, instead of this:

{{< tabs ".NET" >}}
{{< tabs ".NET" Java >}}

{{% codetab %}}

Expand All @@ -175,11 +175,22 @@ string randomString = GetRandomString();

{{% /codetab %}}

{{% codetab %}}

```java
// DON'T DO THIS!
Instant currentTime = Instant.now();
UUID newIdentifier = UUID.randomUUID();
string randomString = GetRandomString();
```

{{% /codetab %}}

{{< /tabs >}}

Do this:

{{< tabs ".NET" >}}
{{< tabs ".NET" Java >}}

{{% codetab %}}

Expand All @@ -192,6 +203,17 @@ string randomString = await context.CallActivityAsync<string>("GetRandomString")

{{% /codetab %}}

{{% codetab %}}

```java
// Do this!!
Instant currentTime = context.getCurrentInstant();
Guid newIdentifier = context.NewGuid();
String randomString = context.callActivity(GetRandomString.class.getName(), String.class).await();
```

{{% /codetab %}}

{{< /tabs >}}


Expand All @@ -202,20 +224,58 @@ Instead, workflows should interact with external state _indirectly_ using workfl

For example, instead of this:

{{< tabs ".NET" Java >}}

{{% codetab %}}

```csharp
// DON'T DO THIS!
string configuration = Environment.GetEnvironmentVariable("MY_CONFIGURATION")!;
string data = await new HttpClient().GetStringAsync("https://example.com/api/data");
```
{{% /codetab %}}

{{% codetab %}}

```java
// DON'T DO THIS!
String configuration = System.getenv("MY_CONFIGURATION");

HttpRequest request = HttpRequest.newBuilder().uri(new URI("https://postman-echo.com/post")).GET().build();
HttpResponse<String> response = HttpClient.newBuilder().build().send(request, HttpResponse.BodyHandlers.ofString());
```

{{% /codetab %}}

{{< /tabs >}}

Do this:

{{< tabs ".NET" Java >}}

{{% codetab %}}

```csharp
// Do this!!
string configuation = workflowInput.Configuration; // imaginary workflow input argument
string data = await context.CallActivityAsync<string>("MakeHttpCall", "https://example.com/api/data");
```

{{% /codetab %}}

{{% codetab %}}

```java
// Do this!!
String configuation = ctx.getInput(InputType.class).getConfiguration(); // imaginary workflow input argument
String data = ctx.callActivity(MakeHttpCall.class, "https://example.com/api/data", String.class).await();
```

{{% /codetab %}}

{{< /tabs >}}


#### Workflow functions must execute only on the workflow dispatch thread.
The implementation of each language SDK requires that all workflow function operations operate on the same thread (goroutine, etc.) that the function was scheduled on. Workflow functions must never:
- Schedule background threads, or
Expand All @@ -225,20 +285,58 @@ Failure to follow this rule could result in undefined behavior. Any background p

For example, instead of this:

{{< tabs ".NET" Java >}}

{{% codetab %}}

```csharp
// DON'T DO THIS!
Task t = Task.Run(() => context.CallActivityAsync("DoSomething"));
await context.CreateTimer(5000).ConfigureAwait(false);
```
{{% /codetab %}}

{{% codetab %}}

```java
// DON'T DO THIS!
new Thread(() -> {
ctx.callActivity(DoSomethingActivity.class.getName()).await();
}).start();
ctx.createTimer(Duration.ofSeconds(5)).await();
```

{{% /codetab %}}

{{< /tabs >}}

Do this:

{{< tabs ".NET" Java >}}

{{% codetab %}}

```csharp
// Do this!!
Task t = context.CallActivityAsync("DoSomething");
await context.CreateTimer(5000).ConfigureAwait(true);
```

{{% /codetab %}}

{{% codetab %}}

```java
// Do this!!
ctx.callActivity(DoSomethingActivity.class.getName()).await();
ctx.createTimer(Duration.ofSeconds(5)).await();
```

{{% /codetab %}}

{{< /tabs >}}


### Updating workflow code

Make sure updates you make to the workflow code maintain its determinism. A couple examples of code updates that can break workflow determinism:
Expand Down

0 comments on commit 79e6312

Please sign in to comment.