From 4d60aa13947f16b980a516e31ed05c03d5eca40f Mon Sep 17 00:00:00 2001 From: Artur Souza Date: Wed, 11 Dec 2019 19:03:39 -0800 Subject: [PATCH] Adding MethodContext + StateSerializer. --- .../io/dapr/actors/DaprClientBuilder.java | 18 +---- .../io/dapr/actors/DaprHttpAsyncClient.java | 6 +- .../io/dapr/actors/runtime/ActorCallType.java | 27 +++++++ .../actors/runtime/ActorMethodContext.java | 78 +++++++++++++++++++ .../runtime/ActorStateProviderSerializer.java | 44 +++++++++++ .../actors/runtime/ActorTypeInformation.java | 2 +- 6 files changed, 154 insertions(+), 21 deletions(-) create mode 100644 sdk/src/main/java/io/dapr/actors/runtime/ActorCallType.java create mode 100644 sdk/src/main/java/io/dapr/actors/runtime/ActorMethodContext.java create mode 100644 sdk/src/main/java/io/dapr/actors/runtime/ActorStateProviderSerializer.java diff --git a/sdk/src/main/java/io/dapr/actors/DaprClientBuilder.java b/sdk/src/main/java/io/dapr/actors/DaprClientBuilder.java index 58bef446a..4ac91f30a 100644 --- a/sdk/src/main/java/io/dapr/actors/DaprClientBuilder.java +++ b/sdk/src/main/java/io/dapr/actors/DaprClientBuilder.java @@ -12,11 +12,6 @@ import okhttp3.OkHttpClient; */ class DaprClientBuilder { - /** - * Default hostname for Dapr. - */ - private String hostname = Constants.DEFAULT_HOSTNAME; - /** * Default port for Dapr after checking environment variable. */ @@ -29,18 +24,7 @@ class DaprClientBuilder { public DaprAsyncClient buildAsyncClient() { OkHttpClient.Builder builder = new OkHttpClient.Builder(); // TODO: Expose configurations for OkHttpClient or com.microsoft.rest.RestClient. - String baseUrl = String.format("http://%s:%d/", this.hostname, this.port); - return new DaprHttpAsyncClient(baseUrl, builder.build()); - } - - /** - * Overrides the hostname. - * @param hostname new hostname. - * @return This instance. - */ - public DaprClientBuilder withHostname(String hostname) { - this.hostname = hostname; - return this; + return new DaprHttpAsyncClient(this.port, builder.build()); } /** diff --git a/sdk/src/main/java/io/dapr/actors/DaprHttpAsyncClient.java b/sdk/src/main/java/io/dapr/actors/DaprHttpAsyncClient.java index 838e13baa..b4e153e5d 100644 --- a/sdk/src/main/java/io/dapr/actors/DaprHttpAsyncClient.java +++ b/sdk/src/main/java/io/dapr/actors/DaprHttpAsyncClient.java @@ -44,12 +44,12 @@ class DaprHttpAsyncClient implements DaprAsyncClient { /** * Creates a new instance of {@link DaprHttpAsyncClient}. - * @param baseUrl Base Url for calling Dapr. (e.g. http://localhost:3500/) + * @param port Port for calling Dapr. (e.g. 3500) * @param httpClient RestClient used for all API calls in this new instance. */ - DaprHttpAsyncClient(String baseUrl, OkHttpClient httpClient) + DaprHttpAsyncClient(int port, OkHttpClient httpClient) { - this.baseUrl = baseUrl; + this.baseUrl = String.format("http://%s:%d/", Constants.DEFAULT_HOSTNAME, port);; this.httpClient = httpClient; } diff --git a/sdk/src/main/java/io/dapr/actors/runtime/ActorCallType.java b/sdk/src/main/java/io/dapr/actors/runtime/ActorCallType.java new file mode 100644 index 000000000..b382e1134 --- /dev/null +++ b/sdk/src/main/java/io/dapr/actors/runtime/ActorCallType.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + */ + +package io.dapr.actors.runtime; + +/** + * Represents the call-type associated with the method invoked by actor runtime. + */ +enum ActorCallType { + + /** + * Specifies that the method invoked is an actor interface method for a given client request. + */ + ACTOR_INTERFACE_METHOD, + + /** + * Specifies that the method invoked is a timer callback method. + */ + TIMER_METHOD, + + /** + * Specifies that the method is when a reminder fires. + */ + REMINDER_METHOD +} diff --git a/sdk/src/main/java/io/dapr/actors/runtime/ActorMethodContext.java b/sdk/src/main/java/io/dapr/actors/runtime/ActorMethodContext.java new file mode 100644 index 000000000..3fcff78b4 --- /dev/null +++ b/sdk/src/main/java/io/dapr/actors/runtime/ActorMethodContext.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + */ + +package io.dapr.actors.runtime; + +/** + * Contains information about the method that is invoked by actor runtime. + */ +class ActorMethodContext { + + /** + * Method name to be invoked. + */ + private final String methodName; + + /** + * Call type to be used. + */ + private final ActorCallType callType; + + /** + * Constructs a new instance of {@link ActorMethodContext} + * @param methodName Method name to be invoked. + * @param callType Call type to be used. + */ + private ActorMethodContext(String methodName, ActorCallType callType) { + this.methodName = methodName; + this.callType = callType; + } + + /** + * Gets the name of the method invoked by actor runtime. + * @return The method name. + */ + public String getMethodName() { + return this.methodName; + } + + /** + * Gets the call type to be used. + * @return Call type. + */ + public ActorCallType getCallType() { + return this.callType; + } + + /** + * Creates a context to invoke an Actor's method. + * @param methodName THe method to be invoked. + * @return Context of the method call as {@link ActorMethodContext} + */ + static ActorMethodContext CreateForActor(String methodName) + { + return new ActorMethodContext(methodName, ActorCallType.ACTOR_INTERFACE_METHOD); + } + + /** + * Creates a context to invoke an Actor's timer. + * @param methodName THe method to be invoked. + * @return Context of the method call as {@link ActorMethodContext} + */ + static ActorMethodContext CreateForTimer(String methodName) + { + return new ActorMethodContext(methodName, ActorCallType.TIMER_METHOD); + } + + /** + * Creates a context to invoke an Actor's reminder. + * @param methodName THe method to be invoked. + * @return Context of the method call as {@link ActorMethodContext} + */ + static ActorMethodContext CreateForReminder(String methodName) + { + return new ActorMethodContext(methodName, ActorCallType.REMINDER_METHOD); + } +} diff --git a/sdk/src/main/java/io/dapr/actors/runtime/ActorStateProviderSerializer.java b/sdk/src/main/java/io/dapr/actors/runtime/ActorStateProviderSerializer.java new file mode 100644 index 000000000..a55945850 --- /dev/null +++ b/sdk/src/main/java/io/dapr/actors/runtime/ActorStateProviderSerializer.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + */ + +package io.dapr.actors.runtime; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.IOException; + +/** + * Serializes and deserializes an object. + */ +class ActorStateProviderSerializer { + + /** + * Shared Json serializer/deserializer as per Jackson's documentation. + */ + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + /** + * Serializes a given state object into byte array. + * @param state State object to be serialized. + * @return Array of bytes[] with the serialized content. + * @throws IOException + */ + byte[] serialize(Object state) throws IOException { + return OBJECT_MAPPER.writeValueAsBytes(state); + } + + /** + * Deserializes the byte array into the original object. + * @param buffer Array of bytes to be parsed. + * @param clazz Type of the object being deserialized. + * @param Generic type of the object being deserialized. + * @return Object of type T. + * @throws IOException + */ + T deserialize(byte[] buffer, Class clazz) throws IOException { + return OBJECT_MAPPER.readValue(buffer, clazz); + } + +} diff --git a/sdk/src/main/java/io/dapr/actors/runtime/ActorTypeInformation.java b/sdk/src/main/java/io/dapr/actors/runtime/ActorTypeInformation.java index 1e8d738c7..5c21557dc 100644 --- a/sdk/src/main/java/io/dapr/actors/runtime/ActorTypeInformation.java +++ b/sdk/src/main/java/io/dapr/actors/runtime/ActorTypeInformation.java @@ -12,7 +12,7 @@ import java.util.Collections; /** * Contains the information about the class implementing an actor. */ -public final class ActorTypeInformation { +final class ActorTypeInformation { /** * Actor type's name.