Java

    1. dependencies {
    2. ...
    3. // Dapr's core SDK with all features, except Actors.
    4. compile('io.dapr:dapr-sdk:1.1.0'))
    5. // Dapr's SDK for Actors (optional).
    6. compile('io.dapr:dapr-sdk-actors:1.1.0')
    7. // Dapr's SDK integration with SpringBoot (optional).
    8. compile('io.dapr:dapr-sdk-springboot:1.1.0')
    9. }
    1. import io.dapr.client.DaprClient;
    2. import io.dapr.client.DaprClientBuilder;
    3. try (DaprClient client = (new DaprClientBuilder()).build()) {
    4. // invoke a 'GET' method (HTTP) skipping serialization: \say with a Mono<byte[]> return type
    5. // for gRPC set HttpExtension.NONE parameters below
    6. response = client.invokeMethod(SERVICE_TO_INVOKE, METHOD_TO_INVOKE, "{\"name\":\"World!\"}", HttpExtension.GET, byte[].class).block();
    7. // invoke a 'POST' method (HTTP) skipping serialization: to \say with a Mono<byte[]> return type
    8. System.out.println(new String(response));
    9. // invoke a 'POST' method (HTTP) with serialization: \employees with a Mono<Employee> return type
    10. Employee newEmployee = new Employee("Nigel", "Guitarist");
    11. Employee employeeResponse = client.invokeMethod(SERVICE_TO_INVOKE, "employees", newEmployee, HttpExtension.POST, Employee.class).block();
    12. }
    • For a full guide on service invocation visit .
    • Visit Java SDK examples for code samples and instructions to try out service invocation

    Save & get application state

    • For a full list of state operations visit How-To: Get & save state.
    • Visit for code samples and instructions to try out state management
    Publish messages
    1. import io.dapr.client.DaprClient;
    2. import io.dapr.client.domain.Metadata;
    3. import static java.util.Collections.singletonMap;
    4. try (DaprClient client = (new DaprClientBuilder()).build()) {
    5. client.publishEvent(PUBSUB_NAME, TOPIC_NAME, message, singletonMap(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS)).block();
    6. }
    Subscribe to messages
    1. import com.fasterxml.jackson.databind.ObjectMapper;
    2. import io.dapr.Topic;
    3. import io.dapr.client.domain.CloudEvent;
    4. import org.springframework.web.bind.annotation.PostMapping;
    5. import org.springframework.web.bind.annotation.RequestBody;
    6. import org.springframework.web.bind.annotation.RestController;
    7. import reactor.core.publisher.Mono;
    8. @RestController
    9. public class SubscriberController {
    10. private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    11. @Topic(name = "testingtopic", pubsubName = "${myAppProperty:messagebus}")
    12. @PostMapping(path = "/testingtopic")
    13. public Mono<Void> handleMessage(@RequestBody(required = false) CloudEvent cloudEvent) {
    14. return Mono.fromRunnable(() -> {
    15. try {
    16. System.out.println("Subscriber got: " + cloudEvent.getData());
    17. System.out.println("Subscriber got: " + OBJECT_MAPPER.writeValueAsString(cloudEvent));
    18. } catch (Exception e) {
    19. });
    20. }
    21. }

    Interact with output bindings

    • For a full guide on output bindings visit .
    • Visit Java SDK examples for code samples and instructions to try out output bindings
    1. import com.fasterxml.jackson.databind.ObjectMapper;
    2. import io.dapr.client.DaprClient;
    3. import io.dapr.client.DaprClientBuilder;
    4. import java.util.Map;
    5. try (DaprClient client = (new DaprClientBuilder()).build()) {
    6. Map<String, String> secret = client.getSecret(SECRET_STORE_NAME, secretKey).block();
    7. System.out.println(JSON_SERIALIZER.writeValueAsString(secret));
    8. }
    • For a full guide on secrets visit .
    • Visit Java SDK examples for code samples and instructions to try out retrieving secrets

    Actors

    1. import io.dapr.actors.ActorMethod;
    2. import io.dapr.actors.ActorType;
    3. import reactor.core.publisher.Mono;
    4. @ActorType(name = "DemoActor")
    5. public interface DemoActor {
    6. void registerReminder();
    7. @ActorMethod(name = "echo_message")
    8. String say(String something);
    9. void clock(String message);
    10. @ActorMethod(returns = Integer.class)
    11. }