Ingress to the Monix-Execution: Scheduler

Table of contents
Reading Time: < 1 minute

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

Monix Scheduler

Written by 

Charmy is a Software Consultant having experience of more than 1.5 years. She is familiar with Object Oriented Programming Paradigms and has familiarity with Technical languages such as Scala, Lagom, Java, Apache Solr, Apache Spark, Apache Kafka, Apigee. She is always eager to learn new concepts in order to expand her horizon. Her hobbies include playing guitar and Sketching.

1 thought on “Ingress to the Monix-Execution: Scheduler1 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading