Skip to content

Commit d71ed09

Browse files
committed
Update docs and better toString to ActorContext
1 parent 6e4eae4 commit d71ed09

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
368427
And 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

src/main/java/io/eigr/spawn/api/actors/ActorContext.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.eigr.spawn.api.Spawn;
44

55
import java.util.Optional;
6+
import java.util.StringJoiner;
67

78
public final class ActorContext<S extends Object> {
89

@@ -30,9 +31,9 @@ public Optional<S> getState() {
3031

3132
@Override
3233
public String toString() {
33-
final StringBuilder sb = new StringBuilder("ActorContext{");
34-
sb.append("state=").append(state);
35-
sb.append('}');
36-
return sb.toString();
34+
return new StringJoiner(", ", ActorContext.class.getSimpleName() + "[", "]")
35+
.add("spawn=" + spawn)
36+
.add("state=" + state)
37+
.toString();
3738
}
3839
}

0 commit comments

Comments
 (0)