@@ -365,6 +365,65 @@ volumes:
365365
366366```
367367
368+ You may also want your Actors to be initialized with some dependent objects similarly to how you would use the
369+ dependency injection pattern.
370+ In this case, it is enough to declare a constructor that receives a single argument for its actor.
371+
372+ ``` java
373+ package io.eigr.spawn.java.demo ;
374+
375+ import io.eigr.spawn.api.Value ;
376+ import io.eigr.spawn.api.actors.ActorContext ;
377+ import io.eigr.spawn.api.actors.annotations.Action ;
378+ import io.eigr.spawn.api.actors.annotations.NamedActor ;
379+ import io.eigr.spawn.api.actors.annotations.TimerAction ;
380+ import io.eigr.spawn.api.actors.workflows.Broadcast ;
381+ import io.eigr.spawn.java.demo.domain.Domain ;
382+
383+ import java.util.Map ;
384+
385+ @NamedActor (name = " joe" , stateful = true , stateType = Domain . JoeState . class, channel = " test" )
386+ public final class Joe {
387+ private final String someValue;
388+
389+ public Joe (Map<String , String > args ) {
390+ this . someValue = args. get(" someKey" );
391+ }
392+
393+ @Action (inputType = Domain . Request . class)
394+ public Value setLanguage (Domain .Request msg , ActorContext<Domain . JoeState > context ) {
395+ return Value . at()
396+ .response(Domain . Reply . newBuilder()
397+ .setResponse(" Hello From Java" )
398+ .build())
399+ .state(updateState(" java" ))
400+ .reply();
401+ }
402+
403+ // ...
404+ }
405+ ```
406+
407+ In this case you need to register your Actor using the ` addActorWithArgs ` method like as follows:
408+
409+ ``` java
410+ public class App {
411+ public static void main (String [] args ) throws Exception {
412+ Map<String , String > actorConstructorArgs = new HashMap<> ();
413+ actorConstructorArgs. put(" someKey" , " someValue" );
414+
415+ Spawn spawnSystem = new Spawn .SpawnSystem ()
416+ .create(" spawn-system" )
417+ .withPort(8091 )
418+ .withProxyPort(9003 )
419+ .addActorWithArgs(Joe . class, actorConstructorArgs, arg - > new Joe ((Map<String , String > ) arg))
420+ .build();
421+
422+ spawnSystem. start();
423+ }
424+ }
425+ ```
426+
368427And this is it to start! Now that you know the basics of local development, we can go a little further.
369428
370429## Advanced Use Cases
0 commit comments