Ingress to the Monix-Execution: Scheduler

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:-

import scala.concurrent.ExecutionContext
trait Scheduler extends ExecutionContext
{
//
}

view raw
MonixScheduler.scala
hosted with ❤ by GitHub

Schedule With a Delay

To schedule with a delay let say after 2 seconds it must be run:-

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)
}

view raw
MonixScheduler.scala
hosted with ❤ by GitHub

Scala-friendly Scheduler code:-

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)
}

view raw
MonixScheduler.scala
hosted with ❤ by GitHub

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:-

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)
}

view raw
MonixScheduler.scala
hosted with ❤ by GitHub

Scala-friendly Scheduler code:-

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)
}

view raw
MonixScheduler.scala
hosted with ❤ by GitHub

Cancel Scheduler

We can cancel the scheduler any time we want to by using:-

delayOnce.cancel()
fixedDelay.cancel()

view raw
MonixScheduler.scala
hosted with ❤ by GitHub

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.

%d bloggers like this: