Akka与OSGi

Configuring the OSGi Framework

To use Akka in an OSGi environment, the org.osgi.framework.bootdelegation
property must be set to always delegate the sun.misc package to the boot classloader
instead of resolving it through the normal OSGi class space.

Activator

To bootstrap Akka inside an OSGi environment, you can use the akka.osgi.ActorSystemActivator class
to conveniently set up the ActorSystem.

  1. import akka.actor.{ Props, ActorSystem }
  2. import org.osgi.framework.BundleContext
  3. import akka.osgi.ActorSystemActivator
  4. class Activator extends ActorSystemActivator {
  5. def configure(context: BundleContext, system: ActorSystem) {
  6. // optionally register the ActorSystem in the OSGi Service Registry
  7. registerService(context, system)
  8. val someActor = system.actorOf(Props[SomeActor], name = "someName")
  9. someActor ! SomeMessage
  10. }
  11. }

The ActorSystemActivator creates the actor system with a class loader that finds resources
(reference.conf files) and classes from the application bundle and all transitive dependencies.

The ActorSystemActivator class is included in the akka-osgi artifact::

  1. <dependency>
  2. <groupId>com.typesafe.akka</groupId>
  3. <artifactId>akka-osgi_@binVersion@</artifactId>
  4. <version>@version@</version>
  5. </dependency>

Sample

A complete sample project is provided in akka-sample-osgi-dining-hakkers.