apache / apache/pekko

[Feature] Utility class used for creating Actor.

Open
#1,301 0 comments 1 reaction 0 assignees View on GitHub
enhancement
Dominant language
Scala
Stars
1.6k
Forks
211
Avg merge
1d 6h
Merged PRs (30d)
89

Description

## Motivation

In typed actor, if we want to create an actor, we have to use code like this:

```java
CompletionStage> future = AskPattern.>ask(
system,
reply ->
new SpawnProtocol.Spawn<>(
MyType.createBehavior(),
"name",
Props.empty(),
reply),
Duration.ofSeconds(3),
system.scheduler())

ActorRef actorRef = future.toCompletableFuture().join();
```

for comparison, in classic we have:

```java
Props props = Props.create(MyType.class, ()-> new MyType()).withDispatcher("dispatcher");
ActorRef actorRef = actorSystem.actorOf(props, "name");
```

Can we wrapper those ask into utility class like this?

```java
public interface SpawnUtility {

ActorRef spawn(ActorSystem spawnableSystem, Behavior behavior, String name, Props props);

ActorRef spawn(ActorSystem spawnableSystem, Behavior behavior, String name);

ActorRef spawn(ActorSystem spawnableSystem, Behavior behavior);

CompletionStage> spawnAsync(ActorSystem spawnableSystem, Behavior behavior, String name, Props props);

CompletionStage> spawnAsync(ActorSystem spawnableSystem, Behavior behavior, String name);

CompletionStage> spawnAsync(ActorSystem spawnableSystem, Behavior behavior);

}

@Test
public void spawnTest(){
ActorSystem spawnableSystem = null;
SpawnUtility utility = null;

ActorRef actorRef = utility.spawn(spawnableSystem, MyType.createBehavior(), "name");
actorRef.tell(null);
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.