In the previous blog you learnt about Monix Task. Now lets dive into Monix Scheduler. Akka’s Scheduler that includes pretty heavy dependency whereas Monix Scheduler that provides a common API which is resuable.
For this Scheduler to work first we need to include the dependency which is:-
libraryDependencies += "io.monix" % "monix-execution_2.11" % "2.0-RC10"
Make sure that your Scala version is same as Monix version in your dependency. The Monix Scheduler can be a replacement for Scala’s ExecutionContext as:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.concurrent.ExecutionContext | |
trait Scheduler extends ExecutionContext | |
{ | |
// … | |
} |
Schedule With a Delay
To schedule with a delay let say after 2 seconds it must be run:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.TimeUnit | |
import monix.execution.Scheduler.{global => scheduler} | |
import scala.concurrent.duration._ | |
object MonixScheduler extends App { | |
val delayOnce = scheduler.scheduleOnce( | |
2, TimeUnit.SECONDS, | |
new Runnable { | |
def run(): Unit = { | |
println("Schedule Example 1") | |
} | |
}) | |
Thread.sleep(20000) | |
} |
Scala-friendly Scheduler code:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.TimeUnit | |
import monix.execution.Scheduler.{global => scheduler} | |
import scala.concurrent.duration._ | |
object MonixScheduler extends App { | |
val delayOnce = scheduler.scheduleOnce(2.seconds) { | |
println("Schedule Once Example 2") | |
} | |
Thread.sleep(20000) | |
} |
Schedule Repeatedly
If we want to schedule task repeatedly with an initial delay say 2 seconds and then with a fixed delay of say 3 seconds:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.TimeUnit | |
import monix.execution.Scheduler.{global => scheduler} | |
import scala.concurrent.duration._ | |
object MonixScheduler extends App { | |
val fixedDelay = scheduler.scheduleWithFixedDelay( | |
2, 3, TimeUnit.SECONDS, | |
new Runnable { | |
def run(): Unit = { | |
println("Fixed Delay Example 1") | |
} | |
}) | |
Thread.sleep(20000) | |
} |
Scala-friendly Scheduler code:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.TimeUnit | |
import monix.execution.Scheduler.{global => scheduler} | |
import scala.concurrent.duration._ | |
object MonixScheduler extends App { | |
scheduler.scheduleWithFixedDelay(2.seconds, 3.seconds) { | |
println("Fixed Delay Example 2") | |
} | |
Thread.sleep(20000) | |
} |
Cancel Scheduler
We can cancel the scheduler any time we want to by using:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
delayOnce.cancel() | |
fixedDelay.cancel() |
Thanks For Reading!! 🙂
References:- Monix Official Documentation
Reblogged this on akashsethi24.