How to Create Cron Job in Java

Muhammad Zeeshan Feb 02, 2024
  1. What is a Cron Job
  2. Create a Cron Job in Java
How to Create Cron Job in Java

This tutorial will go through how to create a Cron job using Java. But before we get into it, let’s look at Cron or Cron job.

What is a Cron Job

Cron is an application that is commonly used for work scheduling. It is accessible on operating systems similar to Unix and may be downloaded there.

Specific programs or scripts may need to be executed on occasion. These programs or scripts are added as Cron jobs, and a schedule is set to explain when this job should be performed.

  1. For instance, you might use Cron to execute the program, such as saving data database systems or information, rebuilding the structure with automated changes, evaluating disk space use, delivering messages, etc. Cron may also be used to send emails.
  2. In other words, the purpose of a Cron expression is to describe the date and time at which a job that has been scheduled must be carried out.

Create a Cron Job in Java

Creating a Cron job in Java may be performed in several different ways. Here we’ll discuss Timer Task in Java.

Use Timer Task Class

A job may be assigned to a Timer to run either once or repeatedly at specific intervals. Importing the following library into your project will allow you to use the TimerTask.

import java.util.TimerTask;

Let’s have a look at the following example to get a better understanding of how the TimerTask function works.

  1. First, we will construct a class named MyTask that extends the TimerTask class. This will be the point where our task logic will be written.

    class MyTask extends TimerTask {
      public MyTask() {}
      @Override
      public void run() {
        System.out.println("Hello, folks");
      }
    }
    
  1. Now, we will build the Main() class to use the TimerTask.

    public class Main {}
    
  2. Create an instance of the Timer() class object, which we will name timer below.

    Timer timer = new Timer();
    
  3. After that, we will construct an instance of the MyTask class and give it the name shaniitask.

    MyTask shaniitask = new MyTask();
    
  4. Lastly, we’ll schedule the job by using the scheduleAtFixedRate function and providing it with the name of the task shaniitask and the time in seconds of the activity that has to be executed every time. The following activity is set to repeat every 40 seconds on the schedule.

    timer.scheduleAtFixedRate(shaniitask, 0, 40000);
    

Complete source code:

MyTask.java:

class MyTask extends TimerTask {
  public MyTask() {}
  @Override
  public void run() {
    System.out.println("Hello, folks");
  }
}

Main.java:

public class Main {
  public static void main(String[] args) {
    Timer timer = new Timer();
    MyTask shaniitask = new MyTask();
    timer.scheduleAtFixedRate(shaniitask, 0, 40000);
  }
}

Output:

Hello, folks
Hello, folks
Hello, folks
...
Muhammad Zeeshan avatar Muhammad Zeeshan avatar

I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.

LinkedIn