1818import io .dapr .config .Properties ;
1919import io .dapr .serializer .DaprObjectSerializer ;
2020import io .dapr .serializer .DefaultObjectSerializer ;
21- import io .dapr .utils .Version ;
21+ import io .dapr .utils .NetworkUtils ;
2222import io .grpc .ManagedChannel ;
23- import io .grpc .ManagedChannelBuilder ;
2423import reactor .core .publisher .Mono ;
2524
2625import java .io .Closeable ;
@@ -80,23 +79,32 @@ public class ActorRuntime implements Closeable {
8079 * @throws IllegalStateException If cannot instantiate Runtime.
8180 */
8281 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 ));
8492 }
8593
8694 /**
8795 * Constructor once channel is available. This should not be called directly.
8896 *
8997 * @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.
9199 */
92100 private ActorRuntime (ManagedChannel channel ) throws IllegalStateException {
93- this (channel , buildDaprClient (channel ));
101+ this (channel , new DaprClientImpl (channel ));
94102 }
95103
96104 /**
97105 * Constructor with dependency injection, useful for testing. This should not be called directly.
98106 *
99- * @param channel GRPC managed channel to be closed (or null).
107+ * @param channel GRPC managed channel to be closed (or null).
100108 * @param daprClient Client to communicate with Dapr.
101109 * @throws IllegalStateException If class has one instance already.
102110 */
@@ -128,6 +136,24 @@ public static ActorRuntime getInstance() {
128136 return instance ;
129137 }
130138
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+
131157 /**
132158 * Gets the Actor configuration for this runtime.
133159 *
@@ -149,24 +175,22 @@ public byte[] serializeConfig() throws IOException {
149175
150176 /**
151177 * Registers an actor with the runtime, using {@link DefaultObjectSerializer} and {@link DefaultActorFactory}.
152- *
153178 * {@link DefaultObjectSerializer} is not recommended for production scenarios.
154179 *
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.
157182 */
158183 public <T extends AbstractActor > void registerActor (Class <T > clazz ) {
159184 registerActor (clazz , new DefaultObjectSerializer (), new DefaultObjectSerializer ());
160185 }
161186
162187 /**
163188 * Registers an actor with the runtime, using {@link DefaultObjectSerializer}.
164- *
165189 * {@link DefaultObjectSerializer} is not recommended for production scenarios.
166190 *
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.
170194 */
171195 public <T extends AbstractActor > void registerActor (Class <T > clazz , ActorFactory <T > actorFactory ) {
172196 registerActor (clazz , actorFactory , new DefaultObjectSerializer (), new DefaultObjectSerializer ());
@@ -181,8 +205,8 @@ public <T extends AbstractActor> void registerActor(Class<T> clazz, ActorFactory
181205 * @param <T> Actor class type.
182206 */
183207 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 );
186210 }
187211
188212 /**
@@ -195,9 +219,9 @@ public <T extends AbstractActor> void registerActor(
195219 * @param <T> Actor class type.
196220 */
197221 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 ) {
201225 if (clazz == null ) {
202226 throw new IllegalArgumentException ("Class is required." );
203227 }
@@ -216,12 +240,12 @@ public <T extends AbstractActor> void registerActor(
216240 // Create ActorManager, if not yet registered.
217241 this .actorManagers .computeIfAbsent (actorTypeInfo .getName (), (k ) -> {
218242 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 ));
225249 this .config .addRegisteredActorType (actorTypeInfo .getName ());
226250 return new ActorManager <T >(context );
227251 });
@@ -236,7 +260,7 @@ public <T extends AbstractActor> void registerActor(
236260 */
237261 public Mono <Void > deactivate (String actorTypeName , String actorId ) {
238262 return Mono .fromSupplier (() -> this .getActorManager (actorTypeName ))
239- .flatMap (m -> m .deactivateActor (new ActorId (actorId )));
263+ .flatMap (m -> m .deactivateActor (new ActorId (actorId )));
240264 }
241265
242266 /**
@@ -252,8 +276,8 @@ public Mono<Void> deactivate(String actorTypeName, String actorId) {
252276 public Mono <byte []> invoke (String actorTypeName , String actorId , String actorMethodName , byte [] payload ) {
253277 ActorId id = new ActorId (actorId );
254278 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 ));
257281 }
258282
259283 /**
@@ -268,8 +292,8 @@ public Mono<byte[]> invoke(String actorTypeName, String actorId, String actorMet
268292 public Mono <Void > invokeReminder (String actorTypeName , String actorId , String reminderName , byte [] params ) {
269293 ActorId id = new ActorId (actorId );
270294 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 ));
273297 }
274298
275299 /**
@@ -284,8 +308,8 @@ public Mono<Void> invokeReminder(String actorTypeName, String actorId, String re
284308 public Mono <Void > invokeTimer (String actorTypeName , String actorId , String timerName , byte [] params ) {
285309 ActorId id = new ActorId (actorId );
286310 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 ));
289313 }
290314
291315 /**
@@ -318,23 +342,6 @@ private static DaprClient buildDaprClient(ManagedChannel channel) {
318342 return new DaprClientImpl (channel );
319343 }
320344
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-
338345 /**
339346 * {@inheritDoc}
340347 */
0 commit comments