Skip to content

Commit 8c81222

Browse files
committed
prepare before testing
1 parent 10d0961 commit 8c81222

File tree

2 files changed

+119
-116
lines changed

2 files changed

+119
-116
lines changed

sdk-actors/src/main/java/io/dapr/actors/runtime/DaprGrpcClient.java

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@
1414
package io.dapr.actors.runtime;
1515

1616
import com.fasterxml.jackson.databind.ObjectMapper;
17-
import com.google.common.util.concurrent.ListenableFuture;
1817
import com.google.protobuf.Any;
1918
import com.google.protobuf.ByteString;
2019
import com.google.protobuf.Empty;
2120
import io.dapr.config.Properties;
21+
import io.dapr.exceptions.DaprException;
2222
import io.dapr.utils.DurationUtils;
2323
import io.dapr.v1.DaprGrpc;
2424
import io.dapr.v1.DaprProtos;
2525
import io.grpc.ManagedChannel;
26+
import io.grpc.stub.StreamObserver;
2627
import reactor.core.publisher.Mono;
28+
import reactor.core.publisher.MonoSink;
2729

2830
import java.io.IOException;
2931
import java.nio.charset.Charset;
3032
import java.util.ArrayList;
3133
import java.util.List;
34+
import java.util.concurrent.ExecutionException;
3235

3336
/**
3437
* A DaprClient over HTTP for Actor's runtime.
@@ -48,44 +51,41 @@ class DaprGrpcClient implements DaprClient {
4851
/**
4952
* The GRPC client to be used.
5053
*
51-
* @see io.dapr.v1.DaprGrpc.DaprFutureStub
54+
* @see io.dapr.v1.DaprGrpc.DaprStub
5255
*/
53-
private DaprGrpc.DaprFutureStub client;
56+
private DaprGrpc.DaprStub client;
5457

5558
/**
5659
* Internal constructor.
5760
*
5861
* @param channel channel (client needs to close channel after use).
5962
*/
6063
DaprGrpcClient(ManagedChannel channel) {
61-
this(DaprGrpc.newFutureStub(channel));
64+
this(DaprGrpc.newStub(channel));
6265
}
6366

