Skip to content

Commit

Permalink
Merge branch 'master' into workflowLogging
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Souza <artursouza.ms@outlook.com>
  • Loading branch information
artursouza committed Jan 8, 2024
2 parents 9a66cf2 + 4ed41ad commit 01e666d
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 194 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/automerge-bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install dependencies
run: pip install PyGithub
- name: Automerge and update
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,28 @@ jobs:
DAPR_REF:
TOXIPROXY_URL: https://github.com/Shopify/toxiproxy/releases/download/v2.5.0/toxiproxy-server-linux-amd64
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up OpenJDK ${{ env.JDK_VER }}
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
distribution: 'adopt'
java-version: ${{ env.JDK_VER }}
- name: Set up Dapr CLI
run: wget -q ${{ env.DAPR_INSTALL_URL }} -O - | /bin/bash -s ${{ env.DAPR_CLI_VER }}
- name: Set up Go ${{ env.GOVER }}
if: env.DAPR_REF != '' || env.DAPR_CLI_REF != ''
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ env.GOVER }}
- name: Checkout Dapr CLI repo to override dapr command.
uses: actions/checkout@v3
uses: actions/checkout@v4
if: env.DAPR_CLI_REF != ''
with:
repository: dapr/cli
ref: ${{ env.DAPR_CLI_REF }}
path: cli
- name: Checkout Dapr repo to override daprd.
uses: actions/checkout@v3
uses: actions/checkout@v4
if: env.DAPR_REF != ''
with:
repository: dapr/dapr
Expand Down Expand Up @@ -158,9 +158,9 @@ jobs:
GPG_KEY: ${{ secrets.GPG_KEY }}
GPG_PWD: ${{ secrets.GPG_PWD }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up OpenJDK ${{ env.JDK_VER }}
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
distribution: 'adopt'
java-version: ${{ env.JDK_VER }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dapr_bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Comment analyzer
uses: actions/github-script@v6
uses: actions/github-script@v7
with:
github-token: ${{secrets.DAPR_BOT_TOKEN}}
script: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fossa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
FOSSA_API_KEY: b88e1f4287c3108c8751bf106fb46db6 # This is a push-only token that is safe to be exposed.
steps:
- name: "Checkout code"
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: "Run FOSSA Scan"
uses: fossas/fossa-action@v1.3.1 # Use a specific version if locking is preferred
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,28 @@ jobs:
DAPR_CLI_REF:
DAPR_REF:
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up OpenJDK ${{ env.JDK_VER }}
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
distribution: 'adopt'
java-version: ${{ env.JDK_VER }}
- name: Set up Dapr CLI
run: wget -q ${{ env.DAPR_INSTALL_URL }} -O - | /bin/bash -s ${{ env.DAPR_CLI_VER }}
- name: Set up Go ${{ env.GOVER }}
if: env.DAPR_REF != '' || env.DAPR_CLI_REF != ''
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ env.GOVER }}
- name: Checkout Dapr CLI repo to override dapr command.
uses: actions/checkout@v3
uses: actions/checkout@v4
if: env.DAPR_CLI_REF != ''
with:
repository: dapr/cli
ref: ${{ env.DAPR_CLI_REF }}
path: cli
- name: Checkout Dapr repo to override daprd.
uses: actions/checkout@v3
uses: actions/checkout@v4
if: env.DAPR_REF != ''
with:
repository: dapr/dapr
Expand Down
190 changes: 20 additions & 170 deletions daprdocs/content/en/java-sdk-docs/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ cascade:
github_branch: master
---

Dapr offers a variety of packages to help with the development of Java applications. Using them you can create Java clients, servers, and virtual actors with Dapr.

## Prerequisites

