I could not log the `ClusterClientUp` event when my cluster client connect 
receptionist.

Here is my cluster code:

class PingPongActor extends Actor {

  ClusterClientReceptionist(context.system).registerService(self)

  override def receive = {
    case "Ping" => sender() ! "Pong"
  }
}


Listener trying to log on cluster client events:

class ReceptionistListener(targetReceptionist: ActorRef) extends Actor with 
ActorLogging {

  override def preStart(): Unit =
    targetReceptionist ! SubscribeClusterClients

  def receive: Receive =
    receiveWithClusterClients(Set.empty)
  def receiveWithClusterClients(clusterClients: Set[ActorRef]): Receive = {
    case ClusterClients(cs) =>
      cs.foreach{c => log.info(s"Cluster Client: ${c.toString()}")} 
      context.become(receiveWithClusterClients(cs))
    // Now do something with the up-to-date "c"
    case ClusterClientUp(c) =>
      log.info(s"Cluster Client Up: ${c.toString()}")
      context.become(receiveWithClusterClients(clusterClients + c))
    // Now do something with an up-to-date "clusterClients + c"
    case ClusterClientUnreachable(c) =>
      log.info(s"Cluster Client Unreachable: ${c.toString()}")
      context.become(receiveWithClusterClients(clusterClients - c))
    // Now do something with an up-to-date "clusterClients - c"
  }
}


Cluster's main application code:

object Main {

  val role = "pinger"

  def main(args: Array[String]): Unit = {

    val port = if (args.isEmpty) "0" else args(0)

    val config = ConfigFactory.parseString(s"akka.cluster.roles=[$role]")
      .withFallback(ConfigFactory.parseString("akka.remote.netty.tcp.port=" + 
port))
      .withFallback(ConfigFactory.load())

    val pingPongSystem = ActorSystem("PingPongSystem", config)

    val pingActorRef = pingPongSystem.actorOf(Props[PingPongActor], "pingActor")
    pingPongSystem.actorOf(Props(classOf[ReceptionistListener], pingActorRef))
  }
}


`application.conf` for cluster:

akka {

  actor.provider = "akka.cluster.ClusterActorRefProvider"

  remote.netty.tcp.port=0
  remote.netty.tcp.hostname=127.0.0.1

  cluster {
    seed-nodes = [
      "akka.tcp://[email protected]:2551",
      "akka.tcp://[email protected]:2552"]

    auto-down-unreachable-after = 10s
  }

  extensions = ["akka.cluster.client.ClusterClientReceptionist"]

}


And here is my cluster client code:

class PingerClientActor (clusterClient: ActorRef) extends Actor with 
ActorLogging {

  implicit val timeout = Timeout(5 seconds)

  def receive = {
    case "Hello" =>
      (clusterClient ? ClusterClient.Send("/user/pingActor", "Ping", 
false)).mapTo[String] map { r =>
        log.info(s"Returned: $r")
      }
  }
}


Client's main application code:

object Main {

  def main(args: Array[String]): Unit = {

    val port = if (args.isEmpty) "0" else args(0)
    val conf = ConfigFactory.parseString("akka.remote.netty.tcp.port=" + port)
                .withFallback(ConfigFactory.load())

    val clientSystem = ActorSystem("NotificationClient", conf)
    val initialContacts = 
immutableSeq(conf.getStringList("contact-points")).map {
      case AddressFromURIString(addr) => RootActorPath(addr) / "system" / 
"receptionist"
    }.toSet

    val clusterClient = clientSystem.actorOf(ClusterClient.props(
      ClusterClientSettings(clientSystem).withInitialContacts(initialContacts)
    ), "pingerClusterClient")

    Thread.sleep(2000)
    val notificationClientActor = 
clientSystem.actorOf(Props(classOf[PingerClientActor], clusterClient), 
"pingerClientActor")
    notificationClientActor ! "Hello"
  }
}


configuration file:

akka {

  actor.provider = "akka.cluster.ClusterActorRefProvider"

  remote.netty.tcp.port=0
  remote.netty.tcp.hostname=127.0.0.1

}

contact-points = [
  "akka.tcp://[email protected]:2551",
  "akka.tcp://[email protected]:2552"
]


Using this code, I can start my cluster system and the cluster client can 
also log the `Pong` returned response. Referring to this Cluster Client 
Events <http://doc.akka.io/docs/akka/2.4/scala/cluster-client.html#Events>, 
I assumed I can subscribe to the cluster client connect events. But, there 
are no log in `ReceptionistListener` when client registers in cluster. 
Please correct me, if I misunderstood the documentation and any help to 
achieve this will be highly appreciated.

Thanks


-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to