concept actor in category groovy

appears as: Actors, ctors, actor, The actor, n actor, ctor
Groovy in Action, Second Edition

This is an excerpt from Manning's book Groovy in Action, Second Edition.

Implementing the equals() and hashCode() methods correctly is repetitive and error-prone. Luckily the @EqualsAndHashCode annotation does it for you. The generated equals() method obeys the contract of Object.equals(), and the generated hashCode() produces an appropriate hash value using a standard algorithm factoring in the constituent fields. The following listing shows using @EqualsAndHashCode in action on an Actor class.

Hopefully by now you’re comfortable with the static factory methods that GPars has consistently provided. The actor and the reactor methods are two more examples of the same, living in the Actors class. They each return an Actor instance, which is started right away.

They both have a closure argument, telling them what the generated actor should do when its act() method is called, which happens as part of starting the actor. This is straightforward for the actor{} factory method but a bit more involved in the case of reactor{}. Here, the given closure is wrapped so that it’s executed concurrently whenever a message is waiting in the inbox and the actor is not already busy. The message is passed to the closure and the closure result is replied to the sender. You can think of a reactor as having an act() method of

Figure 18.2. A simple example network of actors for processing a request. A coordination actor waits for the authorization reply and triggers a calculation. Many actors inform the audit actor. A collector returns the result.

Third, sendAndWait() is a troublesome feature. You may wait forever. Give it a timeout at least. But if it times out, what do we do? Try again? The rule of thumb is that if you’re using actors together with sendAndWait(), you’ve probably chosen the wrong concept.

When creating a network of actors you may get some inspiration for tailoring responsibilities along the lines of enterprise integration patterns as implemented in the Apache Camel project (see http://camel.apache.org/enterprise-integration-patterns.html and Camel in Action by Claus Ibsen and Jonathan Anstey (Manning, 2010), http://manning.com/ibsen/). If you think in terms of Enricher, Router, Translator, Endpoint, Splitter, Aggregator, Filter, Resequencer, and Checker, you’re on the right track.