Dropwizard Sundial Scheduler Tutorial

Dropwizard Sundial Scheduler Tutorial


        The functionality of scheduling jobs is frequently used in many applications today. Sundial is a quartz fork. dropwizard-sundial library makes it very easy to configure and integrate a job scheduler into a Dropwizard application. This blog post will give you a comprehensive overview of this functionality.

        Sundial is a Lightweight Job Scheduling Framework for Java. Sundial makes adding scheduled jobs to your Java application very simple. All we have to do is define jobs, define triggers, and start the Sundial scheduler. For more details about it please refer to: Sundial GitHub page.

        Dropwizard is one of the most popular and most used frameworks for building microservices.
I am assuming that you have a Basic Knowledge of Dropwizard and have a Basic Dropwizard Application running in your machine. If not, please check my blog post on Basic Dropwizard Application by going to the link: Dropwizard Tutorial.

Requirements to Run the Application:
  1. Java
  2. Maven
  3. IDE of your choice
Once you have a Basic Dropwizard Application up and running in your machine, here are the additional steps required to add the job scheduling functionality.

Step 1: Maven Dependencies to be added in pom.xml

Add dropwizard-sundial library to the project by using the following dependency in the pom.xml:
Here is the full pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">






        <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">

Step 2: Configuration in yml file and in the Configuration class

In the yml file I am setting the Sundial related parameters. I am also setting the Application Name as DropwizardRedis, Application server port as 4000 and adminContextPath as: /admin
Here is the full yml file dropwizardsundial.yml:
  level: INFO
    - type: console
      threshold: ALL
      timeZone: IST
  type: simple
  applicationContextPath: /
  adminContextPath: /admin
      port: 4000
      type: http

appName : DropwizardSundial

  thread-pool-size: 10
  shutdown-on-unload: true
  start-delay-seconds: 0
  start-scheduler-on-load: true
  global-lock-on-load: false
  annotated-jobs-package-name: com.aj.dropwizardsundial.job
  tasks: [startjob, stopjob]
Getters and setters for the properties in the yml file are created in the Configuration class. Here is the file: DropwizardSundialConfiguration.java:
package com.aj.dropwizardsundial;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.dropwizard.Configuration;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.knowm.dropwizard.sundial.SundialConfiguration;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@JsonIgnoreProperties(ignoreUnknown = true)
public class DropwizardSundialConfiguration extends Configuration {

    public String appName;

