scala actors:如果队列太长,请丢弃消息?

Kim*_*bel 3 concurrency scala actor scala-2.9

如果它变得太满,我想从演员的邮箱中删除邮件.例如,如果队列大小达到1000条消息,则应删除最旧的消息.

Vas*_*iuk 6

您无法直接使用邮箱,但可以在现有库之上实现Message Expiration模式.

发送每条消息的创建日期:

case class ExpirableMessage(msg: String, createdAt: Long) 
Run Code Online (Sandbox Code Playgroud)

使用扫描邮箱reactWithin(0),并过滤掉过期的邮件:

react{ 
  case msg: ExpirableMessage => 
    // handle the message
    // clean the mailbox with nested react
    reactWithin(0){
        case ExpirableMessage(_, createdAt) if(currentTimeMillis - createdAt > INTERVAL) =>
        case TIMEOUT =>
    }
}
Run Code Online (Sandbox Code Playgroud)