1919 * ActorRef is responsible for representing an instance of an Actor
2020 *
2121 * @author Adriano Santos
22- *
2322 */
2423public final class ActorRef {
2524 private static final int CACHE_MAXIMUM_SIZE = 1_000 ;
@@ -41,16 +40,17 @@ private ActorRef(ActorOuterClass.ActorId actorId, SpawnClient client) {
4140 /**
4241 * <p>This method is responsible for creating instances of the ActorRef class
4342 * </p>
43+ *
4444 * @param client is the client part of the Spawn protocol and is responsible for communicating with the Proxy.
4545 * @param system ActorSystem name of the actor that this ActorRef instance should represent
46- * @param name the name of the actor that this ActorRef instance should represent
46+ * @param name the name of the actor that this ActorRef instance should represent
4747 * @return the ActorRef instance
4848 * @since 0.0.1
4949 */
5050 protected static ActorRef of (SpawnClient client , String system , String name ) throws Exception {
5151 ActorOuterClass .ActorId actorId = buildActorId (system , name );
5252 ActorRef ref = ACTOR_REF_CACHE .getIfPresent (actorId );
53- if (Objects .nonNull (ref )){
53+ if (Objects .nonNull (ref )) {
5454 return ref ;
5555 }
5656
@@ -62,17 +62,18 @@ protected static ActorRef of(SpawnClient client, String system, String name) thr
6262 /**
6363 * <p>This method is responsible for creating instances of the ActorRef class when Actor is a UnNamed actor.
6464 * </p>
65+ *
6566 * @param client is the client part of the Spawn protocol and is responsible for communicating with the Proxy.
6667 * @param system ActorSystem name of the actor that this ActorRef instance should represent
67- * @param name the name of the actor that this ActorRef instance should represent
68+ * @param name the name of the actor that this ActorRef instance should represent
6869 * @param parent the name of the unnamed parent actor
6970 * @return the ActorRef instance
7071 * @since 0.0.1
7172 */
7273 protected static ActorRef of (SpawnClient client , String system , String name , String parent ) throws Exception {
7374 ActorOuterClass .ActorId actorId = buildActorId (system , name , parent );
7475 ActorRef ref = ACTOR_REF_CACHE .getIfPresent (actorId );
75- if (Objects .nonNull (ref )){
76+ if (Objects .nonNull (ref )) {
7677 return ref ;
7778 }
7879
@@ -82,18 +83,42 @@ protected static ActorRef of(SpawnClient client, String system, String name, Str
8283 return ref ;
8384 }
8485
86+ private static ActorOuterClass .ActorId buildActorId (String system , String name ) {
87+ ActorOuterClass .ActorId .Builder actorIdBuilder = ActorOuterClass .ActorId .newBuilder ()
88+ .setSystem (system )
89+ .setName (name );
90+
91+ return actorIdBuilder .build ();
92+ }
93+
94+ private static ActorOuterClass .ActorId buildActorId (String system , String name , String parent ) {
95+ return ActorOuterClass .ActorId .newBuilder ()
96+ .setSystem (system )
97+ .setName (name )
98+ .setParent (parent )
99+ .build ();
100+ }
101+
102+ private static void spawnActor (ActorOuterClass .ActorId actorId , SpawnClient client ) throws Exception {
103+ Protocol .SpawnRequest req = Protocol .SpawnRequest .newBuilder ()
104+ .addActors (actorId )
105+ .build ();
106+ client .spawn (req );
107+ }
108+
85109 /**
86110 * <p>This method synchronously invokes an action on the actor that this ActorRef instance represents through the Spawn Proxy.
87111 * Used when it is not necessary to send parameters to the Action.
88112 * </p>
89- * @param action name of the action to be called.
113+ *
114+ * @param action name of the action to be called.
90115 * @param outputType the class that corresponds to the expected return type
91116 * @return an Optional containing, or not, the response object to the Action call
92117 * @since 0.0.1
93118 */
94- public <T extends GeneratedMessageV3 > Optional <T > invoke (String action , Class <T > outputType ) throws Exception {
119+ public <T extends GeneratedMessageV3 > Optional <T > invoke (String action , Class <T > outputType ) throws Exception {
95120 Optional <T > res = invokeActor (action , Empty .getDefaultInstance (), outputType , Optional .empty ());
96- if (res .isPresent () ) {
121+ if (res .isPresent ()) {
97122 return Optional .of (outputType .cast (res .get ()));
98123 }
99124
@@ -104,16 +129,17 @@ public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T
104129 * <p>This method synchronously invokes an action on the actor that this ActorRef instance represents through the Spawn Proxy.
105130 * Used when it is not necessary to send parameters to the Action.
106131 * </p>
107- * @param action name of the action to be called.
132+ *
133+ * @param action name of the action to be called.
108134 * @param outputType the class that corresponds to the expected return type
109- * @param opts options that can be passed during the invocation of the Action.
110- * Please see the {@link io.eigr.spawn.api.InvocationOpts} class for more information
135+ * @param opts options that can be passed during the invocation of the Action.
136+ * Please see the {@link io.eigr.spawn.api.InvocationOpts} class for more information
111137 * @return an Optional containing, or not, the response object to the Action call
112138 * @since 0.0.1
113139 */
114- public <T extends GeneratedMessageV3 > Optional <T > invoke (String action , Class <T > outputType , InvocationOpts opts ) throws Exception {
140+ public <T extends GeneratedMessageV3 > Optional <T > invoke (String action , Class <T > outputType , InvocationOpts opts ) throws Exception {
115141 Optional <T > res = invokeActor (action , Empty .getDefaultInstance (), outputType , Optional .ofNullable (opts ));
116- if (res .isPresent () ) {
142+ if (res .isPresent ()) {
117143 return Optional .of (outputType .cast (res .get ()));
118144 }
119145
@@ -124,15 +150,16 @@ public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T
124150 * <p>This method synchronously invokes an action on the actor that this ActorRef instance represents through the Spawn Proxy.
125151 * Used when it is not necessary to send parameters to the Action.
126152 * </p>
127- * @param action name of the action to be called.
128- * @param value the action argument object.
153+ *
154+ * @param action name of the action to be called.
155+ * @param value the action argument object.
129156 * @param outputType the class that corresponds to the expected return type
130157 * @return an Optional containing, or not, the response object to the Action call
131158 * @since 0.0.1
132159 */
133160 public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > Optional <T > invoke (String action , S value , Class <T > outputType ) throws Exception {
134161 Optional <T > res = invokeActor (action , value , outputType , Optional .empty ());
135- if (res .isPresent () ) {
162+ if (res .isPresent ()) {
136163 return Optional .of (outputType .cast (res .get ()));
137164 }
138165
@@ -143,17 +170,18 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
143170 * <p>This method synchronously invokes an action on the actor that this ActorRef instance represents through the Spawn Proxy.
144171 * Used when it is not necessary to send parameters to the Action.
145172 * </p>
146- * @param action name of the action to be called.
147- * @param value the action argument object.
173+ *
174+ * @param action name of the action to be called.
175+ * @param value the action argument object.
148176 * @param outputType the class that corresponds to the expected return type
149- * @param opts options that can be passed during the invocation of the Action.
150- * Please see the {@link io.eigr.spawn.api.InvocationOpts} class for more information
177+ * @param opts options that can be passed during the invocation of the Action.
178+ * Please see the {@link io.eigr.spawn.api.InvocationOpts} class for more information
151179 * @return an Optional containing, or not, the response object to the Action call
152180 * @since 0.0.1
153181 */
154182 public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > Optional <T > invoke (String action , S value , Class <T > outputType , InvocationOpts opts ) throws Exception {
155183 Optional <T > res = invokeActor (action , value , outputType , Optional .ofNullable (opts ));
156- if (res .isPresent () ) {
184+ if (res .isPresent ()) {
157185 return Optional .of (outputType .cast (res .get ()));
158186 }
159187
@@ -164,10 +192,11 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
164192 * <p>This method asynchronously invokes an action on the actor that this ActorRef instance represents via the Spawn Proxy.
165193 * Used when it is not necessary to send parameters to the Action.
166194 * </p>
195+ *
167196 * @param action name of the action to be called.
168197 * @since 0.0.1
169198 */
170- public <T extends GeneratedMessageV3 > void invokeAsync (String action ) throws Exception {
199+ public <T extends GeneratedMessageV3 > void invokeAsync (String action ) throws Exception {
171200 InvocationOpts opts = InvocationOpts .builder ().async (true ).build ();
172201 invokeActor (action , Empty .getDefaultInstance (), null , Optional .of (opts ));
173202 }
@@ -176,9 +205,10 @@ public <T extends GeneratedMessageV3> void invokeAsync(String action) throws Ex
176205 * <p>This method asynchronously invokes an action on the actor that this ActorRef instance represents via the Spawn Proxy.
177206 * Used when it is not necessary to send parameters to the Action.
178207 * </p>
208+ *
179209 * @param action name of the action to be called.
180- * @param opts options that can be passed during the invocation of the Action.
181- * Please see the {@link io.eigr.spawn.api.InvocationOpts} class for more information
210+ * @param opts options that can be passed during the invocation of the Action.
211+ * Please see the {@link io.eigr.spawn.api.InvocationOpts} class for more information
182212 * @since 0.0.1
183213 */
184214 public <T extends GeneratedMessageV3 > void invokeAsync (String action , InvocationOpts opts ) throws Exception {
@@ -195,8 +225,9 @@ public <T extends GeneratedMessageV3> void invokeAsync(String action, Invocation
195225 * <p>This method asynchronously invokes an action on the actor that this ActorRef instance represents through the Spawn Proxy.
196226 * Used when it is not necessary to send parameters to the Action.
197227 * </p>
228+ *
198229 * @param action name of the action to be called.
199- * @param value the action argument object.
230+ * @param value the action argument object.
200231 * @since 0.0.1
201232 */
202233 public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > void invokeAsync (String action , S value ) throws Exception {
@@ -208,10 +239,11 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeA
208239 * <p>This method asynchronously invokes an action on the actor that this ActorRef instance represents through the Spawn Proxy.
209240 * Used when it is not necessary to send parameters to the Action.
210241 * </p>
242+ *
211243 * @param action name of the action to be called.
212- * @param value the action argument object.
213- * @param opts options that can be passed during the invocation of the Action.
214- * Please see the {@link io.eigr.spawn.api.InvocationOpts} class for more information
244+ * @param value the action argument object.
245+ * @param opts options that can be passed during the invocation of the Action.
246+ * Please see the {@link io.eigr.spawn.api.InvocationOpts} class for more information
215247 * @since 0.0.1
216248 */
217249 public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > void invokeAsync (String action , S value , InvocationOpts opts ) throws Exception {
@@ -250,6 +282,8 @@ public boolean isUnNamedActor() {
250282
251283 private <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > Optional <T > invokeActor (
252284 String cmd , S argument , Class <T > outputType , Optional <InvocationOpts > options ) throws Exception {
285+ Objects .requireNonNull (this .actorId , "ActorId cannot be null" );
286+
253287 Protocol .InvocationRequest .Builder invocationRequestBuilder = Protocol .InvocationRequest .newBuilder ();
254288
255289 if (options .isPresent ()) {
@@ -282,8 +316,10 @@ private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
282316 case UNKNOWN :
283317 case ERROR :
284318 case UNRECOGNIZED :
285- throw new ActorInvokeException (
286- String .format ("Unknown error when trying to invoke Actor %s" , this .getActorName ()));
319+ String msg = String .format ("Error when trying to invoke Actor %s. Details: %s" ,
320+ this .getActorName (), status .getMessage ());
321+
322+ throw new ActorInvokeException (msg );
287323 case ACTOR_NOT_FOUND :
288324 throw new ActorNotFoundException ();
289325 case OK :
@@ -295,27 +331,4 @@ private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
295331
296332 return Optional .empty ();
297333 }
298-
299- private static ActorOuterClass .ActorId buildActorId (String system , String name ) {
300- ActorOuterClass .ActorId .Builder actorIdBuilder = ActorOuterClass .ActorId .newBuilder ()
301- .setSystem (system )
302- .setName (name );
303-
304- return actorIdBuilder .build ();
305- }
306-
307- private static ActorOuterClass .ActorId buildActorId (String system , String name , String parent ) {
308- return ActorOuterClass .ActorId .newBuilder ()
309- .setSystem (system )
310- .setName (name )
311- .setParent (parent )
312- .build ();
313- }
314-
315- private static void spawnActor (ActorOuterClass .ActorId actorId , SpawnClient client ) throws Exception {
316- Protocol .SpawnRequest req = Protocol .SpawnRequest .newBuilder ()
317- .addActors (actorId )
318- .build ();
319- client .spawn (req );
320- }
321334}
0 commit comments