Merge pull request #43 from artursouza/actors

Adding MethodContext + StateSerializer.
This commit is contained in:
Leon Mai 2019-12-11 19:10:41 -08:00 committed by GitHub
commit 821c21b3fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 154 additions and 21 deletions

View File

@ -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());
}
/**

View File

@ -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;
}

View File

@ -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
}

View File

@ -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);
}
}

View File

@ -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 <T> Generic type of the object being deserialized.
* @return Object of type T.
* @throws IOException
*/
<T> T deserialize(byte[] buffer, Class<T> clazz) throws IOException {
return OBJECT_MAPPER.readValue(buffer, clazz);
}
}

View File

@ -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.