6467
/**
6568
* Internal constructor.
6669
*
67-
* @param grpcClient Dapr's GRPC client.
70+
* @param daprStubClient Dapr's GRPC client.
6871
*/
69-
DaprGrpcClient(DaprGrpc.DaprFutureStub grpcClient) {
70-
this.client = grpcClient;
72+
DaprGrpcClient(DaprGrpc.DaprStub daprStubClient) {
73+
this.client = daprStubClient;
7174
}
7275

7376
/**
7477
* {@inheritDoc}
7578
*/
7679
@Override
7780
public Mono<byte[]> getState(String actorType, String actorId, String keyName) {
78-
return Mono.fromCallable(() -> {
7981
DaprProtos.GetActorStateRequest req =
80-
DaprProtos.GetActorStateRequest.newBuilder()
81-
.setActorType(actorType)
82-
.setActorId(actorId)
83-
.setKey(keyName)
84-
.build();
82+
DaprProtos.GetActorStateRequest.newBuilder()
83+
.setActorType(actorType)
84+
.setActorId(actorId)
85+
.setKey(keyName)
86+
.build();
8587

86-
ListenableFuture<DaprProtos.GetActorStateResponse> futureResponse = client.getActorState(req);
87-
return futureResponse.get();
88-
}).map(r -> r.getData().toByteArray());
88+
return Mono.<DaprProtos.GetActorStateResponse>create(it -> client.getActorState(req, createStreamObserver(it))).map(r -> r.getData().toByteArray());
8989
}
9090

9191
/**
@@ -132,10 +132,7 @@ public Mono<Void> saveStateTransactionally(
132132
.addAllOperations(grpcOps)
133133
.build();
134134

135-
return Mono.fromCallable(() -> {
136-
ListenableFuture<Empty> futureResponse = client.executeActorStateTransaction(req);
137-
return futureResponse.get();
138-
}).then();
135+
return Mono.<Empty>create(it -> client.executeActorStateTransaction(req, createStreamObserver(it))).then();
139136
}
140137

141138
/**
@@ -147,40 +144,31 @@ public Mono<Void> registerReminder(
147144
String actorId,
148145
String reminderName,
149146
ActorReminderParams reminderParams) {
150-
return Mono.fromCallable(() -> {
151-
DaprProtos.RegisterActorReminderRequest req =
152-
DaprProtos.RegisterActorReminderRequest.newBuilder()
153-
.setActorType(actorType)
154-
.setActorId(actorId)
155-
.setName(reminderName)
156-
.setData(ByteString.copyFrom(reminderParams.getData()))
157-
.setDueTime(DurationUtils.convertDurationToDaprFormat(reminderParams.getDueTime()))
158-
.setPeriod(DurationUtils.convertDurationToDaprFormat(reminderParams.getPeriod()))
159-
.build();
160-
161-
ListenableFuture<Empty> futureResponse = client.registerActorReminder(req);
162-
futureResponse.get();
163-
return null;
164-
});
147+
DaprProtos.RegisterActorReminderRequest req =
148+
DaprProtos.RegisterActorReminderRequest.newBuilder()
149+
.setActorType(actorType)
150+
.setActorId(actorId)
151+
.setName(reminderName)
152+
.setData(ByteString.copyFrom(reminderParams.getData()))
153+
.setDueTime(DurationUtils.convertDurationToDaprFormat(reminderParams.getDueTime()))
154+
.setPeriod(DurationUtils.convertDurationToDaprFormat(reminderParams.getPeriod()))
155+
.build();
156+
return Mono.<Empty>create(it -> client.registerActorReminder(req, createStreamObserver(it))).then().then();
165157
}
166158

167159
/**
168160
* {@inheritDoc}
169161
*/
170162
@Override
171163
public Mono<Void> unregisterReminder(String actorType, String actorId, String reminderName) {
172-
return Mono.fromCallable(() -> {
173164
DaprProtos.UnregisterActorReminderRequest req =
174165
DaprProtos.UnregisterActorReminderRequest.newBuilder()
175166
.setActorType(actorType)
176167
.setActorId(actorId)
177168
.setName(reminderName)
178169
.build();
179170

180-
ListenableFuture<Empty> futureResponse = client.unregisterActorReminder(req);
181-
futureResponse.get();
182-
return null;
183-
});
171+
return Mono.<Empty>create(it -> client.unregisterActorReminder(req, createStreamObserver(it))).then().then();
184172
}
185173

186174
/**
@@ -192,7 +180,6 @@ public Mono<Void> registerTimer(
192180
String actorId,
193181
String timerName,
194182
ActorTimerParams timerParams) {
195-
return Mono.fromCallable(() -> {
196183
DaprProtos.RegisterActorTimerRequest req =
197184
DaprProtos.RegisterActorTimerRequest.newBuilder()
198185
.setActorType(actorType)
@@ -204,29 +191,41 @@ public Mono<Void> registerTimer(
204191
.setPeriod(DurationUtils.convertDurationToDaprFormat(timerParams.getPeriod()))
205192
.build();
206193

207-
ListenableFuture<Empty> futureResponse = client.registerActorTimer(req);
208-
futureResponse.get();
209-
return null;
210-
});
194+
return Mono.<Empty>create(it -> client.registerActorTimer(req, createStreamObserver(it))).then().then();
211195
}
212196

213197
/**
214198
* {@inheritDoc}
215199
*/
216200
@Override
217201
public Mono<Void> unregisterTimer(String actorType, String actorId, String timerName) {
218-
return Mono.fromCallable(() -> {
219202
DaprProtos.UnregisterActorTimerRequest req =
220203
DaprProtos.UnregisterActorTimerRequest.newBuilder()
221204
.setActorType(actorType)
222205
.setActorId(actorId)
223206
.setName(timerName)
224207
.build();
225208

226-
ListenableFuture<Empty> futureResponse = client.unregisterActorTimer(req);
227-
futureResponse.get();
228-
return null;
229-
});
209+
return Mono.<Empty>create(it -> client.unregisterActorTimer(req, createStreamObserver(it))).then().then();
210+
}
211+
212+
private <T> StreamObserver<T> createStreamObserver(MonoSink<T> sink) {
213+
return new StreamObserver<T>() {
214+
@Override
215+
public void onNext(T value) {
216+
sink.success(value);
217+
}
218+
219+
@Override
220+
public void onError(Throwable t) {
221+
sink.error(DaprException.propagate(new ExecutionException(t)));
222+
}
223+
224+
@Override
225+
public void onCompleted() {
226+
sink.success();
227+
}
228+
};
230229
}
231230

232231
}

0 commit comments

Comments
 (0)