move code to src/test
* so that it compiles and tests pass * fix some additional snip references in getting started
This commit is contained in:
parent
413df8e0f4
commit
59f53e1a22
289 changed files with 45 additions and 45 deletions
|
|
@ -0,0 +1,52 @@
|
|||
package scala.docs.cluster
|
||||
|
||||
import language.postfixOps
|
||||
import scala.concurrent.duration._
|
||||
import akka.actor.Actor
|
||||
import akka.actor.ActorRef
|
||||
import akka.actor.ActorSystem
|
||||
import akka.actor.Props
|
||||
import akka.actor.RootActorPath
|
||||
import akka.cluster.Cluster
|
||||
import akka.cluster.ClusterEvent.CurrentClusterState
|
||||
import akka.cluster.ClusterEvent.MemberUp
|
||||
import akka.cluster.Member
|
||||
import akka.cluster.MemberStatus
|
||||
import com.typesafe.config.ConfigFactory
|
||||
|
||||
//#backend
|
||||
class TransformationBackend extends Actor {
|
||||
|
||||
val cluster = Cluster(context.system)
|
||||
|
||||
// subscribe to cluster changes, MemberUp
|
||||
// re-subscribe when restart
|
||||
override def preStart(): Unit = cluster.subscribe(self, classOf[MemberUp])
|
||||
override def postStop(): Unit = cluster.unsubscribe(self)
|
||||
|
||||
def receive = {
|
||||
case TransformationJob(text) => sender() ! TransformationResult(text.toUpperCase)
|
||||
case state: CurrentClusterState =>
|
||||
state.members.filter(_.status == MemberStatus.Up) foreach register
|
||||
case MemberUp(m) => register(m)
|
||||
}
|
||||
|
||||
def register(member: Member): Unit =
|
||||
if (member.hasRole("frontend"))
|
||||
context.actorSelection(RootActorPath(member.address) / "user" / "frontend") !
|
||||
BackendRegistration
|
||||
}
|
||||
//#backend
|
||||
|
||||
object TransformationBackend {
|
||||
def main(args: Array[String]): Unit = {
|
||||
// Override the configuration of the port when specified as program argument
|
||||
val port = if (args.isEmpty) "0" else args(0)
|
||||
val config = ConfigFactory.parseString(s"akka.remote.netty.tcp.port=$port").
|
||||
withFallback(ConfigFactory.parseString("akka.cluster.roles = [backend]")).
|
||||
withFallback(ConfigFactory.load())
|
||||
|
||||
val system = ActorSystem("ClusterSystem", config)
|
||||
system.actorOf(Props[TransformationBackend], name = "backend")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue