Yesterday I started rewriting in Java a few scripts that I use for
checking the consistency of my photo library, basically they compute the
MD5 for each file and re-check it periodically. I took the opportunity to
design the code in actor-like way.
I stress the point "design in actor-like way", I mean I'm using actors at
gross grain. I'm not going to have thousands or millions of actors, but
only ten or such. Thus I don't need to use a greatly tuned library for
optimal performance. At the moment I'm liking actors only on an API design
premise, that is they fit my need for decoupling things and have a nicely
pluggable stuff.
Before going on, I need to compare with existing solutions. I only know a
little about existing Java solutions for actors and all the ones I know
are too much for my needs.
Just for the record, my code is such as:
final String path =
"/media/Media/Photography/Photos/Incoming/2011/September";
final UnitOfWork unitOfWork =
FileScanRequestMessage.forPath(path).send();
unitOfWork.waitForCompletion(); // yeah, not very
actor-oriented, but the world is made of mixed stuff
@Actor @ThreadSafe @Slf4j
public class FileObjectDiscoveryActor
{
@MessageListener
public void discover (final @Nonnull FileScanRequestMessage message)
{
for (final FileObject child : message.getFolder().getChildren())
{
(child.isFolder() ? FileScanRequestMessage.forFile(child) :
FileDiscoveredMessage.forFile(child)).send();
}
}
}
@Actor @ThreadSafe @Slf4j
public class FingerprintComputerActor
{
private final Provider<FingerprintComputer> fingerprintComputer =
Locator.createProviderFor(FingerprintComputer.class);
@MessageListener
public void process (final @Nonnull FileDiscoveredMessage message)
{
final FileObject fileObject = message.getFileObject();
try
{
final Fingerprint fingerPrint =
fingerprintComputer.get().computeFingerprint(fileObject, "MD5");
FingerprintComputedMessage.forFile(fileObject).withFingerprint(fingerPrint).send();
}
catch (Exception e)
{
FileDamageDetectedMessage.forFile(fileObject).withCause(e).send();
}
}
}
@Message, @Actor and @MessageListener are three in-house annotations that
do the job, with a few dozens of LoC. My only need is to have a context
that is similar to a transaction: that UnitOfWork is created for the first
request and propagated, via ThreadLocal, to all the involved threads,
including the case in which further messages are generated for the
elaboration. A special completion message is fired when a UnitOfWork is
completed. Last but not least, my Actors are going to be statically
instantiated.
That's what I need. My code is just a few kbs of bytecode. I need
something similar: if it's small and works, I'll use it. Otherwise I'll
keep my stuff. No, Kevin, I'm not going to move to Scala for this :-)
--
Fabrizio Giudici - Java Architect, Project Manager
Tidalwave s.a.s. - "We make Java work. Everywhere."
[email protected]
http://tidalwave.it - http://fabriziogiudici.it
--
You received this message because you are subscribed to the Google Groups "The Java
Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en.