99import io .eigr .functions .protocol .actors .ActorOuterClass ;
1010import io .eigr .spawn .api .exceptions .ActorInvokeException ;
1111import io .eigr .spawn .api .exceptions .ActorNotFoundException ;
12+ import io .eigr .spawn .api .exceptions .SpawnException ;
1213import io .eigr .spawn .internal .transport .client .SpawnClient ;
1314
1415import java .time .Duration ;
@@ -47,7 +48,7 @@ private ActorRef(ActorOuterClass.ActorId actorId, SpawnClient client) {
4748 * @return the ActorRef instance
4849 * @since 0.0.1
4950 */
50- protected static ActorRef of (SpawnClient client , String system , String name ) throws Exception {
51+ protected static ActorRef of (SpawnClient client , String system , String name ) throws SpawnException {
5152 ActorOuterClass .ActorId actorId = buildActorId (system , name );
5253 ActorRef ref = ACTOR_REF_CACHE .getIfPresent (actorId );
5354 if (Objects .nonNull (ref )) {
@@ -70,7 +71,7 @@ protected static ActorRef of(SpawnClient client, String system, String name) thr
7071 * @return the ActorRef instance
7172 * @since 0.0.1
7273 */
73- protected static ActorRef of (SpawnClient client , String system , String name , String parent ) throws Exception {
74+ protected static ActorRef of (SpawnClient client , String system , String name , String parent ) throws SpawnException {
7475 ActorOuterClass .ActorId actorId = buildActorId (system , name , parent );
7576 ActorRef ref = ACTOR_REF_CACHE .getIfPresent (actorId );
7677 if (Objects .nonNull (ref )) {
@@ -99,7 +100,7 @@ private static ActorOuterClass.ActorId buildActorId(String system, String name,
99100 .build ();
100101 }
101102
102- private static void spawnActor (ActorOuterClass .ActorId actorId , SpawnClient client ) throws Exception {
103+ private static void spawnActor (ActorOuterClass .ActorId actorId , SpawnClient client ) throws SpawnException {
103104 Protocol .SpawnRequest req = Protocol .SpawnRequest .newBuilder ()
104105 .addActors (actorId )
105106 .build ();
@@ -116,13 +117,10 @@ private static void spawnActor(ActorOuterClass.ActorId actorId, SpawnClient clie
116117 * @return an Optional containing, or not, the response object to the Action call
117118 * @since 0.0.1
118119 */
119- public <T extends GeneratedMessageV3 > Optional <T > invoke (String action , Class <T > outputType ) throws Exception {
120+ public <T extends GeneratedMessageV3 > Optional <T > invoke (String action , Class <T > outputType ) throws SpawnException {
120121 Optional <T > res = invokeActor (action , Empty .getDefaultInstance (), outputType , Optional .empty ());
121- if (res .isPresent ()) {
122- return Optional .of (outputType .cast (res .get ()));
123- }
122+ return res .map (outputType ::cast );
124123
125- return res ;
126124 }
127125
128126 /**
@@ -137,13 +135,10 @@ public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T>
137135 * @return an Optional containing, or not, the response object to the Action call
138136 * @since 0.0.1
139137 */
140- public <T extends GeneratedMessageV3 > Optional <T > invoke (String action , Class <T > outputType , InvocationOpts opts ) throws Exception {
138+ public <T extends GeneratedMessageV3 > Optional <T > invoke (String action , Class <T > outputType , InvocationOpts opts ) throws SpawnException {
141139 Optional <T > res = invokeActor (action , Empty .getDefaultInstance (), outputType , Optional .ofNullable (opts ));
142- if (res .isPresent ()) {
143- return Optional .of (outputType .cast (res .get ()));
144- }
140+ return res .map (outputType ::cast );
145141
146- return res ;
147142 }
148143
149144 /**
@@ -157,13 +152,10 @@ public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T>
157152 * @return an Optional containing, or not, the response object to the Action call
158153 * @since 0.0.1
159154 */
160- public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > Optional <T > invoke (String action , S value , Class <T > outputType ) throws Exception {
155+ public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > Optional <T > invoke (String action , S value , Class <T > outputType ) throws SpawnException {
161156 Optional <T > res = invokeActor (action , value , outputType , Optional .empty ());
162- if (res .isPresent ()) {
163- return Optional .of (outputType .cast (res .get ()));
164- }
157+ return res .map (outputType ::cast );
165158
166- return res ;
167159 }
168160
169161 /**
@@ -179,13 +171,10 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
179171 * @return an Optional containing, or not, the response object to the Action call
180172 * @since 0.0.1
181173 */
182- public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > Optional <T > invoke (String action , S value , Class <T > outputType , InvocationOpts opts ) throws Exception {
174+ public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > Optional <T > invoke (String action , S value , Class <T > outputType , InvocationOpts opts ) throws SpawnException {
183175 Optional <T > res = invokeActor (action , value , outputType , Optional .ofNullable (opts ));
184- if (res .isPresent ()) {
185- return Optional .of (outputType .cast (res .get ()));
186- }
176+ return res .map (outputType ::cast );
187177
188- return res ;
189178 }
190179
191180 /**
@@ -196,7 +185,7 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
196185 * @param action name of the action to be called.
197186 * @since 0.0.1
198187 */
199- public <T extends GeneratedMessageV3 > void invokeAsync (String action ) throws Exception {
188+ public <T extends GeneratedMessageV3 > void invokeAsync (String action ) throws SpawnException {
200189 InvocationOpts opts = InvocationOpts .builder ().async (true ).build ();
201190 invokeActor (action , Empty .getDefaultInstance (), null , Optional .of (opts ));
202191 }
@@ -211,7 +200,7 @@ public <T extends GeneratedMessageV3> void invokeAsync(String action) throws Exc
211200 * Please see the {@link io.eigr.spawn.api.InvocationOpts} class for more information
212201 * @since 0.0.1
213202 */
214- public <T extends GeneratedMessageV3 > void invokeAsync (String action , InvocationOpts opts ) throws Exception {
203+ public <T extends GeneratedMessageV3 > void invokeAsync (String action , InvocationOpts opts ) throws SpawnException {
215204 InvocationOpts mergedOpts = InvocationOpts .builder ()
216205 .async (true )
217206 .delay (opts .getDelay ())
@@ -230,7 +219,7 @@ public <T extends GeneratedMessageV3> void invokeAsync(String action, Invocation
230219 * @param value the action argument object.
231220 * @since 0.0.1
232221 */
233- public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > void invokeAsync (String action , S value ) throws Exception {
222+ public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > void invokeAsync (String action , S value ) throws SpawnException {
234223 InvocationOpts opts = InvocationOpts .builder ().async (true ).build ();
235224 invokeActor (action , value , null , Optional .of (opts ));
236225 }
@@ -246,7 +235,7 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeA
246235 * Please see the {@link io.eigr.spawn.api.InvocationOpts} class for more information
247236 * @since 0.0.1
248237 */
249- public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > void invokeAsync (String action , S value , InvocationOpts opts ) throws Exception {
238+ public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > void invokeAsync (String action , S value , InvocationOpts opts ) throws SpawnException {
250239 InvocationOpts mergedOpts = InvocationOpts .builder ()
251240 .async (true )
252241 .delay (opts .getDelay ())
@@ -281,21 +270,18 @@ public boolean isUnNamedActor() {
281270 }
282271
283272 private <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > Optional <T > invokeActor (
284- String cmd , S argument , Class <T > outputType , Optional <InvocationOpts > options ) throws Exception {
273+ String cmd , S argument , Class <T > outputType , Optional <InvocationOpts > options ) throws SpawnException {
285274 Objects .requireNonNull (this .actorId , "ActorId cannot be null" );
286275
287276 Protocol .InvocationRequest .Builder invocationRequestBuilder = Protocol .InvocationRequest .newBuilder ();
288277
289- if (options .isPresent ()) {
290- InvocationOpts opts = options .get ();
278+ options .ifPresent (opts -> {
291279 invocationRequestBuilder .setAsync (opts .isAsync ());
292-
293- if (opts .getDelay ().isPresent () && !opts .getScheduledTo ().isPresent ()) {
294- invocationRequestBuilder .setScheduledTo (opts .getDelay ().get ());
295- } else if (opts .getScheduledTo ().isPresent ()) {
296- invocationRequestBuilder .setScheduledTo (opts .getScheduleTimeInLong ());
297- }
298- }
280+ opts .getDelay ().ifPresent (invocationRequestBuilder ::setScheduledTo );
281+ // 'scheduledTo' override 'delay' if both is set.
282+ opts .getScheduledTo ()
283+ .ifPresent (scheduleTo -> invocationRequestBuilder .setScheduledTo (opts .getScheduleTimeInLong ()));
284+ });
299285
300286 final ActorOuterClass .Actor actorRef = ActorOuterClass .Actor .newBuilder ()
301287 .setId (this .actorId )
@@ -318,13 +304,16 @@ private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
318304 case UNRECOGNIZED :
319305 String msg = String .format ("Error when trying to invoke Actor %s. Details: %s" ,
320306 this .getActorName (), status .getMessage ());
321-
322307 throw new ActorInvokeException (msg );
323308 case ACTOR_NOT_FOUND :
324309 throw new ActorNotFoundException ();
325310 case OK :
326311 if (resp .hasValue () && Objects .nonNull (outputType )) {
327- return Optional .of (resp .getValue ().unpack (outputType ));
312+ try {
313+ return Optional .of (resp .getValue ().unpack (outputType ));
314+ } catch (Exception e ) {
315+ throw new SpawnException (e );
316+ }
328317 }
329318 return Optional .empty ();
330319 }
0 commit comments