title: Ways for heartbeat message
body: <p>I am trying to set a heartbeat over a network, i.e. having an actor send a message to the network on a fixed period of time. I would like to know if you have any better solution than the one I used below as I feel is pretty ugly, considering synchronisation contraints.</p>
  
  <pre><code>import akka.actor._
  import akka.actor.Actor
  import akka.actor.Props
  import akka.actor.ScalaActorRef
  import akka.pattern.gracefulStop
  import akka.util._
  import java.util.Calendar
  import java.util.concurrent._
  import java.text.SimpleDateFormat
  import scala.Array._
  import scala.concurrent._
  import scala.concurrent.duration._
  import scala.concurrent.ExecutionContext.Implicits.global
  
  sealed trait Message
  case class Information() extends Message//does really need to be here?
  case class StartMessage() extends Message
  case class HeartbeatMessage() extends Message
  case class StopMessage() extends Message
  case class FrequencyChangeMessage(
      f: Int
  ) extends Message
  
  class Gps extends Actor {
      override def preStart() {
          val child = context.actorOf(Props(new Cadencer(500)), name = "cadencer")
      }
      def receive = {
          case "beat" =&gt;
              //TODO
          case _      =&gt;
              println("gps: wut?")
      }
  }
  
  class Cadencer(p3riod: Int) extends Actor {
      var period: Int = _
      var stop: Boolean = _
      override def preStart() {
          period = p3riod
          stop = false
          context.system.scheduler.scheduleOnce(period milliseconds, self, HeartbeatMessage)
      }
      def receive = {
          case StartMessage =&gt;
              stop = false
              context.system.scheduler.scheduleOnce(period milliseconds, self, HeartbeatMessage)
          case HeartbeatMessage =&gt;
              if (false == stop) {
                  context.system.scheduler.scheduleOnce(0 milliseconds, context.parent, "beat")
                  context.system.scheduler.scheduleOnce(period milliseconds, self, HeartbeatMessage)
              }
          case StopMessage =&gt;
              stop = true
          case FrequencyChangeMessage(f) =&gt;
              period = f
          case _  =&gt;
              println("wut?\n")
              //throw exception
      }
  }
  
  object main extends App {
      val system = akka.actor.ActorSystem("mySystem")
      val gps = system.actorOf(Props[Gps], name = "gps")
  }
  </code></pre>
  
  <p>What I called <code>cadencer</code> here sends to a target <code>actor</code> and to itself an <code>HeartbeatMessage</code> ; to itself to transmit the order to resend one after a given amount of time, and thus going on with the process till a <code>StopMessage</code> (flipping the <code>stop</code> to <code>true</code>). Good?</p>
  
  <p>Is even having a separated <code>actor</code> efficient rather than having it within a greater one?</p>