const val POLL_INTERVAL = 2.0 data class DataResult(val data : String, val lastRetrieve : LocalDateTime) class PollView : View("Poll App") { val controller : PollController by inject() override val root = vbox { textarea(controller.currentData) { vgrow = Priority.ALWAYS } hbox { button("Start") { disableProperty().bind( controller.stopped.not() ) setOnAction { controller.start() } } button("Stop") { disableProperty().bind( controller.stopped ) setOnAction { controller.stop() } } button("Exit") { setOnAction { Platform.exit() } } spacing = 4.0 padding = Insets(4.0) } padding = Insets(10.0) spacing = 10.0 } } class PollController : Controller() { val api : Rest by inject() val currentData = SimpleStringProperty() val stopped = SimpleBooleanProperty(true) val scheduledService = object : ScheduledService() { init { period = Duration.seconds(POLL_INTERVAL) } override fun createTask() : Task = FetchDataTask() } fun start() { scheduledService.restart() stopped.value = false } fun stop() { scheduledService.cancel() stopped.value = true } inner class FetchDataTask : Task() { override fun call(): DataResult { val htmlBody = api.get("/") // fake "recv" return DataResult(htmlBody.text()!!, LocalDateTime.now()) } override fun succeeded() { println("data updated: " + value.lastRetrieve) this@PollController.currentData.value = value.data } override fun failed() { println("failed retrieval") exception.printStackTrace() } } } class PollApp : App(PollView::class) { val api : Rest by inject() override fun createPrimaryScene(view: UIComponent): Scene = Scene(view.root, 736.0, 414.0) init { api.baseURI = "https://www.bekwam.com" } } fun main(args : Array) { Application.launch( PollApp::class.java ) }