mirror of https://github.com/dapr/java-sdk.git
add wait for sidecar docs & update java-sdk docs to match .net more w/ client separation (#981)
Signed-off-by: Cassandra Coyle <cassie@diagrid.io> Co-authored-by: Artur Souza <artursouza.ms@outlook.com>
This commit is contained in:
parent
5c312bd001
commit
657fc37a43
|
@ -11,6 +11,8 @@ cascade:
|
||||||
github_branch: master
|
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
|
## Prerequisites
|
||||||
|
|
||||||
- [Dapr CLI]({{< ref install-dapr-cli.md >}}) installed
|
- [Dapr CLI]({{< ref install-dapr-cli.md >}}) installed
|
||||||
|
@ -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 >}}).
|
- 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.
|
- 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
|
## Available packages
|
||||||
|
|
||||||
```java
|
<div class="card-deck">
|
||||||
import org.springframework.web.bind.annotation.*;
|
<div class="card">
|
||||||
import org.slf4j.Logger;
|
<div class="card-body">
|
||||||
import org.slf4j.LoggerFactory;
|
<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>
|
||||||
@RestController
|
<a href="{{< ref java-client >}}" class="stretched-link"></a>
|
||||||
@RequestMapping("/")
|
</div>
|
||||||
public class myClass {
|
</div>
|
||||||
private static final Logger log = LoggerFactory.getLogger(myClass);
|
<div class="card">
|
||||||
@PostMapping(path = "/checkout")
|
<div class="card-body">
|
||||||
public Mono<String> getCheckout(@RequestBody(required = false) byte[] body) {
|
<h5 class="card-title"><b>Workflow</b></h5>
|
||||||
return Mono.fromRunnable(() ->
|
<p class="card-text">Create and manage workflows that work with other Dapr APIs in Java.</p>
|
||||||
log.info("Received Message: " + new String(body)));
|
<a href="{{< ref workflow >}}" class="stretched-link"></a>
|
||||||
}
|
</div>
|
||||||
}
|
</div>
|
||||||
```
|
</div>
|
||||||
|
|
||||||
- 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/).
|
|
||||||
|
|
|
@ -1,11 +1,18 @@
|
||||||
---
|
---
|
||||||
type: docs
|
type: docs
|
||||||
title: "Dapr Java SDK"
|
title: "Getting started with the Dapr client Java SDK"
|
||||||
linkTitle: "Java"
|
linkTitle: "Client"
|
||||||
weight: 2000
|
weight: 3000
|
||||||
description: Java SDK packages for developing Dapr applications
|
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
|
## Prerequisites
|
||||||
|
|
||||||
[Complete initial setup and import the Java SDK into your project]({{< ref java >}})
|
[Complete initial setup and import the Java SDK into your project]({{< ref java >}})
|
||||||
|
@ -564,5 +571,35 @@ public class DemoWorkflowClient {
|
||||||
- [How-To: Manage workflows]({{< ref howto-manage-workflow.md >}}).
|
- [How-To: Manage workflows]({{< ref howto-manage-workflow.md >}}).
|
||||||
- [Learn more about how to use workflows with the Java SDK]({{< ref java-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
|
## Related links
|
||||||
- [Java SDK examples](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples)
|
- [Java SDK examples](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples)
|
Loading…
Reference in New Issue