77import com .google .protobuf .GeneratedMessageV3 ;
88import io .eigr .functions .protocol .Protocol ;
99import io .eigr .functions .protocol .actors .ActorOuterClass ;
10- import io .eigr .spawn .api .exceptions .ActorInvokeException ;
10+ import io .eigr .spawn .api .exceptions .ActorCreationException ;
11+ import io .eigr .spawn .api .exceptions .ActorInvocationException ;
1112import io .eigr .spawn .api .exceptions .ActorNotFoundException ;
13+ import io .eigr .spawn .api .exceptions .SpawnException ;
1214import io .eigr .spawn .internal .transport .client .SpawnClient ;
1315
1416import java .time .Duration ;
@@ -49,7 +51,7 @@ private ActorRef(ActorOuterClass.ActorId actorId, SpawnClient client) {
4951 * @return the ActorRef instance
5052 * @since 0.0.1
5153 */
52- protected static ActorRef of (SpawnClient client , String system , String name ) throws Exception {
54+ protected static ActorRef of (SpawnClient client , String system , String name ) throws ActorCreationException {
5355 ActorOuterClass .ActorId actorId = buildActorId (system , name );
5456 ActorRef ref = ACTOR_REF_CACHE .getIfPresent (actorId );
5557 if (Objects .nonNull (ref )) {
@@ -72,7 +74,7 @@ protected static ActorRef of(SpawnClient client, String system, String name) thr
7274 * @return the ActorRef instance
7375 * @since 0.0.1
7476 */
75- protected static ActorRef of (SpawnClient client , String system , String name , String parent ) throws Exception {
77+ protected static ActorRef of (SpawnClient client , String system , String name , String parent ) throws ActorCreationException {
7678 ActorOuterClass .ActorId actorId = buildActorId (system , name , parent );
7779 ActorRef ref = ACTOR_REF_CACHE .getIfPresent (actorId );
7880 if (Objects .nonNull (ref )) {
@@ -101,7 +103,7 @@ private static ActorOuterClass.ActorId buildActorId(String system, String name,
101103 .build ();
102104 }
103105
104- private static void spawnActor (ActorOuterClass .ActorId actorId , SpawnClient client ) throws Exception {
106+ private static void spawnActor (ActorOuterClass .ActorId actorId , SpawnClient client ) throws ActorCreationException {
105107 Protocol .SpawnRequest req = Protocol .SpawnRequest .newBuilder ()
106108 .addActors (actorId )
107109 .build ();
@@ -118,13 +120,10 @@ private static void spawnActor(ActorOuterClass.ActorId actorId, SpawnClient clie
118120 * @return an Optional containing, or not, the response object to the Action call
119121 * @since 0.0.1
120122 */
121- public <T extends GeneratedMessageV3 > Optional <T > invoke (String action , Class <T > outputType ) throws Exception {
123+ public <T extends GeneratedMessageV3 > Optional <T > invoke (String action , Class <T > outputType ) throws ActorInvocationException {
122124 Optional <T > res = invokeActor (action , Empty .getDefaultInstance (), outputType , Optional .empty ());
123- if (res .isPresent ()) {
124- return Optional .of (outputType .cast (res .get ()));
125- }
125+ return res .map (outputType ::cast );
126126
127- return res ;
128127 }
129128
130129 /**
@@ -139,13 +138,10 @@ public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T>
139138 * @return an Optional containing, or not, the response object to the Action call
140139 * @since 0.0.1
141140 */
142- public <T extends GeneratedMessageV3 > Optional <T > invoke (String action , Class <T > outputType , InvocationOpts opts ) throws Exception {
141+ public <T extends GeneratedMessageV3 > Optional <T > invoke (String action , Class <T > outputType , InvocationOpts opts ) throws ActorInvocationException {
143142 Optional <T > res = invokeActor (action , Empty .getDefaultInstance (), outputType , Optional .ofNullable (opts ));
144- if (res .isPresent ()) {
145- return Optional .of (outputType .cast (res .get ()));
146- }
143+ return res .map (outputType ::cast );
147144
148- return res ;
149145 }
150146
151147 /**
@@ -159,13 +155,10 @@ public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T>
159155 * @return an Optional containing, or not, the response object to the Action call
160156 * @since 0.0.1
161157 */
162- public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > Optional <T > invoke (String action , S value , Class <T > outputType ) throws Exception {
158+ public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > Optional <T > invoke (String action , S value , Class <T > outputType ) throws ActorInvocationException {
163159 Optional <T > res = invokeActor (action , value , outputType , Optional .empty ());
164- if (res .isPresent ()) {
165- return Optional .of (outputType .cast (res .get ()));
166- }
160+ return res .map (outputType ::cast );
167161
168- return res ;
169162 }
170163
171164 /**
@@ -181,13 +174,10 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
181174 * @return an Optional containing, or not, the response object to the Action call
182175 * @since 0.0.1
183176 */
184- public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > Optional <T > invoke (String action , S value , Class <T > outputType , InvocationOpts opts ) throws Exception {
177+ public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > Optional <T > invoke (String action , S value , Class <T > outputType , InvocationOpts opts ) throws ActorInvocationException {
185178 Optional <T > res = invokeActor (action , value , outputType , Optional .ofNullable (opts ));
186- if (res .isPresent ()) {
187- return Optional .of (outputType .cast (res .get ()));
188- }
179+ return res .map (outputType ::cast );
189180
190- return res ;
191181 }
192182
193183 /**
@@ -198,7 +188,7 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
198188 * @param action name of the action to be called.
199189 * @since 0.0.1
200190 */
201- public <T extends GeneratedMessageV3 > void invokeAsync (String action ) throws Exception {
191+ public <T extends GeneratedMessageV3 > void invokeAsync (String action ) throws ActorInvocationException {
202192 InvocationOpts opts = InvocationOpts .builder ().async (true ).build ();
203193 invokeActor (action , Empty .getDefaultInstance (), null , Optional .of (opts ));
204194 }
@@ -213,10 +203,10 @@ public <T extends GeneratedMessageV3> void invokeAsync(String action) throws Exc
213203 * Please see the {@link io.eigr.spawn.api.InvocationOpts} class for more information
214204 * @since 0.0.1
215205 */
216- public <T extends GeneratedMessageV3 > void invokeAsync (String action , InvocationOpts opts ) throws Exception {
206+ public <T extends GeneratedMessageV3 > void invokeAsync (String action , InvocationOpts opts ) throws ActorInvocationException {
217207 InvocationOpts mergedOpts = InvocationOpts .builder ()
218208 .async (true )
219- .delay (opts .getDelay ())
209+ .delaySeconds (opts .getDelaySeconds ())
220210 .scheduledTo (opts .getScheduledTo ())
221211 .timeoutSeconds (opts .getTimeoutSeconds ())
222212 .build ();
@@ -233,7 +223,7 @@ public <T extends GeneratedMessageV3> void invokeAsync(String action, Invocation
233223 * @param value the action argument object.
234224 * @since 0.0.1
235225 */
236- public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > void invokeAsync (String action , S value ) throws Exception {
226+ public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > void invokeAsync (String action , S value ) throws ActorInvocationException {
237227 InvocationOpts opts = InvocationOpts .builder ().async (true ).build ();
238228 invokeActor (action , value , null , Optional .of (opts ));
239229 }
@@ -249,10 +239,10 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeA
249239 * Please see the {@link io.eigr.spawn.api.InvocationOpts} class for more information
250240 * @since 0.0.1
251241 */
252- public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > void invokeAsync (String action , S value , InvocationOpts opts ) throws Exception {
242+ public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > void invokeAsync (String action , S value , InvocationOpts opts ) throws ActorInvocationException {
253243 InvocationOpts mergedOpts = InvocationOpts .builder ()
254244 .async (true )
255- .delay (opts .getDelay ())
245+ .delaySeconds (opts .getDelaySeconds ())
256246 .scheduledTo (opts .getScheduledTo ())
257247 .timeoutSeconds (opts .getTimeoutSeconds ())
258248 .build ();
@@ -285,33 +275,27 @@ public boolean isUnNamedActor() {
285275 }
286276
287277 private <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > Optional <T > invokeActor (
288- String cmd , S argument , Class <T > outputType , Optional <InvocationOpts > options ) throws Exception {
278+ String cmd , S argument , Class <T > outputType , Optional <InvocationOpts > options ) throws ActorInvocationException {
289279 Objects .requireNonNull (this .actorId , "ActorId cannot be null" );
290280
291281 Protocol .InvocationRequest .Builder invocationRequestBuilder = Protocol .InvocationRequest .newBuilder ();
292282
293283 Map <String , String > metadata = new HashMap <>();
294- if (options .isPresent ()) {
295- InvocationOpts opts = options .get ();
284+ options .ifPresent (opts -> {
296285 invocationRequestBuilder .setAsync (opts .isAsync ());
297-
298- if (opts .getDelay ().isPresent () && !opts .getScheduledTo ().isPresent ()) {
299- invocationRequestBuilder .setScheduledTo (opts .getDelay ().get ());
300- } else if (opts .getScheduledTo ().isPresent ()) {
301- invocationRequestBuilder .setScheduledTo (opts .getScheduleTimeInLong ());
302- }
303-
304286 metadata .put ("request-timeout" , String .valueOf (opts .getTimeout ()));
305- }
287+ opts .getDelaySeconds ().ifPresent (invocationRequestBuilder ::setScheduledTo );
288+ // 'scheduledTo' override 'delay' if both is set.
289+ opts .getScheduledTo ()
290+ .ifPresent (scheduleTo -> invocationRequestBuilder .setScheduledTo (opts .getScheduleTimeInLong ()));
291+ });
306292
307293 final ActorOuterClass .Actor actorRef = ActorOuterClass .Actor .newBuilder ()
308294 .setId (this .actorId )
309295 .build ();
310296
311297 Any commandArg = Any .pack (argument );
312298
313-
314-
315299 invocationRequestBuilder
316300 .setSystem (ActorOuterClass .ActorSystem .newBuilder ().setName (this .actorId .getSystem ()).build ())
317301 .setActor (actorRef )
@@ -328,13 +312,16 @@ private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
328312 case UNRECOGNIZED :
329313 String msg = String .format ("Error when trying to invoke Actor %s. Details: %s" ,
330314 this .getActorName (), status .getMessage ());
331-
332- throw new ActorInvokeException (msg );
315+ throw new ActorInvocationException (msg );
333316 case ACTOR_NOT_FOUND :
334- throw new ActorNotFoundException ();
317+ throw new ActorNotFoundException ("Actor not found." );
335318 case OK :
336319 if (resp .hasValue () && Objects .nonNull (outputType )) {
337- return Optional .of (resp .getValue ().unpack (outputType ));
320+ try {
321+ return Optional .of (resp .getValue ().unpack (outputType ));
322+ } catch (Exception e ) {
323+ throw new ActorInvocationException ("Error handling response." , e );
324+ }
338325 }
339326 return Optional .empty ();
340327 }
0 commit comments