私はakkaスケジューラの使用法のいくつかの例を探しています。私は、あるデータベースからデータを取得するための1つのアクター(dataProducerと呼ぶ)を実装しています。私は、5秒間隔でdataProducerアクターをポールするスケジューラーアクターを1つ作成したいと思います。また、データ検索にスケジューラ間隔よりも時間がかかる場合は、ケースをどのように処理するか。スケジューラのアクタのscheduleOnceメソッドがこれを処理しますか?ここでakkaスケジューラを使用したデータプッシュ
は
import java.util.concurrent.{Executors, TimeUnit}
import akka.actor.{Actor, Cancellable, Props}
import scala.concurrent.ExecutionContext
class SchedulerActor(interval: Long) extends Actor with LogF{
implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(100))
private var scheduler: Cancellable = _
override def preStart(): Unit = {
import scala.concurrent.duration._
scheduler = context.system.scheduler.schedule(
initialDelay = 0 seconds,
interval = FiniteDuration(interval, TimeUnit.SECONDS),
receiver = self,
message = FetchData
)
}
override def postStop(): Unit = {
scheduler.cancel()
}
def receive = {
case FetchData =>
logger.debug("Fetch Data")
sender() ! "Data Fetched!!!" //here I'll call dataProducer API
true
case unknown =>
throw new RuntimeException("ERROR: Received unknown message [" + unknown + "], can't handle it")
}
}
object SchedulerActor {
def props(interval: Long): Props = Props(new SchedulerActor(interval))
}
sealed trait FetchDataMessage
case object FetchData extends FetchDataMessage
意味のある回答を得たい場合は、これまでに試したことを示すことをおすすめします。 – hasumedic
私の質問が更新されました... – Abhay