Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package net.gutefrage
import com.twitter.app.App
import com.twitter.conversions.time._
import com.twitter.finagle.{ThriftMux}
import com.twitter.finagle.util.DefaultTimer
import com.twitter.util.{Await, Future}
import net.gutefrage.temperature.thrift._r
/**
* Sends random temperature changes to the temperature service
*/
object TemperatureSensor extends App {
def main(): Unit = {
// create a temperature service client. Use services announced in zookeeper under /service/temperature
val client: TemperatureService.FutureIface = ThriftMux.client.newIface[TemperatureService.FutureIface](
dest = "zk!127.0.0.1:2181/service/temperature",
label = "temperature-sensor"
)
// infinite loop, sending data every second
implicit val timer = DefaultTimer.twitter
val randomTemp = new java.util.Random()
def sendLoop: Future[Unit] = {
val datum = TemperatureDatum(randomTemp.nextInt(40) - 10, System.currentTimeMillis / 1000)
for {
_ <- Future.sleep(1.second)
_ <- client.add(datum)
_ <- sendLoop
} yield ()
}
Await.ready(sendLoop)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment