Global Data Collector (Singleton) in Akka - best practices?

130 views
Skip to first unread message

Markus Jais

unread,
Jan 13, 2014, 5:15:23 AM1/13/14
to akka...@googlegroups.com
Hello,

I need to integrate a Java library that is used to collects lot's of runtime information about errors, failures, etc.
It has many data collectors. There may be better ways to do this but I need to use this library for current projects.

The collectors should exist once per JVM (we are not using Akka cluster right now). If the JVM crashes, the data can be lost.
Everything is in memory.

How would I use such a singleton in the best way within an Akka/Spray service?

I had the following ideas:

1) Just use a global singleton and something like a getInstance method which can be called form everywhere incl. each actor

2) Hide the singleton within an actor as a private member. Create only one such Actor and pass it around. Send messages to that actor and translate the message to calls to the data collector. This is ugly as I have to pass around the actor.

3) Use a trait similar ActorLogging which I can add to each Actor that needs those collectors and guarantee within the trait that there is only once instance.



Are there any other options? Who has had to deal with a similar problem? Any recomendations for how to do this the Akka way?

The old Java applications just use Spring and Singleton scop but the Scala/Akka services don't use Spring.


Cheers,

Markus

Alec Zorab

unread,
Jan 13, 2014, 5:20:59 AM1/13/14
to akka...@googlegroups.com


--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/groups/opt_out.

Markus Jais

unread,
Jan 13, 2014, 7:39:20 AM1/13/14
to akka...@googlegroups.com
Hi Alec,

thanks. That looks great. 

Would it make sense to hide the extension in a trait and use it as a mixin?
Meaning, the trait could have the code like:

counter = CountExtension(context.system)

and in my Actor I only have some like:

class MyActor with Counting {
  counter.increment()
}

Markus

Alec Zorab

unread,
Jan 13, 2014, 7:44:49 AM1/13/14
to akka...@googlegroups.com
That's certainly how I tend to do it. We use the same thing for injecting configs, dbs etc. Almost anything that you want to be a "singleton" but you need to be able to swap in different implementations for testing, etc.

Markus Jais

unread,
Jan 13, 2014, 8:02:48 AM1/13/14
to akka...@googlegroups.com
Thanks Alec! I will do it like that.

Markus

Markus Jais

unread,
Jan 16, 2014, 1:39:52 AM1/16/14
to akka...@googlegroups.com
One more question. How I can pass an ActorRef to my extension?

I want to do something like this:

class MyExtension (val myActorRef: ActorRef) extends Extension {
  //....
}
object MyExtension extends ExtensionId[MyExtension]
  with ExtensionIdProvider {
  override def lookup = MyExtension
  override def createExtension(system: ExtendedActorSystem) = new MyExtension(????????????????)
  override def get(system: ActorSystem): MyExtension = super.get(system.)
}

trait ActorDataCollector { self: Actor =>
  def myActorRef = {
    MyExtension(context.system).myActorRef
  }
}

I want to pass an ActorRef to MyExension  that I create in the main method to my extension and then use a trait to make it easier to use it. Of course the extension gets created with createExtension which seems to be called by Akka itself.

 I thought I can do something like this in main
val myActor = system.actorOf(Props[MyActor], "my")
val myExtension = new MyExtension(myActor)


And then in my REST actors (I use Spray 1.2):

class EchoActor extends Actor with ActorLogging with ActorServerCheck {
...
 import MyActor._
....
myActorRef ! EchoSuccess


This should guarantee that there is only once instance of that actor and would make it easy to use.

But it doesn't work as the Extension get's initialized on it's own as it seems. I cannot set the Actor where the "?????????????" are.

Is this a good idea to pass an Actor to an extension and what is the recommend way to do it?


Cheers,

Markus















Roland Kuhn

unread,
Jan 16, 2014, 3:46:57 AM1/16/14
to akka-user
Hi Markus,

since extensions have get-or-create semantics they cannot take arguments: how would you know which argument was provided to the instance you actually get back? Therefore extensions are only providers, not consumers (well, they can reference other extensions if you carefully avoid loops).

What this means is that you could just move the creation of that actor into the extension. OTOH every actor with a name is already a singleton which can be looked up, so I don’t see a need for using an extension to achieve this effect.

Regards,

Roland


Dr. Roland Kuhn
Akka Tech Lead
Typesafe – Reactive apps on the JVM.
twitter: @rolandkuhn


Alec Zorab

unread,
Jan 16, 2014, 5:53:01 AM1/16/14
to akka...@googlegroups.com
We have a number of utility extensions that create single actors for caching various systems, providing db access etc. 
Generally, if I think a system is reusable in other projects, it goes into an extension. If it's application specific, it'll often just be an actor I bring up in the main method.

Markus Jais

unread,
Jan 16, 2014, 7:24:55 AM1/16/14
to akka...@googlegroups.com
Thanks Alec, Roland,

as  always very helpful. I will play with it a bit more and try the lookup as suggested.

Markus


Alec Zorab <alec...@gmail.com> schrieb am 11:53 Donnerstag, 16.Januar 2014:
We have a number of utility extensions that create single actors for caching various systems, providing db access etc. 
Generally, if I think a system is reusable in other projects, it goes into an extension. If it's application specific, it'll often just be an actor I bring up in the main method.

Roland Kuhn

unread,
Jan 16, 2014, 7:25:13 AM1/16/14
to akka-user
16 jan 2014 kl. 11:53 skrev Alec Zorab <alec...@gmail.com>:

We have a number of utility extensions that create single actors for caching various systems, providing db access etc. 
Generally, if I think a system is reusable in other projects, it goes into an extension. If it's application specific, it'll often just be an actor I bring up in the main method.

This is a good guide line, thanks for sharing!

Regards,

Roland


--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages