简体   繁体   English

在处理当前消息时,在akka中使用排队请求阻止其他参与者

[英]block other actors with queuing requests in akka while processing current message

I have an actor with two types of messages, one for inserting to mysql and the other for inserting to redis. 我有一个具有两种消息类型的actor,一种用于插入mysql,另一种用于插入redis。 I wonder how block that redis actor with queuing the messages in that actor related to inserting in redis, while the mysql actor is processing. 我想知道如何在mysql actor正在处理的同时,通过排队该actor中与插入redis有关的消息来阻止该redis actor。

@Override
public Receive createReceive() {
    return receiveBuilder()
            .match(RedisMessage.class, message -> {
                try {
                    accountService.saveBusinessAccountRedis(message.redisBusinessAccount);
                    getSender().tell(OK, getSelf());
                } catch (Exception e) {
                    //getSender().tell(new Status.Failure(e), getSelf());
                    e.printStackTrace();
                    //createRedisUser(idMessage.uid);
                    getSender().tell(NOK, getSelf());
                }

            }).match(MySqlMessage.class, message -> {
                try {
                    accountService.saveBusinessAccount();
                    //ToDo: test for queuing another actor! remove!! :)
                    Thread.sleep(300);
                    getSender().tell(OK, getSelf());
                } catch (Exception e) {
                    e.printStackTrace();
                    getSender().tell(NOK, getSelf());
                }

            }).build();
}


@Getter
@Setter
@AllArgsConstructor
public static class RedisMessage {
    private final RedisBusinessAccount redisBusinessAccount;
}

@Getter
@Setter
@AllArgsConstructor
public static class MySqlMessage {
    private final BusinessAccount businessAccount;
}

So what you want is you want to avoid processing RedisMessage s while you haven't yet received the response of the action triggered by MySqlMessage , right? 所以,你要的是要避免处理RedisMessage s,而你还没有收到触发的动作的响应MySqlMessage ,对不对?

Do you want to be able to process multiple MySqlMessage s in the same actor? 您是否希望能够在同一个actor中处理多个MySqlMessage I suspect you do not. 我怀疑你没有。

I would suggest using become and 'stashing'. 我建议使用“ become和“存储”。 When processing a MySqlMessage , use become to move the actor to a state where it is 'waiting for the MySql result'. 在处理MySqlMessage ,可以使用become将actor移到“正在等待MySql结果”的状态。 In that state, when you receive additional MySqlMessage or RedisMessage , you stash(msg) them. 在这种状态下,当您收到其他MySqlMessageRedisMessageRedisMessage它们RedisMessage stash(msg) When you receive the OK or NOK from the MySQL call, you unstashAll() and use become to go back to the 'normal' state where you can start processing any (stashed or new) commands again. 当您收到OKNOK从MySQL调用,你unstashAll()和使用become回到“正常”状态,在那里你可以开始处理任何(藏或新的)再命令。

Does that make sense? 那有意义吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM