2009-12-21 09:12:26 +01:00
|
|
|
/**
|
2009-12-27 16:01:53 +01:00
|
|
|
* Copyright (C) 2009-2010 Scalable Solutions AB <http://scalablesolutions.se>
|
2009-12-21 09:12:26 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package se.scalablesolutions.akka.remote
|
|
|
|
|
|
|
|
|
|
import se.scalablesolutions.akka.Config.config
|
|
|
|
|
import se.scalablesolutions.akka.config.ScalaConfig._
|
2010-01-02 11:03:06 +01:00
|
|
|
import se.scalablesolutions.akka.serialization.Serializer
|
|
|
|
|
import se.scalablesolutions.akka.actor.{Supervisor, SupervisorFactory, Actor, ActorRegistry}
|
2010-01-14 23:05:26 +01:00
|
|
|
import se.scalablesolutions.akka.util.Logging
|
2009-12-26 22:14:06 +01:00
|
|
|
import scala.collection.immutable.{Map, HashMap}
|
2009-12-21 09:12:26 +01:00
|
|
|
|
|
|
|
|
/**
|
2009-12-26 22:14:06 +01:00
|
|
|
* Interface for interacting with the Cluster Membership API.
|
|
|
|
|
*
|
2009-12-21 09:12:26 +01:00
|
|
|
* @author Viktor Klang
|
|
|
|
|
*/
|
|
|
|
|
trait Cluster {
|
|
|
|
|
def name: String
|
|
|
|
|
|
|
|
|
|
def registerLocalNode(hostname: String, port: Int): Unit
|
|
|
|
|
|
|
|
|
|
def deregisterLocalNode(hostname: String, port: Int): Unit
|
|
|
|
|
|
|
|
|
|
def relayMessage(to: Class[_ <: Actor], msg: AnyRef): Unit
|
|
|
|
|
|
2009-12-26 22:14:06 +01:00
|
|
|
def lookup[T](pf: PartialFunction[RemoteAddress, T]): Option[T]
|
2009-12-21 09:12:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-26 22:14:06 +01:00
|
|
|
* Base class for cluster actor implementations.
|
2009-12-21 09:12:26 +01:00
|
|
|
*/
|
|
|
|
|
abstract class ClusterActor extends Actor with Cluster {
|
|
|
|
|
val name = config.getString("akka.remote.cluster.name") getOrElse "default"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-26 22:14:06 +01:00
|
|
|
* A singleton representing the Cluster.
|
|
|
|
|
* <p/>
|
|
|
|
|
* Loads a specified ClusterActor and delegates to that instance.
|
2009-12-21 09:12:26 +01:00
|
|
|
*/
|
2010-01-14 23:05:26 +01:00
|
|
|
object Cluster extends Cluster with Logging {
|
2009-12-26 22:14:06 +01:00
|
|
|
private[remote] sealed trait ClusterMessage
|
|
|
|
|
private[remote] case class Node(endpoints: List[RemoteAddress]) extends ClusterMessage
|
|
|
|
|
private[remote] case class RelayedMessage(actorClassFQN: String, msg: AnyRef) extends ClusterMessage
|
2009-12-21 09:12:26 +01:00
|
|
|
|
2010-01-14 23:05:26 +01:00
|
|
|
private[remote] val clusterActor: Option[ClusterActor] = {
|
|
|
|
|
val name = config.getString("akka.remote.cluster.actor","not defined")
|
|
|
|
|
try {
|
|
|
|
|
val a = Class.forName(name).newInstance.asInstanceOf[ClusterActor]
|
|
|
|
|
a.start
|
|
|
|
|
Some(a)
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
case e => log.error(e,"Couldn't load Cluster provider: [%s]",name)
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-12-21 09:12:26 +01:00
|
|
|
|
2010-01-02 11:03:06 +01:00
|
|
|
private[remote] val supervisor: Option[Supervisor] = if (clusterActor.isDefined) {
|
|
|
|
|
val sup = SupervisorFactory(
|
|
|
|
|
SupervisorConfig(
|
|
|
|
|
RestartStrategy(OneForOne, 5, 1000, List(classOf[Exception])),
|
|
|
|
|
Supervise(clusterActor.get, LifeCycle(Permanent)) :: Nil)
|
|
|
|
|
).newInstance
|
|
|
|
|
sup.start
|
|
|
|
|
Some(sup)
|
|
|
|
|
} else None
|
|
|
|
|
|
2009-12-26 22:14:06 +01:00
|
|
|
private[remote] lazy val serializer: Serializer = {
|
|
|
|
|
val className = config.getString("akka.remote.cluster.serializer", Serializer.Java.getClass.getName)
|
|
|
|
|
Class.forName(className).newInstance.asInstanceOf[Serializer]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def name = clusterActor.map(_.name).getOrElse("No cluster")
|
2009-12-21 09:12:26 +01:00
|
|
|
|
2009-12-26 22:14:06 +01:00
|
|
|
def lookup[T](pf: PartialFunction[RemoteAddress, T]): Option[T] = clusterActor.flatMap(_.lookup(pf))
|
2009-12-21 09:12:26 +01:00
|
|
|
|
2010-01-02 11:03:06 +01:00
|
|
|
def registerLocalNode(hostname: String, port: Int): Unit = clusterActor.foreach(_.registerLocalNode(hostname, port))
|
2009-12-21 09:12:26 +01:00
|
|
|
|
2010-01-02 11:03:06 +01:00
|
|
|
def deregisterLocalNode(hostname: String, port: Int): Unit = clusterActor.foreach(_.deregisterLocalNode(hostname, port))
|
2009-12-21 09:12:26 +01:00
|
|
|
|
2010-01-02 11:03:06 +01:00
|
|
|
def relayMessage(to: Class[_ <: Actor], msg: AnyRef): Unit = clusterActor.foreach(_.relayMessage(to, msg))
|
|
|
|
|
|
|
|
|
|
def shutdown = supervisor.foreach(_.stop)
|
2009-12-21 09:12:26 +01:00
|
|
|
}
|