DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Culture and Methodologies
  3. Career Development
  4. Akka Scheduler: An Alternative to Akka Quartz?

Akka Scheduler: An Alternative to Akka Quartz?

In the Java world, Quartz is somewhat of a standard for scheduling jobs. In this post, though, we take a look at using Akka Scheduler as a possible alternative.

Deepak Mehra user avatar by
Deepak Mehra
·
Feb. 13, 17 · Tutorial
Like (4)
Save
Tweet
Share
16.15K Views

Join the DZone community and get the full member experience.

Join For Free

In this blog, we will be scheduling jobs based on time using Akka Scheduler. The jobs will be scheduled based on IST (Indian Standard Time). It is basically an alternative to Quartz Scheduler to some extent, which is also used to schedule the time-based jobs. We can schedule jobs at any point of time, including scheduling jobs based on IST or any other time zone, say UTC, for example.

Let’s say you want your job to be scheduled at 9:00:00 AM IST every day. What would you do? Scheduling such a job using the normal Akka Scheduler is not easy. So what I have done is
written an extra function so that our normal Akka Scheduler acts as a Quartz Scheduler, but only time-based — not calendar-based. With the example I am going to share, you will be able to schedule time-based jobs for almost any time zone. You just need to mention the which one. In this example, I am focusing on IST (Indian Standard Time) but you can obviously tweak it.

Scheduling Our Example

Let's say our task is to send an email every day at 09:00:00 AM.

Akka Quartz is one way to do it, but if you can play around with the normal Akka Scheduler, why mess around with that?

So, let’s head over to the code and see how can we accomplish it.

Here is the Schedule Actor Class:

import akka.actor.Actor

class ScheduleActor extends Actor {
    import ScheduleActor._
    var count = 1
        //I am using var over here because it's a simple example to increment count. However you should prefer immutability wherever possible.
    def receive: PartialFunction[Any, Unit] = {
        case IncrementNumber => count += 1
        println(count)
    }
}

/**
 * Created by deepak on 22/1/17.
 */
object ScheduleActor {
    case object IncrementNumber
}


And here is the implementation for the job.

import java.text.SimpleDateFormat
import java.util. {
    Date,
    TimeZone
}

import scala.concurrent.duration._

import ScheduleActor.IncrementNumber
import akka.actor. {
    ActorSystem,
    Props
}

/**
 * Created by deepak on 22/1/17.
 */
object ScheduleJob extends App {

    val system = ActorSystem("SchedulerSystem")
    val schedulerActor = system.actorOf(Props(classOf[ScheduleActor]),
        "Actor")
    implicit val ec = system.dispatcher
    system.scheduler
        .schedule(calculateInitialDelay().milliseconds, 60. seconds)(
            schedulerActor!IncrementNumber)
        //the first argument in the schedule function is the initial delay 
        //the second argument in the schedule function is the interval
    def calculateInitialDelay(): Long = {
            val now = new Date()
            val sdf = new SimpleDateFormat("HH:mm:ss")
            sdf.setTimeZone(TimeZone.getTimeZone("IST"))
            val time1 = sdf.format(now)
            val time2 = "00:00:00" //this is where we provide the time(IST)
            for example I want the job scheduled at 9 PM IST I would replace 00: 00: 00
            with 21: 00: 00
            val format = new SimpleDateFormat("HH:mm:ss")
            val date1 = format.parse(time1)
            val date2 = format.parse(time2)
            val timeDifference = date2.getTime() - date1.getTime()
            val calculatedTime =
            if (timeDifference < 0)
                (Constant.DAYHOURS + timeDifference)
            else timeDifference
                // val modifiedDate = projectDbService.getModifiedDate("sumit")
            calculatedTime
        }
        //Calculate initial delay method basically triggers the job at the IST 
    time provided above.
}


That’s all! You are all set to schedule time-based jobs without using Quartz Scheduler.

If you're interested, the full code is available on GitHub. If you find any challenges, let me know in the comments. If you enjoyed this post, I’d be very grateful if you’d help it spread.K eep smiling, keep coding! Cheers!

job scheduling Akka (toolkit) Quartz (scheduler) career

Published at DZone with permission of Deepak Mehra, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Configure Kubernetes Health Checks
  • Important Takeaways for PostgreSQL Indexes
  • Reconciling Java and DevOps with JeKa
  • How We Solved an OOM Issue in TiDB with GOMEMLIMIT

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: