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.
- 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.
- 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.
-
First, we will construct a class named
MyTask
that extends theTimerTask
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"); } }
-
Now, we will build the
Main()
class to use theTimerTask
.public class Main {}
-
Create an instance of the
Timer()
class object, which we will nametimer
below.Timer timer = new Timer();
-
After that, we will construct an instance of the
MyTask
class and give it the nameshaniitask
.MyTask shaniitask = new MyTask();
-
Lastly, we’ll schedule the job by using the
scheduleAtFixedRate
function and providing it with the name of the taskshaniitask
and the time in seconds of the activity that has to be executed every time. The following activity is set to repeat every40
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
...
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