score:2
Accepted answer
i think you want something like this:
var result = 0
val cancellable = scheduler.schedule(0.seconds, 10.seconds) {
result = result + 1
}
you can use the cancellable
value to stop the schedule when it is no longer needed.
here is the full code:
def initializewebserver(interface: string, port: int) = {
var result = 0
val canceallable = context.system.scheduler.schedule(0.seconds, 10.seconds) {
result = result + 1
}
val route: route =
concat(
get {
path("getresult") {
complete(result)
}
}
)
val bindingfuture = http().bindandhandle(route, interface, port.toint)
println(s"server online at http://$interface:$port/")
coordinatedshutdown(system).addjvmshutdownhook({
bindingfuture
.flatmap(_.unbind())
})
}
score:0
you could model this by using an actor:
class stateholder extends actor {
val state = ???
context.system.scheduler.schedule(0.second, 10.second, self, dotask)
def receive = {
case dotask => /* update state */
case getstate => /* fetch state */
}
}
the scheduler
will cause the actor to send itself an dotask
message and then you can fetch the current state by sending a message.
Source: stackoverflow.com
Related Query
- How to return value from scheduler
- How can I get a return value from ScalaTest indicating test suite failure?
- How to optionally return value from map function
- How to return callback parameter value from its caller?
- How to return a value from a Scala def
- How to return integer value from notebook in adf pipeline
- how to return value from a if block in scala
- How to get return value from if else block
- Scala, ZIO - how to return value from Task?
- How to return value from onComplete Success case with parallel queries with spark and scala to build DataFrame?
- How to return a yielded value from a for-loop in scala?
- How to return a value from the while/for loop in Scala
- How do I return a value from a scala function?
- Scala - How to return simple raw type value from flat map?
- How to return a value from receive method in scala actor model
- Scala: How to return column name and value from a dataframe
- How to use return value from function returning Future(Either[A, B]) in scala?
- How return play.api.mvc.AnyContent value from Blob column?
- how to filter out a null value from spark dataframe
- How are Scala collections able to return the correct collection type from a map operation?
- how to remove key value from map in scala
- how to keep return value when logging in scala
- Scala return value from onComplete
- How map only left value from scala Either?
- How to return null from a generic function in Scala?
- scala main returns unit. How to set program's return value
- How can Scala actors return a value in response to a message?
- How to extract a value from a Vector in a column of a Spark Dataframe
- How to return optional information from methods?
- How To Access access Case class field Value from String name of the field
More Query from same tag
- How to get different URLs pointing to same servlet?
- How do I add a custom unit of measure to Squants
- Used Scala with Lift-Json working fine but why performance is varying?
- How to add the incremental date value with respect to first row value in spark dataframe
- SparkContext: error initializing SparkContext. java.net.UnknownHostException: master
- Concatenating multiple lists in Scala
- Trying to compile a project with Scalatra and failing
- Importing functions from outside a project in scala [sbt]
- Akka Streams, timeout if time between finished processing one element until next processing begins
- Running Spark-Task with SparkLauncher
- How to exclude java source files in doc task?
- Play Framework 2.0 form helper: from select to List<T>
- scala pattern matching on (Try,Try)
- Want to generate unique Ids as value changes from previous row using scala
- Why Unit is a supertype of anything else?
- Modify json field type via circe
- How to extract value from Scala cats IO
- How can I pretty print a wrappedarray in Zeppelin/Spark/Scala?
- Flink Scala - Extending WindowFunction
- Kafka: update a key when there is no update in x amount of time
- How to find the time spent per future in Scala?
- Add a java.library.path to /usr/lib/jni for scala with maven and ubuntu
- How to use a wildcard for a higher-kinded type in Scala?
- How to retrieve derived classes as is from a Map?
- How to produce a message using a Publisher(in reactive-kafka)?
- spark parse json field and match to different case class
- How to provide scala object's with classes that have dependancy injection?
- Fill scala column with nulls
- Typesafe/Hocon config: variable substitution: reference path
- scoverage fails due to unresolved dependency path