mirror of https://github.com/dapr/java-sdk.git
Make serializers optional + javadocs about default serializers. (#168)
This commit is contained in:
parent
0bdf939f91
commit
45fe471c48
|
@ -35,7 +35,7 @@ public class DemoActorClient {
|
|||
private static final ExecutorService POOL = Executors.newFixedThreadPool(NUM_ACTORS);
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ActorProxyBuilder builder = new ActorProxyBuilder("DemoActor", new DefaultObjectSerializer());
|
||||
ActorProxyBuilder builder = new ActorProxyBuilder("DemoActor");
|
||||
|
||||
List<CompletableFuture<Void>> futures = new ArrayList<>(NUM_ACTORS);
|
||||
|
||||
|
|
|
@ -25,7 +25,7 @@ public class OutputBindingExample {
|
|||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
DaprClient client = new DaprClientBuilder(new DefaultObjectSerializer(), new DefaultObjectSerializer()).build();
|
||||
DaprClient client = new DaprClientBuilder().build();
|
||||
|
||||
final String BINDING_NAME = "bindingSample";
|
||||
|
||||
|
|
|
@ -101,7 +101,7 @@ In the `OutputBindingExample.java` file, you will find the `OutputBindingExample
|
|||
public class OutputBindingExample {
|
||||
///...
|
||||
public static void main(String[] args) throws Exception {
|
||||
DaprClient client = new DaprClientBuilder(new DefaultObjectSerializer(), new DefaultObjectSerializer()).build();
|
||||
DaprClient client = new DaprClientBuilder().build();
|
||||
final String BINDING_NAME = "bindingSample";
|
||||
///...
|
||||
MyClass myClass = new MyClass();
|
||||
|
|
|
@ -28,7 +28,7 @@ public class InvokeClient {
|
|||
* @param args Messages to be sent as request for the invoke API.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
DaprClient client = (new DaprClientBuilder(new DefaultObjectSerializer(), new DefaultObjectSerializer())).build();
|
||||
DaprClient client = (new DaprClientBuilder()).build();
|
||||
for (String message : args) {
|
||||
client.invokeService(Verb.POST, SERVICE_APP_ID, "say", message, null, String.class).block();
|
||||
}
|
||||
|
|
|
@ -106,7 +106,7 @@ public class InvokeClient {
|
|||
private static final String SERVICE_APP_ID = "invokedemo";
|
||||
///...
|
||||
public static void main(String[] args) {
|
||||
DaprClient client = (new DaprClientBuilder(new DefaultObjectSerializer(), new DefaultObjectSerializer())).build();
|
||||
DaprClient client = (new DaprClientBuilder()).build();
|
||||
for (String message : args) {
|
||||
client.invokeService(Verb.POST, SERVICE_APP_ID, "say", message, null, String.class).block();
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ public class Publisher {
|
|||
|
||||
public static void main(String[] args) throws Exception {
|
||||
//Creating the DaprClient: Using the default builder client produces an HTTP Dapr Client
|
||||
DaprClient client = new DaprClientBuilder(new DefaultObjectSerializer(), new DefaultObjectSerializer()).build();
|
||||
DaprClient client = new DaprClientBuilder().build();
|
||||
for (int i = 0; i < NUM_MESSAGES; i++) {
|
||||
String message = String.format("This is message #%d", i);
|
||||
//Publishing messages
|
||||
|
|
|
@ -88,7 +88,7 @@ public class Publisher {
|
|||
private static final String TOPIC_NAME = "testingtopic";
|
||||
///...
|
||||
public static void main(String[] args) throws Exception {
|
||||
DaprClient client = new DaprClientBuilder(new DefaultObjectSerializer(), new DefaultObjectSerializer()).build();
|
||||
DaprClient client = new DaprClientBuilder().build();
|
||||
for (int i = 0; i < NUM_MESSAGES; i++) {
|
||||
String message = String.format("This is message #%d", i);
|
||||
client.publishEvent(TOPIC_NAME, message).block();
|
||||
|
|
|
@ -39,7 +39,7 @@ public class OrderManager {
|
|||
HttpServer httpServer = HttpServer.create(new InetSocketAddress(httpPort), 0);
|
||||
|
||||
DaprClient daprClient =
|
||||
(new DaprClientBuilder(new DefaultObjectSerializer(), new DefaultObjectSerializer())).build();
|
||||
(new DaprClientBuilder()).build();
|
||||
|
||||
httpServer.createContext("/order").setHandler(e -> {
|
||||
out.println("Fetching order!");
|
||||
|
|
|
@ -30,7 +30,7 @@ This example implements a service listening on port 3000, while using Dapr's sta
|
|||
|
||||
```
|
||||
DaprClient daprClient =
|
||||
(new DaprClientBuilder(new DefaultObjectSerializer(), new DefaultObjectSerializer())).build();
|
||||
(new DaprClientBuilder()).build();
|
||||
|
||||
httpServer.createContext("/order").setHandler(e -> {
|
||||
out.println("Fetching order!");
|
||||
|
|
|
@ -3,6 +3,7 @@ package io.dapr.actors.client;
|
|||
import io.dapr.actors.ActorId;
|
||||
import io.dapr.client.DaprHttpBuilder;
|
||||
import io.dapr.serializer.DaprObjectSerializer;
|
||||
import io.dapr.serializer.DefaultObjectSerializer;
|
||||
|
||||
/**
|
||||
* Builder to generate an ActorProxy instance. Builder can be reused for multiple instances.
|
||||
|
@ -22,24 +23,37 @@ public class ActorProxyBuilder {
|
|||
/**
|
||||
* Dapr's object serializer.
|
||||
*/
|
||||
private final DaprObjectSerializer objectSerializer;
|
||||
private DaprObjectSerializer objectSerializer;
|
||||
|
||||
/**
|
||||
* Instantiates a new builder for a given Actor type.
|
||||
* Instantiates a new builder for a given Actor type, using {@link DefaultObjectSerializer} by default.
|
||||
*
|
||||
* {@link DefaultObjectSerializer} is not recommended for production scenarios.
|
||||
*
|
||||
* @param actorType Actor's type.
|
||||
* @param objectSerializer Serializer for objects sent/received.
|
||||
*/
|
||||
public ActorProxyBuilder(String actorType, DaprObjectSerializer objectSerializer) {
|
||||
public ActorProxyBuilder(String actorType) {
|
||||
if ((actorType == null) || actorType.isEmpty()) {
|
||||
throw new IllegalArgumentException("ActorType is required.");
|
||||
}
|
||||
|
||||
this.actorType = actorType;
|
||||
this.objectSerializer = new DefaultObjectSerializer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new builder for a given Actor type, using {@link DefaultObjectSerializer}.
|
||||
*
|
||||
* @param objectSerializer Serializer for objects sent/received.
|
||||
* @return This instance.
|
||||
*/
|
||||
public ActorProxyBuilder withObjectSerializer(DaprObjectSerializer objectSerializer) {
|
||||
if (objectSerializer == null) {
|
||||
throw new IllegalArgumentException("Serializer is required.");
|
||||
}
|
||||
|
||||
this.actorType = actorType;
|
||||
this.objectSerializer = objectSerializer;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -9,6 +9,7 @@ import io.dapr.actors.ActorId;
|
|||
import io.dapr.actors.ActorTrace;
|
||||
import io.dapr.client.DaprHttpBuilder;
|
||||
import io.dapr.serializer.DaprObjectSerializer;
|
||||
import io.dapr.serializer.DefaultObjectSerializer;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
|
@ -108,6 +109,31 @@ public class ActorRuntime {
|
|||
return this.INTERNAL_SERIALIZER.serialize(this.config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers an actor with the runtime, using {@link DefaultObjectSerializer} and {@link DefaultActorFactory}.
|
||||
*
|
||||
* {@link DefaultObjectSerializer} is not recommended for production scenarios.
|
||||
*
|
||||
* @param clazz The type of actor.
|
||||
* @param <T> Actor class type.
|
||||
*/
|
||||
public <T extends AbstractActor> void registerActor(Class<T> clazz) {
|
||||
registerActor(clazz, new DefaultObjectSerializer(), new DefaultObjectSerializer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers an actor with the runtime, using {@link DefaultObjectSerializer}.
|
||||
*
|
||||
* {@link DefaultObjectSerializer} is not recommended for production scenarios.
|
||||
*
|
||||
* @param clazz The type of actor.
|
||||
* @param actorFactory An optional factory to create actors. This can be used for dependency injection.
|
||||
* @param <T> Actor class type.
|
||||
*/
|
||||
public <T extends AbstractActor> void registerActor(Class<T> clazz, ActorFactory<T> actorFactory) {
|
||||
registerActor(clazz, actorFactory, new DefaultObjectSerializer(), new DefaultObjectSerializer());
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers an actor with the runtime.
|
||||
*
|
||||
|
@ -118,7 +144,7 @@ public class ActorRuntime {
|
|||
*/
|
||||
public <T extends AbstractActor> void registerActor(
|
||||
Class<T> clazz, DaprObjectSerializer objectSerializer, DaprObjectSerializer stateSerializer) {
|
||||
registerActor(clazz, null, objectSerializer, stateSerializer);
|
||||
registerActor(clazz, new DefaultActorFactory<T>(), objectSerializer, stateSerializer);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -137,21 +163,22 @@ public class ActorRuntime {
|
|||
if (clazz == null) {
|
||||
throw new IllegalArgumentException("Class is required.");
|
||||
}
|
||||
if (actorFactory == null) {
|
||||
throw new IllegalArgumentException("Actor factory is required.");
|
||||
}
|
||||
if (objectSerializer == null) {
|
||||
throw new IllegalArgumentException("Serializer is required.");
|
||||
throw new IllegalArgumentException("Object serializer is required.");
|
||||
}
|
||||
if (stateSerializer == null) {
|
||||
throw new IllegalArgumentException("State objectSerializer is required.");
|
||||
throw new IllegalArgumentException("State serializer is required.");
|
||||
}
|
||||
|
||||
ActorTypeInformation<T> actorTypeInfo = ActorTypeInformation.create(clazz);
|
||||
|
||||
ActorFactory<T> actualActorFactory = actorFactory != null ? actorFactory : new DefaultActorFactory<T>();
|
||||
|
||||
ActorRuntimeContext<T> context = new ActorRuntimeContext<>(
|
||||
this,
|
||||
objectSerializer,
|
||||
actualActorFactory,
|
||||
actorFactory,
|
||||
actorTypeInfo,
|
||||
this.daprClient,
|
||||
new DaprStateAsyncProvider(this.daprClient, stateSerializer));
|
||||
|
|
|
@ -9,35 +9,36 @@ public class ActorProxyBuilderTest {
|
|||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void buildWithNullActorId() {
|
||||
new ActorProxyBuilder("test", new DefaultObjectSerializer())
|
||||
new ActorProxyBuilder("test")
|
||||
.build(null);
|
||||
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void buildWithEmptyActorType() {
|
||||
new ActorProxyBuilder("", new DefaultObjectSerializer())
|
||||
new ActorProxyBuilder("")
|
||||
.build(new ActorId("100"));
|
||||
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void buildWithNullActorType() {
|
||||
new ActorProxyBuilder(null, new DefaultObjectSerializer())
|
||||
new ActorProxyBuilder(null)
|
||||
.build(new ActorId("100"));
|
||||
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void buildWithNullSerializer() {
|
||||
new ActorProxyBuilder("MyActor", null)
|
||||
new ActorProxyBuilder("MyActor")
|
||||
.withObjectSerializer(null)
|
||||
.build(new ActorId("100"));
|
||||
|
||||
}
|
||||
|
||||
@Test()
|
||||
public void build() {
|
||||
ActorProxyBuilder builder = new ActorProxyBuilder("test", new DefaultObjectSerializer());
|
||||
ActorProxyBuilder builder = new ActorProxyBuilder("test");
|
||||
ActorProxy actorProxy = builder.build(new ActorId("100"));
|
||||
|
||||
Assert.assertNotNull(actorProxy);
|
||||
|
|
|
@ -67,21 +67,21 @@ public class ActorRuntimeTest {
|
|||
|
||||
@Test
|
||||
public void registerActor() throws Exception {
|
||||
this.runtime.registerActor(MyActorImpl.class, new DefaultObjectSerializer(), new DefaultObjectSerializer());
|
||||
this.runtime.registerActor(MyActorImpl.class);
|
||||
Assert.assertTrue(new String(this.runtime.serializeConfig()).contains(ACTOR_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void activateActor() throws Exception {
|
||||
String actorId = UUID.randomUUID().toString();
|
||||
this.runtime.registerActor(MyActorImpl.class, new DefaultObjectSerializer(), new DefaultObjectSerializer());
|
||||
this.runtime.registerActor(MyActorImpl.class);
|
||||
this.runtime.activate(ACTOR_NAME, actorId).block();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeActor() throws Exception {
|
||||
String actorId = UUID.randomUUID().toString();
|
||||
this.runtime.registerActor(MyActorImpl.class, new DefaultObjectSerializer(), new DefaultObjectSerializer());
|
||||
this.runtime.registerActor(MyActorImpl.class);
|
||||
this.runtime.activate(ACTOR_NAME, actorId).block();
|
||||
|
||||
byte[] response = this.runtime.invoke(ACTOR_NAME, actorId, "say", null).block();
|
||||
|
@ -92,7 +92,7 @@ public class ActorRuntimeTest {
|
|||
@Test
|
||||
public void activateThendeactivateActor() throws Exception {
|
||||
String actorId = UUID.randomUUID().toString();
|
||||
this.runtime.registerActor(MyActorImpl.class, new DefaultObjectSerializer(), new DefaultObjectSerializer());
|
||||
this.runtime.registerActor(MyActorImpl.class);
|
||||
this.runtime.activate(ACTOR_NAME, actorId).block();
|
||||
this.runtime.deactivate(ACTOR_NAME, actorId).block();
|
||||
}
|
||||
|
@ -100,14 +100,14 @@ public class ActorRuntimeTest {
|
|||
@Test
|
||||
public void deactivateActor() throws Exception {
|
||||
String actorId = UUID.randomUUID().toString();
|
||||
this.runtime.registerActor(MyActorImpl.class, new DefaultObjectSerializer(), new DefaultObjectSerializer());
|
||||
this.runtime.registerActor(MyActorImpl.class);
|
||||
this.runtime.deactivate(ACTOR_NAME, actorId).block();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lazyActivate() throws Exception {
|
||||
String actorId = UUID.randomUUID().toString();
|
||||
this.runtime.registerActor(MyActorImpl.class, new DefaultObjectSerializer(), new DefaultObjectSerializer());
|
||||
this.runtime.registerActor(MyActorImpl.class);
|
||||
this.runtime.activate(ACTOR_NAME, actorId).block();
|
||||
|
||||
this.runtime.invoke(ACTOR_NAME, actorId, "say", null)
|
||||
|
@ -120,7 +120,7 @@ public class ActorRuntimeTest {
|
|||
@Test
|
||||
public void lazyDeactivate() throws Exception {
|
||||
String actorId = UUID.randomUUID().toString();
|
||||
this.runtime.registerActor(MyActorImpl.class, new DefaultObjectSerializer(), new DefaultObjectSerializer());
|
||||
this.runtime.registerActor(MyActorImpl.class);
|
||||
this.runtime.activate(ACTOR_NAME, actorId).block();
|
||||
|
||||
Mono<Void> deacticateCall = this.runtime.deactivate(ACTOR_NAME, actorId);
|
||||
|
@ -139,7 +139,7 @@ public class ActorRuntimeTest {
|
|||
@Test
|
||||
public void lazyInvoke() throws Exception {
|
||||
String actorId = UUID.randomUUID().toString();
|
||||
this.runtime.registerActor(MyActorImpl.class, new DefaultObjectSerializer(), new DefaultObjectSerializer());
|
||||
this.runtime.registerActor(MyActorImpl.class);
|
||||
|
||||
Mono<byte[]> invokeCall = this.runtime.invoke(ACTOR_NAME, actorId, "say", null);
|
||||
|
||||
|
|
|
@ -53,9 +53,8 @@ public class ActivationDeactivationIT extends BaseIT {
|
|||
|
||||
final AtomicInteger atomicInteger = new AtomicInteger(1);
|
||||
String actorType = "DemoActorTest";
|
||||
DefaultObjectSerializer serializer = new DefaultObjectSerializer();
|
||||
logger.debug("Creating proxy builder");
|
||||
ActorProxyBuilder proxyBuilder = new ActorProxyBuilder(actorType, serializer);
|
||||
ActorProxyBuilder proxyBuilder = new ActorProxyBuilder(actorType);
|
||||
logger.debug("Creating actorId");
|
||||
ActorId actorId1 = new ActorId(Integer.toString(atomicInteger.getAndIncrement()));
|
||||
logger.debug("Building proxy");
|
||||
|
|
|
@ -49,7 +49,7 @@ public class BindingIT extends BaseIT {
|
|||
// TODO: figure out why this wait is needed for this scenario to work end-to-end. Kafka not up yet?
|
||||
Thread.sleep(120000);
|
||||
|
||||
DaprClient client = new DaprClientBuilder(new DefaultObjectSerializer(), new DefaultObjectSerializer()).build();
|
||||
DaprClient client = new DaprClientBuilder().build();
|
||||
|
||||
final String BINDING_NAME = "sample123";
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ public class GRPCStateClientIT extends BaseIT {
|
|||
5000
|
||||
);
|
||||
daprRun.switchToGRPC();
|
||||
daprClient = new DaprClientBuilder(new DefaultObjectSerializer(), new DefaultObjectSerializer()).build();
|
||||
daprClient = new DaprClientBuilder().build();
|
||||
|
||||
assertTrue(daprClient instanceof DaprClientGrpcAdapter);
|
||||
}
|
||||
|
|
|
@ -545,7 +545,7 @@ public class HttpStateClientIT extends BaseIT {
|
|||
}
|
||||
|
||||
private static DaprClient buildDaprClient() {
|
||||
return new DaprClientBuilder(new DefaultObjectSerializer(), new DefaultObjectSerializer()).build();
|
||||
return new DaprClientBuilder().build();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -19,40 +19,65 @@ import okhttp3.OkHttpClient;
|
|||
*/
|
||||
public class DaprClientBuilder {
|
||||
|
||||
/**
|
||||
* Serializer used for request and response objects in DaprClient.
|
||||
*/
|
||||
private final DaprObjectSerializer objectSerializer;
|
||||
|
||||
/**
|
||||
* Serializer used for state objects in DaprClient.
|
||||
*/
|
||||
private final DaprObjectSerializer stateSerializer;
|
||||
|
||||
/**
|
||||
* Determine if this builder will create GRPC clients instead of HTTP clients.
|
||||
*/
|
||||
private final boolean useGRPC;
|
||||
|
||||
/**
|
||||
* Serializer used for request and response objects in DaprClient.
|
||||
*/
|
||||
private DaprObjectSerializer objectSerializer;
|
||||
|
||||
/**
|
||||
* Serializer used for state objects in DaprClient.
|
||||
*/
|
||||
private DaprObjectSerializer stateSerializer;
|
||||
|
||||
/**
|
||||
* Creates a constructor for DaprClient.
|
||||
*
|
||||
* {@link DefaultObjectSerializer} is used for object and state serializers by defaul but is not recommended
|
||||
* for production scenarios.
|
||||
*/
|
||||
public DaprClientBuilder() {
|
||||
this.objectSerializer = new DefaultObjectSerializer();
|
||||
this.stateSerializer = new DefaultObjectSerializer();
|
||||
this.useGRPC = Properties.USE_GRPC.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the serializer for objects to be sent and received from Dapr.
|
||||
*
|
||||
* See {@link DefaultObjectSerializer} as possible serializer for non-production scenarios.
|
||||
*
|
||||
* @param objectSerializer Serializer for objects to be sent and received from Dapr.
|
||||
* @param stateSerializer Serializer for objects to be persisted.
|
||||
* @return This instance.
|
||||
*/
|
||||
public DaprClientBuilder(DaprObjectSerializer objectSerializer, DaprObjectSerializer stateSerializer) {
|
||||
public DaprClientBuilder withObjectSerializer(DaprObjectSerializer objectSerializer) {
|
||||
if (objectSerializer == null) {
|
||||
throw new IllegalArgumentException("Serializer is required");
|
||||
throw new IllegalArgumentException("Object serializer is required");
|
||||
}
|
||||
|
||||
this.objectSerializer = objectSerializer;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the serializer for objects to be persisted.
|
||||
*
|
||||
* See {@link DefaultObjectSerializer} as possible serializer for non-production scenarios.
|
||||
*
|
||||
* @param stateSerializer Serializer for objects to be persisted.
|
||||
* @return This instance.
|
||||
*/
|
||||
public DaprClientBuilder withStateSerializer(DaprObjectSerializer stateSerializer) {
|
||||
if (stateSerializer == null) {
|
||||
throw new IllegalArgumentException("State serializer is required");
|
||||
}
|
||||
|
||||
this.objectSerializer = objectSerializer;
|
||||
this.stateSerializer = stateSerializer;
|
||||
this.useGRPC = Properties.USE_GRPC.get();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -11,17 +11,21 @@ public class DaprClientBuilderTest {
|
|||
public void build() {
|
||||
DaprObjectSerializer objectSerializer = mock(DaprObjectSerializer.class);
|
||||
DaprObjectSerializer stateSerializer = mock(DaprObjectSerializer.class);
|
||||
DaprClientBuilder daprClientBuilder = new DaprClientBuilder(objectSerializer, stateSerializer);
|
||||
DaprClientBuilder daprClientBuilder = new DaprClientBuilder();
|
||||
daprClientBuilder.withObjectSerializer(objectSerializer);
|
||||
daprClientBuilder.withStateSerializer(stateSerializer);
|
||||
DaprClient daprClient = daprClientBuilder.build();
|
||||
assertNotNull(daprClient);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void buildException() {
|
||||
DaprClientBuilder daprClientBuilder = new DaprClientBuilder(null,null);
|
||||
DaprClient daprClient = daprClientBuilder.build();
|
||||
assertNotNull(daprClient);
|
||||
public void noObjectSerializer() {
|
||||
new DaprClientBuilder().withObjectSerializer(null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void noStateSerializer() {
|
||||
new DaprClientBuilder().withStateSerializer(null);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue