Quartz API is an excellent choice for scheduling the execution of tasks and jobs.In this tutorial we will use Quartz in conjunction with hibernate to configure daily backup of a web application's database.

As an example consider the case where we need a backup of the database to be taken every day at midnight.

The first step will be to create the class that is going to be the task that will be executed by the Quartz scheduler.

package com.javaonly.jobs;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class DatabaseBackupJob implements Job {

    public void execute(JobExecutionContext jec) throws JobExecutionException {
        Configuration cfg=new Configuration();
        SchemaExport se=new SchemaExport(cfg);
        se.execute(true, true, false, false);

As you can see in the above class used the SchemaExport class for exporting the sql file of the database..In order to configure hbm2ddl tool we must add the following property at the hibernate.cfg.xml file:

 <property name="hibernate.hbm2ddl.auto">create-drop</property>

Finally we will create a servlet context listener where we will configure the scheduler to execute the DataBaseBackupJob every day at midnight:

package com.javaonly.listeners;
import org.quartz.CronTrigger;
import com.javaonly.jobs.DatabaseBackupJob;
import org.quartz.JobDetail;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.*;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

public class DatabaseBackupListener implements ServletContextListener {
    private Scheduler scheduler;
    public void contextInitialized(ServletContextEvent event) {
        try {
//initialize the scheduler factory
            SchedulerFactory schedulerFactory=new StdSchedulerFactory();
//get a scheduler
            scheduler = schedulerFactory.getScheduler();
//configure Database backup job
            JobDetail job=newJob(DatabaseBackupJob.class).withIdentity("databaseBackup","group").build();
//create a cron type Trigger and schedule database backup to happen every day at midnight
            CronTrigger trigger = newTrigger()
    .withIdentity("trigger1", "group1")
    .withSchedule(cronSchedule("0 0 12 * * ?"))
      scheduler.scheduleJob(job, trigger);
        } catch (SchedulerException ex) {
            Logger.getLogger(DatabaseBackupListener.class.getName()).log(Level.SEVERE, null, ex);
    public void contextDestroyed(ServletContextEvent event) {
        try {
        } catch (SchedulerException ex) {
            Logger.getLogger(DatabaseBackupListener.class.getName()).log(Level.SEVERE, null, ex);

The only thing left now is to add the listener at the web.xml.At the web xml add the following snippet: