在实践中与Scala actor编写应用程序 [英] Writing applications with Scala actors in practice

查看:72
本文介绍了在实践中与Scala actor编写应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经使用Scala actor编写了一些应用程序,并且我对人们如何处理或处理我遇到的一些问题很感兴趣。

I've now written a few applications using scala actors and I'm interested in how people have approached or dealt with some of the problems I've encountered.

过多的消息类或!?

我有一个actor,它对用户操作做出反应,必须引起某些事情发生。假设它对消息 UserRequestsX(id) 反应 s。我一直面临的一个问题是,由于我想对程序进行模块化,因此一个单独的参与者无法在不涉及其他参与者的情况下完成操作。例如,假设我需要使用 id 参数来检索一堆值,然后需要通过其他角色将其删除。如果我正在编写普通的Java程序,则可能会执行以下操作:

I have an actor which reacts to a user operation and must cause something to happen. Let's say it reacts to a message UserRequestsX(id). A continuing problem I have is that, because I want to modularize my programs, a single actor on its own is unable to complete the action without involving other actors. For example, suppose I need to use the id parameter to retrieve a bunch of values and then these need to be deleted via some other actor. If I were writing a normal Java program, I might do something like:

public void reportTrades(Date date) {
    Set<Trade> trades = persistence.lookup(date);
    reportService.report(trades);
}

这很简单。但是,使用演员会有点麻烦,因为我想避免使用 !? 。一个参与者对 ReportTrades(date)消息做出反应,但必须向 PersistenceActor 进行交易,然后要求 ReportActor 进行报告。我发现这样做的唯一方法是:

Which is simple enough. However, using actors this becomes a bit of a pain because I want to avoid using !?. One actor reacts to the ReportTrades(date) message but it must ask a PersistenceActor for the trades and then a ReportActor to report them. The only way I've found of doing this is to do:

react {
    case ReportTrades(date) =>
       persistenceActor ! GetTradesAndReport(date)
}

因此在我的 PersistenceActor 我有一个react块:

So that in my PersistenceActor I have a react block:

react {
    case GetTradesAndReport(date) =>
       val ts = trades.get(date) //from persietent store
       reportActor ! ReportTrades(ts)
}

但是现在我有2个问题:

But now I have 2 problems:


  1. 我必须创建额外的消息类来表示相同的请求(即报告交易)。实际上,在这种情况下,我有三个,但我可能还有更多-跟踪这些问题变得很困难

  2. 我应该如何称呼第一条消息和第三条消息 ReportTrades ?将它们都称为 ReportTrades 会造成混淆(或者,如果我这样做,则必须将它们放在单独的包装中)。基本上,没有这样的事情,例如 val 类型的重载

  1. I have to create extra message classes to represent the same request (i.e. "report trades"). In fact I have three in this scenario but I may have many more - it becomes a problem keeping track of these
  2. What should I call the first and third message ReportTrades? It's confusing to call them both ReportTrades (or if I do, I must put them in separate packages). Essentially there is no such thing as overloading a class by val type.

我缺少什么吗?我可以避免吗?我应该放弃并使用 !? 吗?人们是否使用某种组织结构来阐明正在发生的事情?

Is there something I'm missing? Can I avoid this? Should I just give up and use !? Do people use some organizational structure to clarify what is going on?

推荐答案

对我来说,您的 ReportTrades 消息正在混合两个不同的概念。一个是请求,顺序是响应。例如,它们可能被命名为 GetTradesReport(Date) SendTradesReport(List [Trade])。或者,也许是 ReportTradesByDate(Date) GenerateTradesReport(List [Trade])

To me, your ReportTrades message is mixing two different concepts. One is a Request, the order is a Response. They might be named GetTradesReport(Date) and SendTradesReport(List[Trade]), for example. Or, maybe, ReportTradesByDate(Date) and GenerateTradesReport(List[Trade]).

这篇关于在实践中与Scala actor编写应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