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 react
s 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:
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?
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])
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With