本文整理汇总了Scala中akka.cluster.ClusterEvent.CurrentClusterState类的典型用法代码示例。如果您正苦于以下问题:Scala CurrentClusterState类的具体用法?Scala CurrentClusterState怎么用?Scala CurrentClusterState使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CurrentClusterState类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: MetricsListener
//设置package包名称以及导入依赖的类
package sample.cluster.factorial
import akka.actor.ActorLogging
import akka.actor.Actor
//#metrics-listener
import akka.cluster.Cluster
import akka.cluster.ClusterEvent.ClusterMetricsChanged
import akka.cluster.ClusterEvent.CurrentClusterState
import akka.cluster.NodeMetrics
import akka.cluster.StandardMetrics.HeapMemory
import akka.cluster.StandardMetrics.Cpu
class MetricsListener extends Actor with ActorLogging {
val selfAddress = Cluster(context.system).selfAddress
// subscribe to ClusterMetricsChanged
// re-subscribe when restart
override def preStart(): Unit =
Cluster(context.system).subscribe(self, classOf[ClusterMetricsChanged])
override def postStop(): Unit =
Cluster(context.system).unsubscribe(self)
def receive = {
case ClusterMetricsChanged(clusterMetrics) =>
clusterMetrics.filter(_.address == selfAddress) foreach { nodeMetrics =>
logHeap(nodeMetrics)
logCpu(nodeMetrics)
}
case state: CurrentClusterState => // ignore
}
def logHeap(nodeMetrics: NodeMetrics): Unit = nodeMetrics match {
case HeapMemory(address, timestamp, used, committed, max) =>
log.info("Used heap: {} MB", used.doubleValue / 1024 / 1024)
case _ => // no heap info
}
def logCpu(nodeMetrics: NodeMetrics): Unit = nodeMetrics match {
case Cpu(address, timestamp, Some(systemLoadAverage), cpuCombined, processors) =>
log.info("Load: {} ({} processors)", systemLoadAverage, processors)
case _ => // no cpu info
}
}
//#metrics-listener
示例2: MetricsListener
//设置package包名称以及导入依赖的类
package sample.cluster.factorial
import akka.actor.ActorLogging
import akka.actor.Actor
import akka.cluster.Cluster
import akka.cluster.metrics.ClusterMetricsEvent
import akka.cluster.metrics.ClusterMetricsChanged
import akka.cluster.ClusterEvent.CurrentClusterState
import akka.cluster.metrics.NodeMetrics
import akka.cluster.metrics.StandardMetrics.HeapMemory
import akka.cluster.metrics.StandardMetrics.Cpu
import akka.cluster.metrics.ClusterMetricsExtension
class MetricsListener extends Actor with ActorLogging {
val selfAddress = Cluster(context.system).selfAddress
val extension = ClusterMetricsExtension(context.system)
// Subscribe unto ClusterMetricsEvent events.
override def preStart(): Unit = extension.subscribe(self)
// Unsubscribe from ClusterMetricsEvent events.
override def postStop(): Unit = extension.unsubscribe(self)
def receive = {
case ClusterMetricsChanged(clusterMetrics) =>
clusterMetrics.filter(_.address == selfAddress) foreach { nodeMetrics =>
logHeap(nodeMetrics)
logCpu(nodeMetrics)
}
case state: CurrentClusterState => // Ignore.
}
def logHeap(nodeMetrics: NodeMetrics): Unit = nodeMetrics match {
case HeapMemory(address, timestamp, used, committed, max) =>
log.info("Used heap: {} MB", used.doubleValue / 1024 / 1024)
case _ => // No heap info.
}
def logCpu(nodeMetrics: NodeMetrics): Unit = nodeMetrics match {
case Cpu(address, timestamp, Some(systemLoadAverage), cpuCombined, cpuStolen, processors) =>
log.info("Load: {} ({} processors)", systemLoadAverage, processors)
case _ => // No cpu info.
}
}
示例3: MetricsListener
//设置package包名称以及导入依赖的类
package sample.cluster.factorial
//#metrics-listener
import akka.actor.ActorLogging
import akka.actor.Actor
import akka.cluster.Cluster
import akka.cluster.metrics.ClusterMetricsEvent
import akka.cluster.metrics.ClusterMetricsChanged
import akka.cluster.ClusterEvent.CurrentClusterState
import akka.cluster.metrics.NodeMetrics
import akka.cluster.metrics.StandardMetrics.HeapMemory
import akka.cluster.metrics.StandardMetrics.Cpu
import akka.cluster.metrics.ClusterMetricsExtension
class MetricsListener extends Actor with ActorLogging {
val selfAddress = Cluster(context.system).selfAddress
val extension = ClusterMetricsExtension(context.system)
// Subscribe unto ClusterMetricsEvent events.
override def preStart(): Unit = extension.subscribe(self)
// Unsubscribe from ClusterMetricsEvent events.
override def postStop(): Unit = extension.unsubscribe(self)
def receive = {
case ClusterMetricsChanged(clusterMetrics) =>
clusterMetrics.filter(_.address == selfAddress) foreach { nodeMetrics =>
logHeap(nodeMetrics)
logCpu(nodeMetrics)
}
case state: CurrentClusterState => // Ignore.
}
def logHeap(nodeMetrics: NodeMetrics): Unit = nodeMetrics match {
case HeapMemory(address, timestamp, used, committed, max) =>
log.info("Used heap: {} MB", used.doubleValue / 1024 / 1024)
case _ => // No heap info.
}
def logCpu(nodeMetrics: NodeMetrics): Unit = nodeMetrics match {
case Cpu(address, timestamp, Some(systemLoadAverage), cpuCombined, cpuStolen, processors) =>
log.info("Load: {} ({} processors)", systemLoadAverage, processors)
case _ => // No cpu info.
}
}
//#metrics-listener
示例4: BackendActor
//设置package包名称以及导入依赖的类
package com.example
import akka.actor.{Actor, ActorLogging, RootActorPath}
import akka.cluster.ClusterEvent.{CurrentClusterState, MemberUp}
import akka.cluster.{Cluster, Member, MemberStatus}
class BackendActor extends Actor with ActorLogging {
val cluster = Cluster(context.system)
override def preStart(): Unit = {
cluster.subscribe(self, classOf[MemberUp])
}
override def postStop(): Unit = {
cluster.unsubscribe(self)
}
override def receive: Receive = {
case state: CurrentClusterState =>
log.info("receive current cluster state")
state.members.filter(_.status == MemberStatus.Up).foreach(register)
case MemberUp(member) =>
log.info("register new member")
register(member)
case string: String =>
log.info(string)
}
def register(member: Member): Unit = {
if (member.hasRole("frontend")) {
context.actorSelection(RootActorPath(member.address) / "user" / "Frontend") ! "Register"
}
}
}
示例5: MetricsListener
//设置package包名称以及导入依赖的类
package com.example.cluster.metrics
import akka.actor.{Actor, ActorLogging}
import akka.cluster.Cluster
import akka.cluster.ClusterEvent.CurrentClusterState
import akka.cluster.metrics.StandardMetrics.{Cpu, HeapMemory}
import akka.cluster.metrics.{ClusterMetricsChanged, ClusterMetricsExtension, NodeMetrics}
class MetricsListener extends Actor with ActorLogging {
val selfAddress = Cluster(context.system).selfAddress
val extension = ClusterMetricsExtension(context.system)
// Subscribe unto ClusterMetricsEvent events.
override def preStart(): Unit = extension.subscribe(self)
// Unsubscribe from ClusterMetricsEvent events.
override def postStop(): Unit = extension.unsubscribe(self)
override def receive: Receive = {
case ClusterMetricsChanged(clusterMetrics) =>
clusterMetrics.filter(_.address == selfAddress) foreach { nodeMetrics =>
logHeap(nodeMetrics)
logCpu(nodeMetrics)
}
case state: CurrentClusterState => // Ignore.
}
def logHeap(nodeMetrics: NodeMetrics): Unit = nodeMetrics match {
case HeapMemory(address, timestamp, used, committed, max) =>
log.info("Used heap: {} MB", used.doubleValue / 1024 / 1024)
case _ => // No heap info.
}
def logCpu(nodeMetrics: NodeMetrics): Unit = nodeMetrics match {
case Cpu(address, timestamp, Some(systemLoadAverage), cpuCombined, cpuStolen, processors) =>
log.info("Load: {} ({} processors)", systemLoadAverage, processors)
case _ => // No cpu info.
}
}
示例6: UserListener
//设置package包名称以及导入依赖的类
package de.chasmo.chat
import akka.actor._
import akka.cluster.{Cluster, MemberStatus}
import akka.cluster.ClusterEvent.{CurrentClusterState, MemberEvent, MemberRemoved, MemberUp}
class UserListener extends Actor with ActorLogging {
val cluster = Cluster(context.system)
override def preStart(): Unit =
cluster.subscribe(self, classOf[MemberEvent])
override def postStop(): Unit =
cluster unsubscribe self
var nodes = Set.empty[Address]
def receive = {
case state: CurrentClusterState =>
nodes = state.members.collect {
case m if m.status == MemberStatus.Up => m.address
}
case MemberUp(member) =>
nodes += member.address
log.info("Member is Up: {}. {} nodes in cluster",
member.address, nodes.size)
case MemberRemoved(member, _) =>
nodes -= member.address
log.info("Member is Removed: {}. {} nodes cluster",
member.address, nodes.size)
case _: MemberEvent => // ignore
}
}
object UserListener {
def props: Props = Props(classOf[UserListener])
}
示例7: AlcaudonClient
//设置package包名称以及导入依赖的类
package org.alcaudon.api
import akka.actor.{Actor, ActorLogging, ActorSelection, RootActorPath}
import akka.cluster.ClusterEvent.{CurrentClusterState, MemberUp}
import akka.cluster.{Cluster, Member, MemberStatus}
import org.alcaudon.core.{ActorConfig, DataflowGraph}
object AlcaudonClient {
case class RegisterDataflowJob(dataflow: DataflowGraph)
case object UnknownCoordinator
}
class AlcaudonClient extends Actor with ActorLogging with ActorConfig {
import AlcaudonClient._
val cluster = Cluster(context.system)
override def preStart(): Unit = cluster.subscribe(self, classOf[MemberUp])
override def postStop(): Unit = cluster.unsubscribe(self)
var coordinator: Option[ActorSelection] = None
def receive = receiveCoordinatorNode
def receiveCoordinatorNode: Receive = {
case state: CurrentClusterState =>
val coordinator = state.members
.filter(member =>
member.status == MemberStatus.Up && member.hasRole("coordinator"))
.map(getCoordinatorNodePath)
if (coordinator.size == 1)
context.become(receiveWithCoordinator(coordinator.head))
case MemberUp(member) =>
if (member.hasRole("coordinator"))
context.become(receiveWithCoordinator(getCoordinatorNodePath(member)))
case _ =>
sender() ! UnknownCoordinator
}
def receiveWithCoordinator(coordinator: ActorSelection): Receive = {
case request: RegisterDataflowJob =>
coordinator ! request
}
def getCoordinatorNodePath(member: Member): ActorSelection =
context.actorSelection(
RootActorPath(member.address) / "user" / "coordinator")
}