Open Roiocam opened 4 months ago
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); }
Motivation
In typed actor, if we want to create an actor, we have to use code like this:
for comparison, in classic we have:
Can we wrapper those ask into utility class like this?