Skip to content

Commit f3f0d75

Browse files
committed
Added note about spawn cli
1 parent 1831867 commit f3f0d75

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

README.md

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -337,26 +337,35 @@ public final class JoeActor extends StatefulActor<State> {
337337

338338
### Dissecting the code
339339

340-
Class Declaration
340+
***Class Declaration***
341341

342342
```java
343+
package io.eigr.spawn.java.demo;
344+
345+
import io.eigr.spawn.api.actors.StatefulActor;
346+
import io.eigr.spawn.java.demo.domain.Actor.State;
347+
343348
public final class JoeActor extends StatefulActor<State> {
344349
// ...
345350
}
346351
```
347352

348-
The JoeActor class extends StatefulActor<State>. StatefulActor is a generic class provided by the Spawn API,
349-
which takes a type parameter for the state. In this case, the state type is State.
353+
The `JoeActor` class extends `StatefulActor<State>`. `StatefulActor` is a generic class provided by the Spawn API,
354+
which takes a type parameter for the state. In this case, the state type is `io.eigr.spawn.java.demo.domain.Actor.State`
355+
defined in above protobuf file.
356+
357+
***Configure Actor Behavior***
350358

351-
Configure Actor Behavior
352359
```java
353-
@Override
354-
public ActorBehavior configure(BehaviorCtx context) {
355-
return new NamedActorBehavior(
356-
name("JoeActor"),
357-
channel("test.channel"),
358-
action("SetLanguage", ActionBindings.of(Request.class, this::setLanguage))
359-
);
360+
public final class JoeActor extends StatefulActor<State> {
361+
@Override
362+
public ActorBehavior configure(BehaviorCtx context) {
363+
return new NamedActorBehavior(
364+
name("JoeActor"),
365+
channel("test.channel"),
366+
action("SetLanguage", ActionBindings.of(Request.class, this::setLanguage))
367+
);
368+
}
360369
}
361370
```
362371

@@ -370,22 +379,24 @@ This `configure` method is overridden from `StatefulActor` and is used to config
370379
which takes a `Request` message as input.
371380
Where the second parameter of `ActionBindings.of(type, lambda)` method is a lambda.
372381

373-
Handle request
382+
***Handle request***
374383

375384
```java
376-
private Value setLanguage(ActorContext<State> context, Request msg) {
377-
if (context.getState().isPresent()) {
378-
// Do something with the previous state
379-
}
380-
381-
return Value.at()
382-
.response(Reply.newBuilder()
383-
.setResponse(String.format("Hi %s. Hello From Java", msg.getLanguage()))
384-
.build())
385-
.state(updateState(msg.getLanguage()))
386-
.reply();
387-
}
385+
public final class JoeActor extends StatefulActor<State> {
386+
//
387+
private Value setLanguage(ActorContext<State> context, Request msg) {
388+
if (context.getState().isPresent()) {
389+
// Do something with the previous state
390+
}
388391

392+
return Value.at()
393+
.response(Reply.newBuilder()
394+
.setResponse(String.format("Hi %s. Hello From Java", msg.getLanguage()))
395+
.build())
396+
.state(updateState(msg.getLanguage()))
397+
.reply();
398+
}
399+
}
389400
```
390401

391402
This method `setLanguage` is called when the `SetLanguage` action is invoked. It takes an `ActorContext<State>` and a `Request` message as parameters.
@@ -501,6 +512,8 @@ volumes:
501512
502513
```
503514

515+
> **_NOTE:_** Or just use the [Spawn CLI](https://github.com/eigr/spawn?tab=readme-ov-file#getting-started-with-spawn) to take care of the development environment for you.
516+
504517
You may also want your Actors to be initialized with some dependent objects similarly to how you would use the
505518
dependency injection pattern.
506519
In this case, it is enough to declare a constructor that receives a single argument for its actor.

0 commit comments

Comments
 (0)