在 akka 集群框架中使用 Hazelcast MapListner



我想添加一个Hazelcast地图'

class appListner extends Actor with EntryAddedListener[String, String]{
override def entryAdded(event: EntryEvent[String, String]): Unit = {
logger.info("Entry Added:" + event)
// i want to update my local cache using the event.value
updateLocalcache(event.getValue) // which is in turn needs to update the cache implemented using actor
}
}

我正在附加如下所示的地图列表器

addEntryListener(new appListner(), true)

我在运行时收到错误,说您无法使用构造函数(新(显式创建 [appListener] 的实例。您必须使用"actorOf"工厂方法之一来创建新的参与者。

我现在如何使用 actorOf 来附加 appListner?

你不能用你当前的代码架构做到这一点。

您在此处使用的是observer模式。每次添加条目时,您都想在class appListner extends Actor中调用entryAdded。但遗憾的是,Actor 中的函数永远无法通过 AKKA 设计被外部直接调用。所以你需要改变你的足弓。

一个伪逻辑可能如下所示,您需要更改为可行的代码,只需提供一点想法即可。

class CacheImplementedUsingActor extends Actor {
def receive = {
case eventValue => UpdateCacheNowWithEventValue
}
}
class appListner(val cacheActor: ActorRef) with EntryAddedListener[String, String] {
override def entryAdded(event: EntryEvent[String, String]): Unit = {
logger.info("Entry Added:" + event)
// i want to update my local cache using the event.value
updateLocalcache(event.getValue)
}
def updateLocalcache(eventValue: String) {
// which is in turn needs to update the cache implemented using actor
cacheActor ! eventValue
}
}
val system = ActorSystem("mySystem")
val cacheActor = system.actorOf(Props[CacheImplementedUsingActor])
addEntryListener(new appListner(cacheActor), true)

最新更新