18
18
import io .dapr .config .Properties ;
19
19
import io .dapr .serializer .DaprObjectSerializer ;
20
20
import io .dapr .serializer .DefaultObjectSerializer ;
21
- import io .dapr .utils .Version ;
21
+ import io .dapr .utils .NetworkUtils ;
22
22
import io .grpc .ManagedChannel ;
23
- import io .grpc .ManagedChannelBuilder ;
24
23
import reactor .core .publisher .Mono ;
25
24
26
25
import java .io .Closeable ;
@@ -80,23 +79,32 @@ public class ActorRuntime implements Closeable {
80
79
* @throws IllegalStateException If cannot instantiate Runtime.
81
80
*/
82
81
private ActorRuntime () throws IllegalStateException {
83
- this (buildManagedChannel ());
82
+ this (new Properties ());
83
+ }
84
+
85
+ /**
86
+ * The default constructor. This should not be called directly.
87
+ *
88
+ * @throws IllegalStateException If cannot instantiate Runtime.
89
+ */
90
+ private ActorRuntime (Properties properties ) throws IllegalStateException {
91
+ this (NetworkUtils .buildGrpcManagedChannel (properties ));
84
92
}
85
93
86
94
/**
87
95
* Constructor once channel is available. This should not be called directly.
88
96
*
89
97
* @param channel GRPC managed channel to be closed (or null).
90
- * @throws IllegalStateException If cannot instantiate Runtime.
98
+ * @throws IllegalStateException If you cannot instantiate Runtime.
91
99
*/
92
100
private ActorRuntime (ManagedChannel channel ) throws IllegalStateException {
93
- this (channel , buildDaprClient (channel ));
101
+ this (channel , new DaprClientImpl (channel ));
94
102
}
95
103
96
104
/**
97
105
* Constructor with dependency injection, useful for testing. This should not be called directly.
98
106
*
99
- * @param channel GRPC managed channel to be closed (or null).
107
+ * @param channel GRPC managed channel to be closed (or null).
100
108
* @param daprClient Client to communicate with Dapr.
101
109
* @throws IllegalStateException If class has one instance already.
102
110
*/
@@ -128,6 +136,24 @@ public static ActorRuntime getInstance() {
128
136
return instance ;
129
137
}
130
138
139
+ /**
140
+ * Returns an ActorRuntime object.
141
+ *
142
+ * @param properties Properties to be used for the runtime.
143
+ * @return An ActorRuntime object.
144
+ */
145
+ public static ActorRuntime getInstance (Properties properties ) {
146
+ if (instance == null ) {
147
+ synchronized (ActorRuntime .class ) {
148
+ if (instance == null ) {
149
+ instance = new ActorRuntime (properties );
150
+ }
151
+ }
152
+ }
153
+
154
+ return instance ;
155
+ }
156
+
131
157
/**
132
158
* Gets the Actor configuration for this runtime.
133
159
*
@@ -149,24 +175,22 @@ public byte[] serializeConfig() throws IOException {
149
175
150
176
/**
151
177
* Registers an actor with the runtime, using {@link DefaultObjectSerializer} and {@link DefaultActorFactory}.
152
- *
153
178
* {@link DefaultObjectSerializer} is not recommended for production scenarios.
154
179
*
155
- * @param clazz The type of actor.
156
- * @param <T> Actor class type.
180
+ * @param clazz The type of actor.
181
+ * @param <T> Actor class type.
157
182
*/
158
183
public <T extends AbstractActor > void registerActor (Class <T > clazz ) {
159
184
registerActor (clazz , new DefaultObjectSerializer (), new DefaultObjectSerializer ());
160
185
}
161
186
162
187
/**
163
188
* Registers an actor with the runtime, using {@link DefaultObjectSerializer}.
164
- *
165
189
* {@link DefaultObjectSerializer} is not recommended for production scenarios.
166
190
*
167
- * @param clazz The type of actor.
168
- * @param actorFactory An optional factory to create actors. This can be used for dependency injection.
169
- * @param <T> Actor class type.
191
+ * @param clazz The type of actor.
192
+ * @param actorFactory An optional factory to create actors. This can be used for dependency injection.
193
+ * @param <T> Actor class type.
170
194
*/
171
195
public <T extends AbstractActor > void registerActor (Class <T > clazz , ActorFactory <T > actorFactory ) {
172
196
registerActor (clazz , actorFactory , new DefaultObjectSerializer (), new DefaultObjectSerializer ());
@@ -181,8 +205,8 @@ public <T extends AbstractActor> void registerActor(Class<T> clazz, ActorFactory
181
205
* @param <T> Actor class type.
182
206
*/
183
207
public <T extends AbstractActor > void registerActor (
184
- Class <T > clazz , DaprObjectSerializer objectSerializer , DaprObjectSerializer stateSerializer ) {
185
- registerActor (clazz , new DefaultActorFactory <T >(), objectSerializer , stateSerializer );
208
+ Class <T > clazz , DaprObjectSerializer objectSerializer , DaprObjectSerializer stateSerializer ) {
209
+ registerActor (clazz , new DefaultActorFactory <T >(), objectSerializer , stateSerializer );
186
210
}
187
211
188
212
/**
@@ -195,9 +219,9 @@ public <T extends AbstractActor> void registerActor(
195
219
* @param <T> Actor class type.
196
220
*/
197
221
public <T extends AbstractActor > void registerActor (
198
- Class <T > clazz , ActorFactory <T > actorFactory ,
199
- DaprObjectSerializer objectSerializer ,
200
- DaprObjectSerializer stateSerializer ) {
222
+ Class <T > clazz , ActorFactory <T > actorFactory ,
223
+ DaprObjectSerializer objectSerializer ,
224
+ DaprObjectSerializer stateSerializer ) {
201
225
if (clazz == null ) {
202
226
throw new IllegalArgumentException ("Class is required." );
203
227
}
@@ -216,12 +240,12 @@ public <T extends AbstractActor> void registerActor(
216
240
// Create ActorManager, if not yet registered.
217
241
this .actorManagers .computeIfAbsent (actorTypeInfo .getName (), (k ) -> {
218
242
ActorRuntimeContext <T > context = new ActorRuntimeContext <>(
219
- this ,
220
- objectSerializer ,
221
- actorFactory ,
222
- actorTypeInfo ,
223
- this .daprClient ,
224
- new DaprStateAsyncProvider (this .daprClient , stateSerializer ));
243
+ this ,
244
+ objectSerializer ,
245
+ actorFactory ,
246
+ actorTypeInfo ,
247
+ this .daprClient ,
248
+ new DaprStateAsyncProvider (this .daprClient , stateSerializer ));
225
249
this .config .addRegisteredActorType (actorTypeInfo .getName ());
226
250
return new ActorManager <T >(context );
227
251
});
@@ -236,7 +260,7 @@ public <T extends AbstractActor> void registerActor(
236
260
*/
237
261
public Mono <Void > deactivate (String actorTypeName , String actorId ) {
238
262
return Mono .fromSupplier (() -> this .getActorManager (actorTypeName ))
239
- .flatMap (m -> m .deactivateActor (new ActorId (actorId )));
263
+ .flatMap (m -> m .deactivateActor (new ActorId (actorId )));
240
264
}
241
265
242
266
/**
@@ -252,8 +276,8 @@ public Mono<Void> deactivate(String actorTypeName, String actorId) {
252
276
public Mono <byte []> invoke (String actorTypeName , String actorId , String actorMethodName , byte [] payload ) {
253
277
ActorId id = new ActorId (actorId );
254
278
return Mono .fromSupplier (() -> this .getActorManager (actorTypeName ))
255
- .flatMap (m -> m .activateActor (id ).thenReturn (m ))
256
- .flatMap (m -> ((ActorManager )m ).invokeMethod (id , actorMethodName , payload ));
279
+ .flatMap (m -> m .activateActor (id ).thenReturn (m ))
280
+ .flatMap (m -> ((ActorManager ) m ).invokeMethod (id , actorMethodName , payload ));
257
281
}
258
282
259
283
/**
@@ -268,8 +292,8 @@ public Mono<byte[]> invoke(String actorTypeName, String actorId, String actorMet
268
292
public Mono <Void > invokeReminder (String actorTypeName , String actorId , String reminderName , byte [] params ) {
269
293
ActorId id = new ActorId (actorId );
270
294
return Mono .fromSupplier (() -> this .getActorManager (actorTypeName ))
271
- .flatMap (m -> m .activateActor (id ).thenReturn (m ))
272
- .flatMap (m -> ((ActorManager )m ).invokeReminder (new ActorId (actorId ), reminderName , params ));
295
+ .flatMap (m -> m .activateActor (id ).thenReturn (m ))
296
+ .flatMap (m -> ((ActorManager ) m ).invokeReminder (new ActorId (actorId ), reminderName , params ));
273
297
}
274
298
275
299
/**
@@ -284,8 +308,8 @@ public Mono<Void> invokeReminder(String actorTypeName, String actorId, String re
284
308
public Mono <Void > invokeTimer (String actorTypeName , String actorId , String timerName , byte [] params ) {
285
309
ActorId id = new ActorId (actorId );
286
310
return Mono .fromSupplier (() -> this .getActorManager (actorTypeName ))
287
- .flatMap (m -> m .activateActor (id ).thenReturn (m ))
288
- .flatMap (m -> ((ActorManager )m ).invokeTimer (new ActorId (actorId ), timerName , params ));
311
+ .flatMap (m -> m .activateActor (id ).thenReturn (m ))
312
+ .flatMap (m -> ((ActorManager ) m ).invokeTimer (new ActorId (actorId ), timerName , params ));
289
313
}
290
314
291
315
/**
@@ -318,23 +342,6 @@ private static DaprClient buildDaprClient(ManagedChannel channel) {
318
342
return new DaprClientImpl (channel );
319
343
}
320
344
321
- /**
322
- * Creates a GRPC managed channel (or null, if not applicable).
323
- *
324
- * @return GRPC managed channel or null.
325
- */
326
- private static ManagedChannel buildManagedChannel () {
327
- int port = Properties .GRPC_PORT .get ();
328
- if (port <= 0 ) {
329
- throw new IllegalStateException ("Invalid port." );
330
- }
331
-
332
- return ManagedChannelBuilder .forAddress (Properties .SIDECAR_IP .get (), port )
333
- .usePlaintext ()
334
- .userAgent (Version .getSdkVersion ())
335
- .build ();
336
- }
337
-
338
345
/**
339
346
* {@inheritDoc}
340
347
*/
0 commit comments