gpt4 book ai didi

java - 在 Akka 类型的 Actor 中配置 Dispatcher

转载 作者:行者123 更新时间:2023-12-02 02:06:15 28 4
gpt4 key购买 nike

我正在使用 Akka typed,但我无法查看官方文档( https://doc.akka.io/docs/akka/current/typed/actors.html#actors ),我发现该文档非常简短,说明如何在键入的 Actor 中配置调度程序。

这是我的代码示例

private int actorTimeout = Integer.parseInt(getProperty("environment.actor.timeout", "10"));

@Autowired
private AkkaTypedDAO akkaTypedDAO;

private ActorSystem<AkkaTypedDTO> system;

@PostConstruct
private void initActor() {
system = ActorSystem.create(akkaTypedDAO.daoWithSupervisor, "AkkaTypedDAO");
}

private final Behavior<CommandAkkaTyped> service = Actor.immutable((ctx, msg) -> {
sendToDAO(msg.id).thenApply(either -> {
msg.replyTo.tell(either);
return either;
});
return Actor.same();
});

public final Behavior<CommandAkkaTyped> serviceWithSupervisor = Actor.supervise(service).onFailure(Exception.class, restart());

private CompletionStage<Either<ConnectorErrorVO, EntityDaoDTO>> sendToDAO(MUSIn<AkkaTypedPayLoad> id) {
return AskPattern.ask(system,
(ActorRef<Either<ConnectorErrorVO, EntityDaoDTO>> replyTo) -> new AkkaTypedDTO(new EntityDaoDTO(musIn), replyTo),
new Timeout(actorTimeout, TimeUnit.SECONDS), system.scheduler());
}

当我创建 ActorSystem 时,如何为 Actor.immutable 配置调度程序?

最佳答案

您正在使用过时的 Akka Typed 版本(有关 Akka Typed API 历史的更多信息,请参阅 here)。从 Akka Typed documentation 的当前版本(截至撰写本文时为 2.5.14)开始:

To specify a dispatcher when spawning an actor use DispatcherSelector. If not specified, the actor will use the default dispatcher, see Default dispatcher for details.

public static final Behavior<Start> main =
Behaviors.setup(context -> {
final String dispatcherPath = "akka.actor.default-blocking-io-dispatcher";

Props props = DispatcherSelector.fromConfig(dispatcherPath);
final ActorRef<HelloWorld.Greet> greeter =
context.spawn(HelloWorld.greeter, "greeter", props);

return Behaviors.receiveMessage(msg -> {
ActorRef<HelloWorld.Greeted> replyTo =
context.spawn(HelloWorldBot.bot(0, 3), msg.name);
greeter.tell(new HelloWorld.Greet(msg.name, replyTo));
return Behaviors.same();
});
});

关于java - 在 Akka 类型的 Actor 中配置 Dispatcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50759360/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com