Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Utility class used for creating Actor. #1301

Open
Roiocam opened this issue Apr 29, 2024 · 0 comments
Open

[Feature] Utility class used for creating Actor. #1301

Roiocam opened this issue Apr 29, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@Roiocam
Copy link
Member

Roiocam commented Apr 29, 2024

Motivation

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

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

ActorRef<MyType> actorRef = future.toCompletableFuture().join();

for comparison, in classic we have:

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?

  public interface SpawnUtility {

        <T> ActorRef<T> spawn(ActorSystem<SpawnProtocol.Command> spawnableSystem, Behavior<T> behavior, String name, Props props);

        <T> ActorRef<T> spawn(ActorSystem<SpawnProtocol.Command> spawnableSystem, Behavior<T> behavior, String name);

        <T> ActorRef<T> spawn(ActorSystem<SpawnProtocol.Command> spawnableSystem, Behavior<T> behavior);

        <T> CompletionStage<ActorRef<T>> spawnAsync(ActorSystem<SpawnProtocol.Command> spawnableSystem, Behavior<T> behavior, String name, Props props);

        <T> CompletionStage<ActorRef<T>> spawnAsync(ActorSystem<SpawnProtocol.Command> spawnableSystem, Behavior<T> behavior, String name);

        <T> CompletionStage<ActorRef<T>> spawnAsync(ActorSystem<SpawnProtocol.Command> spawnableSystem, Behavior<T> behavior);

    }

    @Test
    public void spawnTest(){
        ActorSystem<SpawnProtocol.Command> spawnableSystem = null;
        SpawnUtility utility = null;

        ActorRef<MyType> actorRef = utility.spawn(spawnableSystem, MyType.createBehavior(), "name");
        actorRef.tell(null);
    }
@Roiocam Roiocam added the enhancement New feature or request label Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant