@@ -49,9 +49,9 @@ public final class Spawn {
4949 private final int proxyPort ;
5050 private final String system ;
5151 private final List <Entity > entities ;
52- private String host ;
53- private Executor executor ;
54- private int terminationGracePeriodSeconds ;
52+ private final String host ;
53+ private final Executor executor ;
54+ private final int terminationGracePeriodSeconds ;
5555
5656 private Spawn (SpawnSystem builder ) {
5757 this .system = builder .system ;
@@ -123,11 +123,7 @@ public Stream<ActorRef> createMultiActorRefs(List<ActorIdentity> identities) thr
123123
124124 return identities .stream ().map (identity -> {
125125 try {
126- if (identity .isParent ()) {
127- return ActorRef .of (this .client , this .actorIdCache , identity , false );
128- }
129-
130- return ActorRef .of (this .client , this .actorIdCache , identity );
126+ return identity .isParent () ? ActorRef .of (this .client , this .actorIdCache , identity , false ) : ActorRef .of (this .client , this .actorIdCache , identity );
131127 } catch (ActorCreationException e ) {
132128 throw new SpawnFailureException (e );
133129 }
@@ -152,38 +148,23 @@ private void startServer() throws SpawnException {
152148 httpServer .setExecutor (this .executor );
153149 httpServer .start ();
154150
155- Runtime .getRuntime ()
156- .addShutdownHook (new Thread (() -> {
157- log .info ("Stopping Spawn HTTP Server with termination grace period %s ..." , this .terminationGracePeriodSeconds );
158- httpServer .stop (this .terminationGracePeriodSeconds );
159- }));
151+ Runtime .getRuntime ().addShutdownHook (new Thread (() -> {
152+ log .info ("Stopping Spawn HTTP Server with termination grace period {} ..." , this .terminationGracePeriodSeconds );
153+ httpServer .stop (this .terminationGracePeriodSeconds );
154+ }));
160155 } catch (IOException ex ) {
161156 throw new SpawnException (ex );
162157 }
163158 }
164159
165160 private void registerActorSystem () throws ActorRegistrationException {
166- ActorOuterClass .Registry registry = ActorOuterClass .Registry .newBuilder ()
167- .putAllActors (getActors (this .entities ))
168- .build ();
169-
170- ActorOuterClass .ActorSystem actorSystem = ActorOuterClass .ActorSystem .newBuilder ()
171- .setName (this .system )
172- .setRegistry (registry )
173- .build ();
174-
175- Protocol .ServiceInfo si = Protocol .ServiceInfo .newBuilder ()
176- .setServiceName ("jvm-std-sdk" )
177- .setServiceVersion ("0.5.0" )
178- .setServiceRuntime (System .getProperty ("java.version" ))
179- .setProtocolMajorVersion (1 )
180- .setProtocolMinorVersion (1 )
181- .build ();
182-
183- Protocol .RegistrationRequest req = Protocol .RegistrationRequest .newBuilder ()
184- .setServiceInfo (si )
185- .setActorSystem (actorSystem )
186- .build ();
161+ ActorOuterClass .Registry registry = ActorOuterClass .Registry .newBuilder ().putAllActors (getActors (this .entities )).build ();
162+
163+ ActorOuterClass .ActorSystem actorSystem = ActorOuterClass .ActorSystem .newBuilder ().setName (this .system ).setRegistry (registry ).build ();
164+
165+ Protocol .ServiceInfo si = Protocol .ServiceInfo .newBuilder ().setServiceName ("jvm-std-sdk" ).setServiceVersion ("0.5.0" ).setServiceRuntime (System .getProperty ("java.version" )).setProtocolMajorVersion (1 ).setProtocolMinorVersion (1 ).build ();
166+
167+ Protocol .RegistrationRequest req = Protocol .RegistrationRequest .newBuilder ().setServiceInfo (si ).setActorSystem (actorSystem ).build ();
187168
188169 log .debug ("Registering Actors on Proxy. Registry: {}" , req );
189170 this .client .register (req );
@@ -193,102 +174,38 @@ private Map<String, ActorOuterClass.Actor> getActors(List<Entity> entities) {
193174 return entities .stream ().map (actorEntity -> {
194175 ActorOuterClass .ActorSnapshotStrategy snapshotStrategy ;
195176 if (actorEntity .isStateful ()) {
196- snapshotStrategy =
197- ActorOuterClass .ActorSnapshotStrategy .newBuilder ()
198- .setTimeout (
199- ActorOuterClass .TimeoutStrategy .newBuilder ()
200- .setTimeout (actorEntity .getSnapshotTimeout ())
201- .build ()
202- )
203- .build ();
177+ snapshotStrategy = ActorOuterClass .ActorSnapshotStrategy .newBuilder ().setTimeout (ActorOuterClass .TimeoutStrategy .newBuilder ().setTimeout (actorEntity .getSnapshotTimeout ()).build ()).build ();
204178 } else {
205179 snapshotStrategy = ActorOuterClass .ActorSnapshotStrategy .newBuilder ().build ();
206180 }
207181
208182
209- ActorOuterClass .ActorDeactivationStrategy deactivateStrategy =
210- ActorOuterClass .ActorDeactivationStrategy .newBuilder ()
211- .setTimeout (
212- ActorOuterClass .TimeoutStrategy .newBuilder ()
213- .setTimeout (actorEntity .getDeactivateTimeout ())
214- .build ()
215- )
216- .build ();
217-
218- ActorOuterClass .ActorSettings settings = ActorOuterClass .ActorSettings .newBuilder ()
219- .setKind (actorEntity .getKind ())
220- .setStateful (actorEntity .isStateful ())
221- .setSnapshotStrategy (snapshotStrategy )
222- .setDeactivationStrategy (deactivateStrategy )
223- .setMinPoolSize (actorEntity .getMinPoolSize ())
224- .setMaxPoolSize (actorEntity .getMaxPoolSize ())
225- .build ();
183+ ActorOuterClass .ActorDeactivationStrategy deactivateStrategy = ActorOuterClass .ActorDeactivationStrategy .newBuilder ().setTimeout (ActorOuterClass .TimeoutStrategy .newBuilder ().setTimeout (actorEntity .getDeactivateTimeout ()).build ()).build ();
184+
185+ ActorOuterClass .ActorSettings settings = ActorOuterClass .ActorSettings .newBuilder ().setKind (actorEntity .getKind ()).setStateful (actorEntity .isStateful ()).setSnapshotStrategy (snapshotStrategy ).setDeactivationStrategy (deactivateStrategy ).setMinPoolSize (actorEntity .getMinPoolSize ()).setMaxPoolSize (actorEntity .getMaxPoolSize ()).build ();
226186
227187 Map <String , String > tags = new HashMap <>();
228- ActorOuterClass .Metadata metadata = ActorOuterClass .Metadata .newBuilder ()
229- .setChannelGroup (actorEntity .getChannel ())
230- .putAllTags (tags )
231- .build ();
232-
233- return ActorOuterClass .Actor .newBuilder ()
234- .setId (
235- ActorOuterClass .ActorId .newBuilder ()
236- .setName (actorEntity .getActorName ())
237- .setSystem (this .system )
238- .build ()
239- )
240- .setMetadata (metadata )
241- .setSettings (settings )
242- .addAllActions (getActions (actorEntity ))
243- .addAllTimerActions (getTimerActions (actorEntity ))
244- .setState (ActorOuterClass .ActorState .newBuilder ().build ())
245- .build ();
188+ ActorOuterClass .Metadata metadata = ActorOuterClass .Metadata .newBuilder ().setChannelGroup (actorEntity .getChannel ()).putAllTags (tags ).build ();
189+
190+ return ActorOuterClass .Actor .newBuilder ().setId (ActorOuterClass .ActorId .newBuilder ().setName (actorEntity .getActorName ()).setSystem (this .system ).build ()).setMetadata (metadata ).setSettings (settings ).addAllActions (getActions (actorEntity )).addAllTimerActions (getTimerActions (actorEntity )).setState (ActorOuterClass .ActorState .newBuilder ().build ()).build ();
246191
247192 }).collect (Collectors .toMap (actor -> actor .getId ().getName (), Function .identity ()));
248193 }
249194
250195 private List <ActorOuterClass .Action > getActions (Entity actorEntity ) {
251- return actorEntity .getActions ()
252- .values ()
253- .stream ()
254- .filter (v -> Entity .EntityMethodType .DIRECT .equals (v .getType ()))
255- .map (action ->
256- ActorOuterClass .Action .newBuilder ()
257- .setName (action .getName ())
258- .build ()
259- )
260- .collect (Collectors .toList ());
196+ return actorEntity .getActions ().values ().stream ().filter (v -> Entity .EntityMethodType .DIRECT .equals (v .getType ())).map (action -> ActorOuterClass .Action .newBuilder ().setName (action .getName ()).build ()).collect (Collectors .toList ());
261197 }
262198
263199 private List <ActorOuterClass .FixedTimerAction > getTimerActions (Entity actorEntity ) {
264- List <ActorOuterClass .FixedTimerAction > timerActions = actorEntity .getTimerActions ()
265- .values ()
266- .stream ()
267- .filter (v -> Entity .EntityMethodType .TIMER .equals (v .getType ()))
268- .map (action ->
269- ActorOuterClass .FixedTimerAction .newBuilder ()
270- .setAction (
271- ActorOuterClass .Action .newBuilder ()
272- .setName (action .getName ())
273- .build ())
274- .setSeconds (action .getFixedPeriod ())
275- .build ()
276- )
277- .collect (Collectors .toList ());
200+ List <ActorOuterClass .FixedTimerAction > timerActions = actorEntity .getTimerActions ().values ().stream ().filter (v -> Entity .EntityMethodType .TIMER .equals (v .getType ())).map (action -> ActorOuterClass .FixedTimerAction .newBuilder ().setAction (ActorOuterClass .Action .newBuilder ().setName (action .getName ()).build ()).setSeconds (action .getFixedPeriod ()).build ()).collect (Collectors .toList ());
278201
279202 log .debug ("Actor have TimeActions: {}" , timerActions );
280203 return timerActions ;
281204 }
282205
283206 @ Override
284207 public String toString () {
285- return new StringJoiner (", " , Spawn .class .getSimpleName () + "[" , "]" )
286- .add ("system='" + system + "'" )
287- .add ("port=" + port )
288- .add ("host='" + host + "'" )
289- .add ("proxyHost='" + proxyHost + "'" )
290- .add ("proxyPort=" + proxyPort )
291- .toString ();
208+ return new StringJoiner (", " , Spawn .class .getSimpleName () + "[" , "]" ).add ("system='" + system + "'" ).add ("port=" + port ).add ("host='" + host + "'" ).add ("proxyHost='" + proxyHost + "'" ).add ("proxyPort=" + proxyPort ).toString ();
292209 }
293210
294211 public static final class SpawnSystem {
@@ -382,15 +299,9 @@ public SpawnSystem withTransportOptions(TransportOpts opts) {
382299 }
383300
384301 public Spawn build () {
385- this .actorIdCache = Caffeine .newBuilder ()
386- .maximumSize (CACHE_MAXIMUM_SIZE )
387- .expireAfterWrite (Duration .ofSeconds (CACHE_EXPIRE_AFTER_WRITE_SECONDS ))
388- .build ();
302+ this .actorIdCache = Caffeine .newBuilder ().maximumSize (CACHE_MAXIMUM_SIZE ).expireAfterWrite (Duration .ofSeconds (CACHE_EXPIRE_AFTER_WRITE_SECONDS )).build ();
389303
390- this .client = new OkHttpSpawnClient (
391- this .system ,
392- this .transportOpts .getProxyHost (),
393- this .transportOpts .getProxyPort ());
304+ this .client = new OkHttpSpawnClient (this .system , this .transportOpts .getProxyHost (), this .transportOpts .getProxyPort ());
394305
395306 return new Spawn (this );
396307 }
@@ -427,37 +338,31 @@ private Optional<Entity> getEntity(Class<?> actorKlass, Object arg, ActorFactory
427338
428339 private Optional <Entity > getStatefulEntity (Class <?> actorKlass , Object arg , ActorFactory factory ) {
429340 if (Objects .nonNull (actorKlass .getAnnotation (StatefulNamedActor .class ))) {
430- return Optional .of (Entity .fromAnnotationToEntity (
431- actorKlass , actorKlass .getAnnotation (StatefulNamedActor .class ), arg , factory ));
341+ return Optional .of (Entity .fromAnnotationToEntity (actorKlass , actorKlass .getAnnotation (StatefulNamedActor .class ), arg , factory ));
432342 }
433343
434344 if (Objects .nonNull (actorKlass .getAnnotation (StatefulUnNamedActor .class ))) {
435- return Optional .of (Entity .fromAnnotationToEntity (
436- actorKlass , actorKlass .getAnnotation (StatefulUnNamedActor .class ), arg , factory ));
345+ return Optional .of (Entity .fromAnnotationToEntity (actorKlass , actorKlass .getAnnotation (StatefulUnNamedActor .class ), arg , factory ));
437346 }
438347
439348 if (Objects .nonNull (actorKlass .getAnnotation (StatefulPooledActor .class ))) {
440- return Optional .of (Entity .fromAnnotationToEntity (
441- actorKlass , actorKlass .getAnnotation (StatefulPooledActor .class ), arg , factory ));
349+ return Optional .of (Entity .fromAnnotationToEntity (actorKlass , actorKlass .getAnnotation (StatefulPooledActor .class ), arg , factory ));
442350 }
443351
444352 return Optional .empty ();
445353 }
446354
447355 private Optional <Entity > getStatelessEntity (Class <?> actorKlass , Object arg , ActorFactory factory ) {
448356 if (Objects .nonNull (actorKlass .getAnnotation (StatelessNamedActor .class ))) {
449- return Optional .of (Entity .fromAnnotationToEntity (
450- actorKlass , actorKlass .getAnnotation (StatelessNamedActor .class ), arg , factory ));
357+ return Optional .of (Entity .fromAnnotationToEntity (actorKlass , actorKlass .getAnnotation (StatelessNamedActor .class ), arg , factory ));
451358 }
452359
453360 if (Objects .nonNull (actorKlass .getAnnotation (StatelessUnNamedActor .class ))) {
454- return Optional .of (Entity .fromAnnotationToEntity (
455- actorKlass , actorKlass .getAnnotation (StatelessUnNamedActor .class ), arg , factory ));
361+ return Optional .of (Entity .fromAnnotationToEntity (actorKlass , actorKlass .getAnnotation (StatelessUnNamedActor .class ), arg , factory ));
456362 }
457363
458364 if (Objects .nonNull (actorKlass .getAnnotation (StatelessPooledActor .class ))) {
459- return Optional .of (Entity .fromAnnotationToEntity (
460- actorKlass , actorKlass .getAnnotation (StatelessPooledActor .class ), arg , factory ));
365+ return Optional .of (Entity .fromAnnotationToEntity (actorKlass , actorKlass .getAnnotation (StatelessPooledActor .class ), arg , factory ));
461366 }
462367
463368 return Optional .empty ();
0 commit comments