简体   繁体   中英

Scheduled Mysql query akka http

I am new to the hole scala/akka ecosistem.
I have a akka-http route defined like:

object RestServiceApp extends CassandraConnector with Directives with JsonSupport {
  def main(args: Array[String]) {
    val config = ConfigFactory.load()
    implicit val actorSystem = ActorSystem("system", config)
    implicit val actorMaterializer = ActorMaterializer()

    var myFilterList = Seq(3L,32L,55L)

    val route: Route = {
      get {
        path("foo") {
          complete( foo().filter(element => myFilterList.contains(element) )
        }
    }
}

foo() is a function that returns elements

I filter that elements and return the list

I want to:

  1. Update the myFilterList every M minutes
  2. For doing that I have to make a Mysql query

I don't care about the performance of the query, I care about being able to continue responding to http request while the new myFilterList is being loaded.

How can I achieve this?

Thanks!

You could try making a simple cache:

object MyRefreshableList {
  private val updateInterval: Int = 600000 // 600000 millis == 10 minutes; you could use Java 8 Date/Time API or Joda Time methods here
  private var lastUpdateTime: Long = System.currentTimeMillis()
  private var cachedList: List[Long] = getList

  def value: List[Long] = cachedList // You would use that during your filtering

  // You would use that before your filtering
  def tryToRefresh() = {
    val currentTime = System.currentTimeMillis()
    if (currentTime > lastUpdateTime + updateInterval) {
      lastUpdateTime = currentTime
      refresh()
    }
  }

  private def refresh(): Unit = {
    cachedList = getList
  }

  private def getList: List[Long] = ??? // Make an actual SQL query here
}

Usage example with your code:

val route: Route = {
  get {
    path("foo") {
      complete {
        MyRefreshableList.tryToRefresh()
        foo().filter(element => MyRefreshableList.value.contains(element)
      }
    }
  }
}

If you would have many collections which require refreshing, you could extract a Refreshable trait out of that code.

I have added some code to the end that every M minutes makes a query to the mysql database and updates de Set

val M = 30
actorSystem.scheduler.schedule(50 milliseconds, M minutes) {
      val myNewFilterList = makeMysqlQuery()
      myFilterList = myNewFilterList 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM