5
5
import io .eigr .functions .protocol .actors .ActorOuterClass ;
6
6
import io .eigr .spawn .api .actors .ActorFactory ;
7
7
import io .eigr .spawn .api .actors .ActorRef ;
8
- import io .eigr .spawn .api .actors .annotations .NamedActor ;
9
- import io .eigr .spawn .api .actors .annotations .PooledActor ;
10
- import io .eigr .spawn .api .actors .annotations .UnNamedActor ;
8
+ import io .eigr .spawn .api .actors .annotations .stateful .StatefulNamedActor ;
9
+ import io .eigr .spawn .api .actors .annotations .stateful .StatefulPooledActor ;
10
+ import io .eigr .spawn .api .actors .annotations .stateful .StatefulUnNamedActor ;
11
+ import io .eigr .spawn .api .actors .annotations .stateless .StatelessNamedActor ;
12
+ import io .eigr .spawn .api .actors .annotations .stateless .StatelessPooledActor ;
13
+ import io .eigr .spawn .api .actors .annotations .stateless .StatelessUnNamedActor ;
11
14
import io .eigr .spawn .internal .Entity ;
12
15
import io .eigr .spawn .internal .client .OkHttpSpawnClient ;
13
16
import io .eigr .spawn .internal .client .SpawnClient ;
@@ -33,14 +36,11 @@ public final class Spawn {
33
36
private final SpawnClient client ;
34
37
35
38
private final int port ;
36
-
37
- private String host ;
38
-
39
39
private final String proxyHost ;
40
40
private final int proxyPort ;
41
41
private final String system ;
42
42
private final List <Entity > entities ;
43
-
43
+ private String host ;
44
44
private Optional <Executor > optionalExecutor ;
45
45
46
46
@@ -124,14 +124,20 @@ private void registerActorSystem() throws Exception {
124
124
125
125
private Map <String , ActorOuterClass .Actor > getActors (List <Entity > entities ) {
126
126
return entities .stream ().map (actorEntity -> {
127
- ActorOuterClass .ActorSnapshotStrategy snapshotStrategy =
128
- ActorOuterClass .ActorSnapshotStrategy .newBuilder ()
129
- .setTimeout (
130
- ActorOuterClass .TimeoutStrategy .newBuilder ()
131
- .setTimeout (actorEntity .getSnapshotTimeout ())
132
- .build ()
133
- )
134
- .build ();
127
+ ActorOuterClass .ActorSnapshotStrategy snapshotStrategy ;
128
+ if (actorEntity .isStateful ()) {
129
+ snapshotStrategy =
130
+ ActorOuterClass .ActorSnapshotStrategy .newBuilder ()
131
+ .setTimeout (
132
+ ActorOuterClass .TimeoutStrategy .newBuilder ()
133
+ .setTimeout (actorEntity .getSnapshotTimeout ())
134
+ .build ()
135
+ )
136
+ .build ();
137
+ } else {
138
+ snapshotStrategy = ActorOuterClass .ActorSnapshotStrategy .newBuilder ().build ();
139
+ }
140
+
135
141
136
142
ActorOuterClass .ActorDeactivationStrategy deactivateStrategy =
137
143
ActorOuterClass .ActorDeactivationStrategy .newBuilder ()
@@ -220,8 +226,8 @@ public String toString() {
220
226
221
227
public static final class SpawnSystem {
222
228
223
- private SpawnClient client ;
224
229
private final List <Entity > entities = new ArrayList <>();
230
+ private SpawnClient client ;
225
231
private int port = 8091 ;
226
232
private String host = "127.0.0.1" ;
227
233
private String proxyHost = "127.0.0.1" ;
@@ -282,38 +288,68 @@ public Spawn build() {
282
288
}
283
289
284
290
private Optional <Entity > getEntity (Class <?> actorKlass ) {
285
- if (Objects .nonNull (actorKlass .getAnnotation (NamedActor .class ))) {
291
+ Optional <Entity > maybeEntity = getStatefulEntity (actorKlass , null , null );
292
+
293
+ if (maybeEntity .isPresent ()) {
294
+ return maybeEntity ;
295
+ }
296
+
297
+ maybeEntity = getStatelessEntity (actorKlass , null , null );
298
+ if (maybeEntity .isPresent ()) {
299
+ return maybeEntity ;
300
+ }
301
+
302
+ return Optional .empty ();
303
+ }
304
+
305
+ private Optional <Entity > getEntity (Class <?> actorKlass , Object arg , ActorFactory factory ) {
306
+ Optional <Entity > maybeEntity = getStatefulEntity (actorKlass , arg , factory );
307
+
308
+ if (maybeEntity .isPresent ()) {
309
+ return maybeEntity ;
310
+ }
311
+
312
+ maybeEntity = getStatelessEntity (actorKlass , arg , factory );
313
+ if (maybeEntity .isPresent ()) {
314
+ return maybeEntity ;
315
+ }
316
+
317
+ return Optional .empty ();
318
+ }
319
+
320
+ private Optional <Entity > getStatefulEntity (Class <?> actorKlass , Object arg , ActorFactory factory ) {
321
+ if (Objects .nonNull (actorKlass .getAnnotation (StatefulNamedActor .class ))) {
286
322
return Optional .of (Entity .fromAnnotationToEntity (
287
- actorKlass , actorKlass .getAnnotation (NamedActor .class ), null , null ));
323
+ actorKlass , actorKlass .getAnnotation (StatefulNamedActor .class ), arg , factory ));
288
324
}
289
325
290
- if (Objects .nonNull (actorKlass .getAnnotation (UnNamedActor .class ))) {
326
+ if (Objects .nonNull (actorKlass .getAnnotation (StatefulUnNamedActor .class ))) {
291
327
return Optional .of (Entity .fromAnnotationToEntity (
292
- actorKlass , actorKlass .getAnnotation (UnNamedActor .class ), null , null ));
328
+ actorKlass , actorKlass .getAnnotation (StatefulUnNamedActor .class ), arg , factory ));
293
329
}
294
330
295
- if (Objects .nonNull (actorKlass .getAnnotation (PooledActor .class ))) {
331
+ if (Objects .nonNull (actorKlass .getAnnotation (StatefulPooledActor .class ))) {
296
332
return Optional .of (Entity .fromAnnotationToEntity (
297
- actorKlass , actorKlass .getAnnotation (PooledActor .class ), null , null ));
333
+ actorKlass , actorKlass .getAnnotation (StatefulPooledActor .class ), arg , factory ));
298
334
}
299
335
300
336
return Optional .empty ();
301
337
}
302
338
303
- private Optional <Entity > getEntity (Class <?> actorType , Object arg , ActorFactory factory ) {
304
- if (Objects .nonNull (actorType .getAnnotation (NamedActor .class ))) {
305
- NamedActor annotation = actorType . getAnnotation ( NamedActor . class );
306
- return Optional . of ( Entity . fromAnnotationToEntity ( actorType , annotation , arg , factory ));
339
+ private Optional <Entity > getStatelessEntity (Class <?> actorKlass , Object arg , ActorFactory factory ) {
340
+ if (Objects .nonNull (actorKlass .getAnnotation (StatelessNamedActor .class ))) {
341
+ return Optional . of ( Entity . fromAnnotationToEntity (
342
+ actorKlass , actorKlass . getAnnotation ( StatelessNamedActor . class ) , arg , factory ));
307
343
}
308
344
309
- if (Objects .nonNull (actorType .getAnnotation (UnNamedActor .class ))) {
310
- UnNamedActor annotation = actorType . getAnnotation ( UnNamedActor . class );
311
- return Optional . of ( Entity . fromAnnotationToEntity ( actorType , annotation , arg , factory ));
345
+ if (Objects .nonNull (actorKlass .getAnnotation (StatelessUnNamedActor .class ))) {
346
+ return Optional . of ( Entity . fromAnnotationToEntity (
347
+ actorKlass , actorKlass . getAnnotation ( StatelessUnNamedActor . class ) , arg , factory ));
312
348
}
313
349
314
- if (Objects .nonNull (actorType .getAnnotation (PooledActor .class ))) {
315
- PooledActor annotation = actorType . getAnnotation ( PooledActor . class );
316
- return Optional . of ( Entity . fromAnnotationToEntity ( actorType , annotation , arg , factory ));
350
+ if (Objects .nonNull (actorKlass .getAnnotation (StatelessPooledActor .class ))) {
351
+ return Optional . of ( Entity . fromAnnotationToEntity (
352
+ actorKlass , actorKlass . getAnnotation ( StatelessPooledActor . class ) , arg , factory ));
317
353
}
318
354
319
355
return Optional .empty ();
0 commit comments