1- package io .eigr .spawn .api . actors ;
1+ package io .eigr .spawn .api ;
22
33import com .github .benmanes .caffeine .cache .Cache ;
44import com .github .benmanes .caffeine .cache .Caffeine ;
77import com .google .protobuf .GeneratedMessageV3 ;
88import io .eigr .functions .protocol .Protocol ;
99import io .eigr .functions .protocol .actors .ActorOuterClass ;
10- import io .eigr .spawn .api .InvocationOpts ;
1110import io .eigr .spawn .api .exceptions .ActorInvokeException ;
1211import io .eigr .spawn .api .exceptions .ActorNotFoundException ;
1312import io .eigr .spawn .internal .transport .client .SpawnClient ;
1615import java .util .Objects ;
1716import java .util .Optional ;
1817
18+ /**
19+ * ActorRef is responsible for representing an instance of an Actor
20+ *
21+ * @author Adriano Santos
22+ *
23+ */
1924public final class ActorRef {
2025 private static final int CACHE_MAXIMUM_SIZE = 1_000 ;
2126 private static final int CACHE_EXPIRE_AFTER_WRITE_SECONDS = 60 ;
@@ -32,8 +37,17 @@ private ActorRef(ActorOuterClass.ActorId actorId, SpawnClient client) {
3237 this .client = client ;
3338 this .actorId = actorId ;
3439 }
35-
36- public static ActorRef of (SpawnClient client , String system , String name ) throws Exception {
40+
41+ /**
42+ * <p>This method is responsible for creating instances of the ActorRef class
43+ * </p>
44+ * @param client is the client part of the Spawn protocol and is responsible for communicating with the Proxy.
45+ * @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
47+ * @return the ActorRef instance
48+ * @since 0.0.1
49+ */
50+ protected static ActorRef of (SpawnClient client , String system , String name ) throws Exception {
3751 ActorOuterClass .ActorId actorId = buildActorId (system , name );
3852 ActorRef ref = ACTOR_REF_CACHE .getIfPresent (actorId );
3953 if (Objects .nonNull (ref )){
@@ -45,7 +59,17 @@ public static ActorRef of(SpawnClient client, String system, String name) throws
4559 return ref ;
4660 }
4761
48- public static ActorRef of (SpawnClient client , String system , String name , String parent ) throws Exception {
62+ /**
63+ * <p>This method is responsible for creating instances of the ActorRef class when Actor is a UnNamed actor.
64+ * </p>
65+ * @param client is the client part of the Spawn protocol and is responsible for communicating with the Proxy.
66+ * @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 parent the name of the unnamed parent actor
69+ * @return the ActorRef instance
70+ * @since 0.0.1
71+ */
72+ protected static ActorRef of (SpawnClient client , String system , String name , String parent ) throws Exception {
4973 ActorOuterClass .ActorId actorId = buildActorId (system , name , parent );
5074 ActorRef ref = ACTOR_REF_CACHE .getIfPresent (actorId );
5175 if (Objects .nonNull (ref )){
@@ -58,70 +82,146 @@ public static ActorRef of(SpawnClient client, String system, String name, String
5882 return ref ;
5983 }
6084
61- public <T extends GeneratedMessageV3 > Optional <Object > invoke (String cmd , Class <T > outputType ) throws Exception {
62- Optional <Object > res = invokeActor (cmd , Empty .getDefaultInstance (), outputType , Optional .empty ());
85+ /**
86+ * <p>This method synchronously invokes an action on the actor that this ActorRef instance represents through the Spawn Proxy.
87+ * Used when it is not necessary to send parameters to the Action.
88+ * </p>
89+ * @param action name of the action to be called.
90+ * @param outputType the class that corresponds to the expected return type
91+ * @return an Optional containing, or not, the response object to the Action call
92+ * @since 0.0.1
93+ */
94+ public <T extends GeneratedMessageV3 > Optional <Object > invoke (String action , Class <T > outputType ) throws Exception {
95+ Optional <Object > res = invokeActor (action , Empty .getDefaultInstance (), outputType , Optional .empty ());
6396 if (res .isPresent () ){
6497 return Optional .of (outputType .cast (res .get ()));
6598 }
6699
67100 return res ;
68101 }
69102
70- public <T extends GeneratedMessageV3 > Optional <Object > invoke (String cmd , Class <T > outputType , InvocationOpts opts ) throws Exception {
71- Optional <Object > res = invokeActor (cmd , Empty .getDefaultInstance (), outputType , Optional .ofNullable (opts ));
103+ /**
104+ * <p>This method synchronously invokes an action on the actor that this ActorRef instance represents through the Spawn Proxy.
105+ * Used when it is not necessary to send parameters to the Action.
106+ * </p>
107+ * @param action name of the action to be called.
108+ * @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
111+ * @return an Optional containing, or not, the response object to the Action call
112+ * @since 0.0.1
113+ */
114+ public <T extends GeneratedMessageV3 > Optional <Object > invoke (String action , Class <T > outputType , InvocationOpts opts ) throws Exception {
115+ Optional <Object > res = invokeActor (action , Empty .getDefaultInstance (), outputType , Optional .ofNullable (opts ));
72116 if (res .isPresent () ){
73117 return Optional .of (outputType .cast (res .get ()));
74118 }
75119
76120 return res ;
77121 }
78122
79- public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > Optional <Object > invoke (String cmd , S value , Class <T > outputType ) throws Exception {
80- Optional <Object > res = invokeActor (cmd , value , outputType , Optional .empty ());
123+ /**
124+ * <p>This method synchronously invokes an action on the actor that this ActorRef instance represents through the Spawn Proxy.
125+ * Used when it is not necessary to send parameters to the Action.
126+ * </p>
127+ * @param action name of the action to be called.
128+ * @param value the action argument object.
129+ * @param outputType the class that corresponds to the expected return type
130+ * @return an Optional containing, or not, the response object to the Action call
131+ * @since 0.0.1
132+ */
133+ public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > Optional <Object > invoke (String action , S value , Class <T > outputType ) throws Exception {
134+ Optional <Object > res = invokeActor (action , value , outputType , Optional .empty ());
81135 if (res .isPresent () ){
82136 return Optional .of (outputType .cast (res .get ()));
83137 }
84138
85139 return res ;
86140 }
87141
88- public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > Optional <Object > invoke (String cmd , S value , Class <T > outputType , InvocationOpts opts ) throws Exception {
89- Optional <Object > res = invokeActor (cmd , value , outputType , Optional .ofNullable (opts ));
142+ /**
143+ * <p>This method synchronously invokes an action on the actor that this ActorRef instance represents through the Spawn Proxy.
144+ * Used when it is not necessary to send parameters to the Action.
145+ * </p>
146+ * @param action name of the action to be called.
147+ * @param value the action argument object.
148+ * @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
151+ * @return an Optional containing, or not, the response object to the Action call
152+ * @since 0.0.1
153+ */
154+ public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > Optional <Object > invoke (String action , S value , Class <T > outputType , InvocationOpts opts ) throws Exception {
155+ Optional <Object > res = invokeActor (action , value , outputType , Optional .ofNullable (opts ));
90156 if (res .isPresent () ){
91157 return Optional .of (outputType .cast (res .get ()));
92158 }
93159
94160 return res ;
95161 }
96162
97- public <T extends GeneratedMessageV3 > void invokeAsync (String cmd , Class <T > outputType ) throws Exception {
163+ /**
164+ * <p>This method asynchronously invokes an action on the actor that this ActorRef instance represents via the Spawn Proxy.
165+ * Used when it is not necessary to send parameters to the Action.
166+ * </p>
167+ * @param action name of the action to be called.
168+ * @since 0.0.1
169+ */
170+ public <T extends GeneratedMessageV3 > void invokeAsync (String action ) throws Exception {
98171 InvocationOpts opts = InvocationOpts .builder ().async (true ).build ();
99- invokeActor (cmd , Empty .getDefaultInstance (), outputType , Optional .of (opts ));
172+ invokeActor (action , Empty .getDefaultInstance (), null , Optional .of (opts ));
100173 }
101174
102- public <T extends GeneratedMessageV3 > void invokeAsync (String cmd , Class <T > outputType , InvocationOpts opts ) throws Exception {
175+ /**
176+ * <p>This method asynchronously invokes an action on the actor that this ActorRef instance represents via the Spawn Proxy.
177+ * Used when it is not necessary to send parameters to the Action.
178+ * </p>
179+ * @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
182+ * @since 0.0.1
183+ */
184+ public <T extends GeneratedMessageV3 > void invokeAsync (String action , InvocationOpts opts ) throws Exception {
103185 InvocationOpts mergedOpts = InvocationOpts .builder ()
104186 .async (true )
105187 .delay (opts .getDelay ())
106188 .scheduledTo (opts .getScheduledTo ())
107189 .build ();
108190
109- invokeActor (cmd , Empty .getDefaultInstance (), outputType , Optional .ofNullable (mergedOpts ));
191+ invokeActor (action , Empty .getDefaultInstance (), null , Optional .ofNullable (mergedOpts ));
110192 }
111193
112- public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > void invokeAsync (String cmd , S value , Class <T > outputType ) throws Exception {
194+ /**
195+ * <p>This method asynchronously invokes an action on the actor that this ActorRef instance represents through the Spawn Proxy.
196+ * Used when it is not necessary to send parameters to the Action.
197+ * </p>
198+ * @param action name of the action to be called.
199+ * @param value the action argument object.
200+ * @since 0.0.1
201+ */
202+ public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > void invokeAsync (String action , S value ) throws Exception {
113203 InvocationOpts opts = InvocationOpts .builder ().async (true ).build ();
114- invokeActor (cmd , value , outputType , Optional .of (opts ));
204+ invokeActor (action , value , null , Optional .of (opts ));
115205 }
116206
117- public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > void invokeAsync (String cmd , S value , Class <T > outputType , InvocationOpts opts ) throws Exception {
207+ /**
208+ * <p>This method asynchronously invokes an action on the actor that this ActorRef instance represents through the Spawn Proxy.
209+ * Used when it is not necessary to send parameters to the Action.
210+ * </p>
211+ * @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
215+ * @since 0.0.1
216+ */
217+ public <T extends GeneratedMessageV3 , S extends GeneratedMessageV3 > void invokeAsync (String action , S value , InvocationOpts opts ) throws Exception {
118218 InvocationOpts mergedOpts = InvocationOpts .builder ()
119219 .async (true )
120220 .delay (opts .getDelay ())
121221 .scheduledTo (opts .getScheduledTo ())
122222 .build ();
123223
124- invokeActor (cmd , value , outputType , Optional .of (mergedOpts ));
224+ invokeActor (action , value , null , Optional .of (mergedOpts ));
125225 }
126226
127227 public String getActorSystem () {
@@ -187,7 +287,7 @@ private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<Ob
187287 case ACTOR_NOT_FOUND :
188288 throw new ActorNotFoundException ();
189289 case OK :
190- if (resp .hasValue ()) {
290+ if (resp .hasValue () && Objects . nonNull ( outputType ) ) {
191291 return Optional .of (resp .getValue ().unpack (outputType ));
192292 }
193293 return Optional .empty ();
0 commit comments