【问题标题】:Initializing actor with Props使用道具初始化actor
【发布时间】:2018-08-31 16:51:14
【问题描述】:

我有以下 FSM

public class ActorOnFsm {
    public static enum State {
    FirstState,
    SecondState,
    ThirdState,
    FourthState
    }

    public static final class ServiceData {

    }


    public class ActorFSM extends AbstractFSM<State, ServiceData> { 
    {
        startWith(FirstState, new ServiceData());
        when(FirstState,
            matchEvent(SomeMessage.class,
                ServiceData.class,
                (powerOn, noData) ->
            goTo(SecondState)
            .replying(SecondState))
            );


        when(SecondState,
            matchEvent(SomeOtherMessage.class,
                ServiceData.class,
                (powerOn, noData) ->
            goTo(ThirdState)
            .replying(ThirdState))
            );

        when(FirstState,
            matchEvent(soemErrorMessage.class,
                ServiceData.class,
                (powerOn, noData) ->
            goTo(FourthState)
            .replying(FourthState))
            );

        initialize();
    }



    }
}

我这样初始化actor

最终道具 props = Props.create(ActorOnFsm.class); 最终 ActorRef underTest = system.actorOf(props);

这给出了一个错误“unknown actor creator [ActorOnFsm] 就行了

final Props props = Props.create(ActorOnFsm.class);

初始化这个actor的正确方法是什么?

我也尝试更改类以扩展 AbstractLogging 但结果相同

我也尝试创建一个空的构造函数,但结果相同

尝试在 props 中发送状态和数据,但仍然得到相同的错误

    final Props props = Props.create(DeployerOnFsm.class, state, data);

【问题讨论】:

    标签: java akka actor


    【解决方案1】:

    您应该传递给Props 工厂的类是ActorFSM,它在ActorOnFsm 内部定义:

    final Props props = Props.create(ActorOnFsm.ActorFSM.class);
    

    但是,将内部类传递给Props 工厂可能会出现问题。将ActorFSM 设置为顶级类会更传统,在这种情况下,对Props 的调用将更改为:

    final Props props = Props.create(ActorFSM.class);
    

    此外,您的状态转换之一似乎有错字:

    when(FirstState,
        matchEvent(soemErrorMessage.class,
                  // ^
    

    大概你打算写SomeErrorMessage.class而不是soemErrorMessage.class

    【讨论】:

    • 非常感谢!!!但我还有另一个问题,即“在类中找不到匹配的构造函数”会出错,是否需要显式构造函数?
    • 原来将类移到嵌套类之外修复了它
    猜你喜欢
    • 2023-03-22
    • 2019-05-10
    • 2014-09-29
    • 2017-07-19
    • 2023-01-29
    • 2019-05-03
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多