Java ScheduledExecutorService examples

In Java, we can use ScheduledExecutorService to run a task periodically or once time after a predefined delay by TimeUnit. 1. Run a Task Once The ScheduledExecutorService accepts both Runnable and Callable tasks. 1.1 Run a Runnable task after 5 seconds initial delay. ScheduledExecutorExample.java package com.mkyong.concurrency.executor.scheduler; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class ScheduledExecutorRunnable …

Read more

Java – Cron job to run a jar file

Quartz is good, but often times we just need a simple scheduler system to run a jar file periodically. On *unix system, you can use the build-in cron to schedule a scheduler job easily. In this example, we will show you how to create a cron job on *nix to run a jar file, by …

Read more

How to list all Jobs in the Quartz Scheduler

Below are two code snippets to show you how to list all Quartz jobs. Quartz 2 APIs are changed a lot, so syntax is different from Quartz 1.x. 1. Quartz 2.1.5 example Scheduler scheduler = new StdSchedulerFactory().getScheduler(); for (String groupName : scheduler.getJobGroupNames()) { for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) { String jobName = jobKey.getName(); String jobGroup …

Read more

Example to run multiple jobs in Quartz

In this example, we show you how to declare multiple Quartz jobs via Quartz APIs, Quartz XML and Spring. In Quartz scheduler framework, each job will be attached to an unique trigger, and run it by scheduler. P.S In Quartz, one trigger for multiple jobs is not possible. (Correct me if this is wrong.) 1. …

Read more

Quartz Scheduler Tutorial

Quartz, is a open source job scheduling framework, that let you scheduler a task to run on a predefine date and time. Happy learning Quartz 🙂 1. Quick Start Hello world to Quartz scheduler frameworks. Quartz 1.6 hello world example The old and popular Quartz 1.6.3, legacy system may still using this. Quartz 2 hello …

Read more

JSF 2 + Quartz 2 example

In this tutorial, we show you how to run a Quartz job during JSF web application via QuartzInitializerListener listener class in Quartz library. This solution is not only works with JSF 2, the concept is applicable on almost all standard Java web application. Tools Used : JSF 2.1.11 Quartz 2.1.5 Maven 3 Eclipse 4.2 Tomcat …

Read more

How to run a task periodically in Java

Some java application need to execute a method between a regular interval of time. For example GUI application should update some information from database. 1. Scheduler Task For this functionality, You should create a class extending TimerTask(available in java.util package). TimerTask is a abstract class. Write your code in public void run() method that you …

Read more

Quartz 2 JobListener example

In this tutorial, we will show you how to create a JobListener, to keep track the running jobs status, like when the job is finished. P.S This example is tested with Quartz 2.1.5 1. Quartz Job Job, print a simple message, and throw a JobExecutionException for testing. File : HelloJob.java package com.mkyong.quartz; import org.quartz.Job; import …

Read more

Quartz 2 scheduler tutorial

Quartz, enterprise scheduler job framework, to help Java application to scheduler a job/task to run at a specified date and time. This tutorial show you how to develop a scheduler job using latest Quartz library 2.1.5. Note Quartz 2 involves significant API changed, read this for older Quartz 1.6.3 example. 1. Download Quartz You can …

Read more

Quartz 1.6 scheduler tutorial

Quartz is a powerful and advance scheduler framework, to help Java developer to scheduler a job to run at a specified date and time. This tutorial show you how to develop a scheduler job using Quartz 1.6.3. Note This example is a bit outdate, unless you are still using the old Quartz 1.6.3 library, otherwise, …

Read more

Spring + JDK Timer scheduler example

Note Learn the JDK Timer scheduler example without Spring and compare the different with this example. In this example, you will use Spring’s Scheduler API to schedule a task. 1. Scheduler Task Create a scheduler task… package com.mkyong.common; public class RunMeTask { public void printMe() { System.out.println("Run Me ~"); } } <bean id="runMeTask" class="com.mkyong.common.RunMeTask" /> …

Read more

JDK Timer scheduler example

JDK Timer is a simple scheduler for a specified task for repeated fixed-delay execution. To use this, you have to extends the TimerTask abstract class, override the run() method with your scheduler function. RunMeTask.java package com.mkyong.common; import java.util.TimerTask; public class RunMeTask extends TimerTask { @Override public void run() { System.out.println("Run Me ~"); } } Now, …

Read more