- [Dapr CLI]({{< ref install-dapr-cli.md >}}) installed
Expand Down Expand Up @@ -123,173 +125,21 @@ try (DaprClient client = (new DaprClientBuilder()).build()) {
- For a full guide on output bindings visit [How-To: Output bindings]({{< ref howto-bindings.md >}}).
- Visit [Java SDK examples](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/bindings/http) for code samples and instructions to try out output bindings.

### Interact with input bindings

```java
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
@RequestMapping("/")
public class myClass {
private static final Logger log = LoggerFactory.getLogger(myClass);
@PostMapping(path = "/checkout")
public Mono<String> getCheckout(@RequestBody(required = false) byte[] body) {
return Mono.fromRunnable(() ->
log.info("Received Message: " + new String(body)));
}
}
```

- For a full guide on input bindings, visit [How-To: Input bindings]({{< ref howto-triggers >}}).
- Visit [Java SDK examples](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/bindings/http) for code samples and instructions to try out input bindings.

### Retrieve secrets

```java
import com.fasterxml.jackson.databind.ObjectMapper;
import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import java.util.Map;

try (DaprClient client = (new DaprClientBuilder()).build()) {
Map<String, String> secret = client.getSecret(SECRET_STORE_NAME, secretKey).block();
System.out.println(JSON_SERIALIZER.writeValueAsString(secret));
}
```

- For a full guide on secrets visit [How-To: Retrieve secrets]({{< ref howto-secrets.md >}}).
- Visit [Java SDK examples](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/secrets) for code samples and instructions to try out retrieving secrets

### Actors
An actor is an isolated, independent unit of compute and state with single-threaded execution. Dapr provides an actor implementation based on the [Virtual Actor pattern](https://www.microsoft.com/en-us/research/project/orleans-virtual-actors/), which provides a single-threaded programming model and where actors are garbage collected when not in use. With Dapr's implementaiton, you write your Dapr actors according to the Actor model, and Dapr leverages the scalability and reliability that the underlying platform provides.

```java
import io.dapr.actors.ActorMethod;
import io.dapr.actors.ActorType;
import reactor.core.publisher.Mono;

@ActorType(name = "DemoActor")
public interface DemoActor {

void registerReminder();

@ActorMethod(name = "echo_message")
String say(String something);

void clock(String message);

@ActorMethod(returns = Integer.class)
Mono<Integer> incrementAndGet(int delta);
}
```

- For a full guide on actors visit [How-To: Use virtual actors in Dapr]({{< ref howto-actors.md >}}).
- Visit [Java SDK examples](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/actors) for code samples and instructions to try actors

### Get & Subscribe to application configurations

> Note this is a preview API and thus will only be accessible via the DaprPreviewClient interface and not the normal DaprClient interface
```java
import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.client.domain.ConfigurationItem;
import io.dapr.client.domain.GetConfigurationRequest;
import io.dapr.client.domain.SubscribeConfigurationRequest;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

try (DaprClient client = (new DaprClientBuilder()).build()) {
// Get configuration for a single key
Mono<ConfigurationItem> item = client.getConfiguration(CONFIG_STORE_NAME, CONFIG_KEY).block();

// Get configurations for multiple keys
Mono<Map<String, ConfigurationItem>> items =
client.getConfiguration(CONFIG_STORE_NAME, CONFIG_KEY_1, CONFIG_KEY_2);

// Subscribe to configuration changes
Flux<SubscribeConfigurationResponse> outFlux = client.subscribeConfiguration(CONFIG_STORE_NAME, CONFIG_KEY_1, CONFIG_KEY_2);
outFlux.subscribe(configItems -> configItems.forEach(...));

// Unsubscribe from configuration changes
Mono<UnsubscribeConfigurationResponse> unsubscribe = client.unsubscribeConfiguration(SUBSCRIPTION_ID, CONFIG_STORE_NAME)
}
```

- For a full list of configuration operations visit [How-To: Manage configuration from a store]({{< ref howto-manage-configuration.md >}}).
- Visit [Java SDK examples](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/configuration) for code samples and instructions to try out different configuration operations.

### Query saved state

> Note this is a preview API and thus will only be accessible via the DaprPreviewClient interface and not the normal DaprClient interface
```java
import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.client.DaprPreviewClient;
import io.dapr.client.domain.QueryStateItem;
import io.dapr.client.domain.QueryStateRequest;
import io.dapr.client.domain.QueryStateResponse;
import io.dapr.client.domain.query.Query;
import io.dapr.client.domain.query.Sorting;
import io.dapr.client.domain.query.filters.EqFilter;

try (DaprClient client = builder.build(); DaprPreviewClient previewClient = builder.buildPreviewClient()) {
String searchVal = args.length == 0 ? "searchValue" : args[0];

// Create JSON data
Listing first = new Listing();
first.setPropertyType("apartment");
first.setId("1000");
...
Listing second = new Listing();
second.setPropertyType("row-house");
second.setId("1002");
...
Listing third = new Listing();
third.setPropertyType("apartment");
third.setId("1003");
...
Listing fourth = new Listing();
fourth.setPropertyType("apartment");
fourth.setId("1001");
...
Map<String, String> meta = new HashMap<>();
meta.put("contentType", "application/json");

// Save state
SaveStateRequest request = new SaveStateRequest(STATE_STORE_NAME).setStates(
new State<>("1", first, null, meta, null),
new State<>("2", second, null, meta, null),
new State<>("3", third, null, meta, null),
new State<>("4", fourth, null, meta, null)
);
client.saveBulkState(request).block();


// Create query and query state request

Query query = new Query()
.setFilter(new EqFilter<>("propertyType", "apartment"))
.setSort(Arrays.asList(new Sorting("id", Sorting.Order.DESC)));
QueryStateRequest request = new QueryStateRequest(STATE_STORE_NAME)
.setQuery(query);

// Use preview client to call query state API
QueryStateResponse<MyData> result = previewClient.queryState(request, MyData.class).block();

// View Query state response
System.out.println("Found " + result.getResults().size() + " items.");
for (QueryStateItem<Listing> item : result.getResults()) {
System.out.println("Key: " + item.getKey());
System.out.println("Data: " + item.getValue());
}
}

```
- For a full list of configuration operations visit [How-To: Query state]({{< ref howto-state-query-api.md >}}).
- Visit [Java SDK examples](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/querystate) for complete code sample.

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

<div class="card-deck">
<div class="card">
<div class="card-body">
<h5 class="card-title"><b>Client</b></h5>
<p class="card-text">Create Java clients that interact with a Dapr sidecar and other Dapr applications.</p>
<a href="{{< ref java-client >}}" class="stretched-link"></a>
</div>
</div>
<div class="card">
<div class="card-body">
<h5 class="card-title"><b>Workflow</b></h5>
<p class="card-text">Create and manage workflows that work with other Dapr APIs in Java.</p>
<a href="{{< ref workflow >}}" class="stretched-link"></a>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
---
type: docs
title: "Dapr Java SDK"
linkTitle: "Java"
weight: 2000
description: Java SDK packages for developing Dapr applications
title: "Getting started with the Dapr client Java SDK"
linkTitle: "Client"
weight: 3000
description: How to get up and running with the Dapr Java SDK
---

The Dapr client package allows you to interact with other Dapr applications from a Java application.

{{% alert title="Note" color="primary" %}}
If you haven't already, [try out one of the quickstarts]({{< ref quickstarts >}}) for a quick walk-through on how to use the Dapr Java SDK with an API building block.

{{% /alert %}}

## Prerequisites

[Complete initial setup and import the Java SDK into your project]({{< ref java >}})
Expand Down Expand Up @@ -564,5 +571,35 @@ public class DemoWorkflowClient {
- [How-To: Manage workflows]({{< ref howto-manage-workflow.md >}}).
- [Learn more about how to use workflows with the Java SDK]({{< ref java-workflow.md >}}).

## Sidecar APIs

#### Wait for sidecar
The `DaprClient` also provides a helper method to wait for the sidecar to become healthy (components only). When using
this method, be sure to specify a timeout in milliseconds and block() to wait for the result of a reactive operation.

```java
// Wait for the Dapr sidecar to report healthy before attempting to use Dapr components.
try (DaprClient client = new DaprClientBuilder().build()) {
System.out.println("Waiting for Dapr sidecar ...");
client.waitForSidecar(10000).block(); // Specify the timeout in milliseconds
System.out.println("Dapr sidecar is ready.");
...
}

// Perform Dapr component operations here i.e. fetching secrets or saving state.
```

### Shutdown the sidecar
```java
try (DaprClient client = new DaprClientBuilder().build()) {
logger.info("Sending shutdown request.");
client.shutdown().block();
logger.info("Ensuring dapr has stopped.");
...
}
```

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

## Related links
- [Java SDK examples](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples)
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public void timerRecoveryTest() throws Exception {
runs.left.stop();

// Pause a bit to let placements settle.
logger.info("Pausing 10 seconds to let placements settle.");
Thread.sleep(Duration.ofSeconds(10).toMillis());
logger.info("Pausing 12 seconds to let placements settle.");
Thread.sleep(Duration.ofSeconds(12).toMillis());

runs.left.start();

Expand Down

0 comments on commit 01e666d

Please sign in to comment.