1
- package io .eigr .spawn .api . actors ;
1
+ package io .eigr .spawn .api ;
2
2
3
3
import com .github .benmanes .caffeine .cache .Cache ;
4
4
import com .github .benmanes .caffeine .cache .Caffeine ;
7
7
import com .google .protobuf .GeneratedMessageV3 ;
8
8
import io .eigr .functions .protocol .Protocol ;
9
9
import io .eigr .functions .protocol .actors .ActorOuterClass ;
10
- import io .eigr .spawn .api .InvocationOpts ;
11
10
import io .eigr .spawn .api .exceptions .ActorInvokeException ;
12
11
import io .eigr .spawn .api .exceptions .ActorNotFoundException ;
13
12
import io .eigr .spawn .internal .transport .client .SpawnClient ;
16
15
import java .util .Objects ;
17
16
import java .util .Optional ;
18
17
18
+ /**
19
+ * ActorRef is responsible for representing an instance of an Actor
20
+ *
21
+ * @author Adriano Santos
22
+ *
23
+ */
19
24
public final class ActorRef {
20
25
private static final int CACHE_MAXIMUM_SIZE = 1_000 ;
21
26
private static final int CACHE_EXPIRE_AFTER_WRITE_SECONDS = 60 ;
@@ -32,8 +37,17 @@ private ActorRef(ActorOuterClass.ActorId actorId, SpawnClient client) {
32
37
this .client = client ;
33
38
this .actorId = actorId ;
34
39
}
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 {
37
51
ActorOuterClass .ActorId actorId = buildActorId (system , name );
38
52
ActorRef ref = ACTOR_REF_CACHE .getIfPresent (actorId );
39
53
if (Objects .nonNull (ref )){
@@ -45,7 +59,17 @@ public static ActorRef of(SpawnClient client, String system, String name) throws
45
59
return ref ;
46
60
}
47
61
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 {
49
73
ActorOuterClass .ActorId actorId = buildActorId (system , name , parent );
50
74
ActorRef ref = ACTOR_REF_CACHE .getIfPresent (actorId );
51
75
if (Objects .nonNull (ref )){
@@ -58,70 +82,146 @@ public static ActorRef of(SpawnClient client, String system, String name, String
58
82
return ref ;
59
83
}
60
84
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 ());
63
96
if (res .isPresent () ){
64
97
return Optional .of (outputType .cast (res .get ()));
65
98
}
66
99
67
100
return res ;
68
101
}
69
102
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 ));
72
116
if (res .isPresent () ){
73
117
return Optional .of (outputType .cast (res .get ()));
74
118
}
75
119
76
120
return res ;
77
121
}
78
122
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 ());
81
135
if (res .isPresent () ){
82
136
return Optional .of (outputType .cast (res .get ()));
83
137
}
84
138
85
139
return res ;
86
140
}
87
141
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 ));
90
156
if (res .isPresent () ){
91
157
return Optional .of (outputType .cast (res .get ()));
92
158
}
93
159
94
160
return res ;
95
161
}
96
162
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 {
98
171
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 ));
100
173
}
101
174
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 {
103
185
InvocationOpts mergedOpts = InvocationOpts .builder ()
104
186
.async (true )
105
187
.delay (opts .getDelay ())
106
188
.scheduledTo (opts .getScheduledTo ())
107
189
.build ();
108
190
109
- invokeActor (cmd , Empty .getDefaultInstance (), outputType , Optional .ofNullable (mergedOpts ));
191
+ invokeActor (action , Empty .getDefaultInstance (), null , Optional .ofNullable (mergedOpts ));
110
192
}
111
193
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 {
113
203
InvocationOpts opts = InvocationOpts .builder ().async (true ).build ();
114
- invokeActor (cmd , value , outputType , Optional .of (opts ));
204
+ invokeActor (action , value , null , Optional .of (opts ));
115
205
}
116
206
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 {
118
218
InvocationOpts mergedOpts = InvocationOpts .builder ()
119
219
.async (true )
120
220
.delay (opts .getDelay ())
121
221
.scheduledTo (opts .getScheduledTo ())
122
222
.build ();
123
223
124
- invokeActor (cmd , value , outputType , Optional .of (mergedOpts ));
224
+ invokeActor (action , value , null , Optional .of (mergedOpts ));
125
225
}
126
226
127
227
public String getActorSystem () {
@@ -187,7 +287,7 @@ private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<Ob
187
287
case ACTOR_NOT_FOUND :
188
288
throw new ActorNotFoundException ();
189
289
case OK :
190
- if (resp .hasValue ()) {
290
+ if (resp .hasValue () && Objects . nonNull ( outputType ) ) {
191
291
return Optional .of (resp .getValue ().unpack (outputType ));
192
292
}
193
293
return Optional .empty ();
0 commit comments