    public String getAppName() {
        return appName;

    public void setAppName(String appName) {
        this.appName = appName;

    public SundialConfiguration sundialConfiguration = new SundialConfiguration();

    public SundialConfiguration getSundialConfiguration() {
        return sundialConfiguration;

Step 3: Add the Sundial Bundle in the Application Class 

Everything comes together in the application class. We register the resources, health check and most importantly add and configure the dropwizard-sundial bundle.
Here is the file: DropwizardSundialApplication.java:
package com.aj.dropwizardsundial;

import com.aj.dropwizardsundial.resource.DropwizardSundialHealthCheckResource;
import com.aj.dropwizardsundial.resource.PingResource;
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import org.knowm.dropwizard.sundial.SundialBundle;
import org.knowm.dropwizard.sundial.SundialConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DropwizardSundialApplication extends Application<DropwizardSundialConfiguration> {

    private static final Logger logger = LoggerFactory.getLogger(DropwizardSundialApplication.class);

 public static void main(String[] args) throws Exception {
  new DropwizardSundialApplication().run("server", args[0]);

    public void initialize(Bootstrap<DropwizardSundialConfiguration> b) {
  b.addBundle(new SundialBundle<DropwizardSundialConfiguration>() {

   public SundialConfiguration getSundialConfiguration(DropwizardSundialConfiguration configuration) {
    return configuration.getSundialConfiguration();

 public void run(DropwizardSundialConfiguration config, Environment env)
   throws Exception {
        logger.info("Registering RESTful API resources");
  env.jersey().register(new PingResource());
    new DropwizardSundialHealthCheckResource(config));

Step 4: Create the required Job Classes

I am creating 2 Job Classes. One is a Job Class with a Simple Trigger and the other is a Job Class with a Cron Trigger. The code is very simple and self explanatory. All you have to do is to write the logic of the scheduled task in your application in the overridden doRun() method. 
Here is SimpleJob.java:
package com.aj.dropwizardsundial.job;

import org.knowm.sundial.Job;
import org.knowm.sundial.annotations.SimpleTrigger;
import org.knowm.sundial.exceptions.JobInterruptException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

@SimpleTrigger(repeatInterval = 10, timeUnit = TimeUnit.SECONDS)
public class SimpleJob extends Job {

    private static final Logger logger = LoggerFactory.getLogger(SimpleJob.class);
    public void doRun() throws JobInterruptException {
        logger.info("Hello from Sample Job");
Here is CronJob.java:
package com.aj.dropwizardsundial.job;

import org.knowm.sundial.Job;
import org.knowm.sundial.annotations.CronTrigger;
import org.knowm.sundial.exceptions.JobInterruptException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@CronTrigger(cron = "0 19 13 * * ?")
public class CronJob extends Job {

    private static final Logger logger = LoggerFactory.getLogger(CronJob.class);
    public void doRun() throws JobInterruptException {
        logger.info("Hello from Cron Job");

Concurrent Job Execution

By default jobs are not set to concurrently execute. This means if a job is currently running and a trigger is fired for that job, it will skip running the job. In some cases concurrent job execution is desired and to configure it use the Sundial annotation as below:
@SimpleTrigger(repeatInterval = 10, timeUnit = TimeUnit.SECONDS, isConcurrencyAllowed = true)
The same can be used for Cron annotation as well.

Few examples of Cron Expressions

0 0 13 * * ? - Fire at 1 PM (noon) every day
0 20 11 * * ? - Fire at 11:20 AM every day
0 20 11 ? * MON-FRI - Fire at 11:20 AM every Monday, Tuesday, Wednesday, Thursday and Friday
0 0/10 * * * ? - Fire every 10 minutes starting at 12 AM (midnight) every day

Project Setup:

For Java Setup, please refer to:  Java Setup
For Maven Setup, please refer to: Maven Setup
For Git and Project Setup, please refer to: Git and Project Setup

Run Application:

1. To run application in your IDE use:
    Program arguments: src/main/resources/dropwizardsundial.yml

2. To run JAR from command prompt:
     Build jar by using command:   
   mvn clean install
     Run JAR by using command in Project folder location:
   java -jar target\DropwizardSundial-1.0.0.jar src\main\resources\dropwizardsundial.yml


Simple Job prints log statement every 10 seconds and the Cron Job prints log statement at the time given in the Cron Expression as shown in the image below:

Other API calls and results:

Other than these APIs, this application has the following APIs:
1. GET API for Application Health Check:
2. GET API to Ping and test if the application is up and running: 
3. POST API to Ping and test if the application is up and running: 
    JSON Request Body:
     "input": "ping"
4. GET Admin API to see application metrics: Dropwizard provides this Admin API. As I have set adminContextPath as: /admin in dropwizardmongodb.yml the link I need to use in any browser is: http://localhost:4000/admin/
You can click on individual links on this page to see different application metrics.

Conclusion and GitHub link:   

    In this post I have shown you as to how you can implement the scheduler functionality in a Dropwizard application. The code used in this post is available on GitHub.
    Learn the most popular and trending technologies like Machine Learning, Chatbots, Internet of Things (IoT), Big Data Processing, Elastic Stack, Angular 5, Akka HTTP, Play Framework, Dropwizard, Docker, Netflix Eureka, Netflix Zuul, Spring Cloud, Spring Boot, Flask and RESTful Web Service integration with MongoDB, Kafka, Redis, Aerospike, MySQL DB in simple steps by reading my most popular blog posts at Software Developer Central.
    If you like my post, please feel free to share it using the share button just below this paragraph or next to the heading of the post. You can also tweet with #SoftwareDeveloperCentral on Twitter. To get a notification on my latest posts or to keep the conversation going, you can follow me on Twitter or Instagram. Please leave a note below if you have any questions or comments.


Popular Posts

Asynchronous Processing (@Async) in Spring Boot

Elasticsearch, Logstash, Kibana Tutorial: Load MySQL Data into Elasticsearch

Dropwizard MySQL Integration Tutorial