1
votes

I want to add a Hazelcast Map`

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

    }

}

I am attaching map listner like below

addEntryListener(new appListner(), true)

I get error during runtime saying You cannot create an instance of [appListener] explicitly using the constructor (new).You have to use one of the 'actorOf' factory methods to create a new actor.

How do i now use actorOf for attaching appListner?

1

1 Answers

1
votes

You can not do that with your current code arch.

You are using a observer pattern here. Every time when there is an entry added, you want to call entryAdded in class appListner extends Actor. But unfortunately, the function in actor can never be directly called by outside by AKKA design. So you need to change your arch.

One pseudo logic maybe like follows, you need to change to workable code, just afford a piece of thought.

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)