Akka 有限状态机实例
我正在尝试利用Akka的有限状态机框架来实现我的用例。我正在开发一个系统,该系统处理通过各种状态的请求。
此处的请求是需要与它所依赖的应用程序一起部署的应用程序名称:
Request for application A -> A is in a QUEUED state
Discover A's dependency B -> B is in a QUEUED state
B is being processed -> B is in a PROCESSING STATE
A is being processed -> A is in a PROCESSING STATE
B is processed -> B is in a DONE state
A is processed -> A is in a DONE state
为此,我在发现时初始化有限状态机。因此,当请求传入时,就会创建 FSM,当从其中一个执行组件发现时,就会初始化 FSM。A
B
B
我是否初始化 FSM 实例并将其传递给所有参与者,同时将 FSM 传递给对数据执行的操作,以便状态机处于正确的状态?tell
以下是状态机的相关部分:
when(QUEUED, matchEvent(requestAccepted.class, MyApp.class,
(requestAccepted, service) ->
goTo(PROCESSING).replying(PROCESSING)));
when(PROCESSING, matchEvent(completed.class, MyApp.class,
(completed, service) -> goTo(DONE).replying(DONE)));
// During transitions, save states in the database.
onTransition(matchState(PROCESSING, DONE, () -> {
nextStateData().setServiceStatus(DONE);
databaseWriter.tell(nextStateData(), getSelf());
下面是处理请求的其中一个参与者的示例:
ProcessingActor extends AbstractActor {
@Override
public void onReceive(Object message) throws Throwable {
if (message instanceof processApplication) {
// process the app
// Initialize FSM for the Application
FSM myFSM = Props.create(MYFSM.class);
myFSM.tell( new completed(processApplication.app)
}
这是初始化状态机并使用它的正确方法吗?或者初始化应该在 的构造函数中进行?但在这种情况下,每个应用程序(数据)不会有一个状态机。ProcessingActor