6
votes

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.

A plethora of Message classes or !?

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);
}

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)
}

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)
}

But now I have 2 problems:

  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?

2
Is it safe to access the persistent store directly in "react" ? I thought you couldn't do blocking operations. Maybe "receive" is needed for that particular actor.Dobes Vandermeer

2 Answers

2
votes

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]).

0
votes

Are there some objections to using reply? Or passing trades around? If not, your code would probably look like

react {
  case ReportTrades(date) => persistenceActor ! GetTrades(date)
  case Trades(ts) => // do smth with trades
}

and

react {
  case GetTrades(date) => reply(Trades(trades.get(date)))
}

respectively.