Setup Quartz Scheduler with MYSQL Database

Setup Quartz Scheduler with MYSQL Database

Setup Quartz Scheduler with MYSQL Database

This article details on how to setup Quartz server with MYSQL database.

Setup MYSQL

  1. Download the latest version of Quartz distribution from the website http://quartz-scheduler.org/downloads and extract it. The distribution will be in TAR format.
  2. Install MYSQL and go to command line interface to create the required database and tables.
  3. Create a database named quartz.
  4. Now use the command use quartz to make MYSQL use the newly created database.
  5. Go to QUARTZ_HOME(extracted TAR)/docs/dbTables and open the file tables_mysql_innodb.sql
  6. Execute the command in the file in the MYSQL console to create the required tables.
  7. There was an syntax error in the command in the distribution which I downloaded. Make sure that the table QRTZ_FIRED_TRIGGERS ends with the syntax ENGINE=InnoDB;.

Setup Quartz Server

  1. Create a Maven project in Eclipse and use the following dependencies or import the JAR files specified in the POM to the required project.
  2. <dependencies>
        <dependency>
             <groupId>org.quartz-scheduler</groupId>
              <artifactId>quartz</artifactId>
              <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz-jobs</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.directory.studio</groupId>
            <artifactId>org.apache.commons.logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>1.5.3</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>
    </dependencies>
  3. Create a class with the following programming logic.
  4. package quartz;
    
    import java.util.Properties;
    import org.quartz.Scheduler;
    import org.quartz.SchedulerException;
    import org.quartz.SchedulerFactory;
    import org.quartz.impl.StdSchedulerFactory;
    
    public class quartzserver {
         public static void main(String[] args) {
              try {
                   Properties prop = new Properties();
    
                   //RMI configuration to make the client to connect to the Quartz server
                   prop.put("org.quartz.scheduler.rmi.export", "true");
                   prop.put("org.quartz.scheduler.rmi.createRegistry", "true");
                   prop.put("org.quartz.scheduler.rmi.registryHost", "localhost");
                   prop.put("org.quartz.scheduler.rmi.registryPort", "1099");
                   prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
                   prop.put("org.quartz.threadPool.threadCount", "2");
    
                   //Quartz Server Properties
                   prop.put("quartz.scheduler.instanceName", "ServerScheduler");
                   prop.put("org.quartz.scheduler.instanceId", "AUTO");
                   prop.put("org.quartz.scheduler.skipUpdateCheck", "true");
                   prop.put("org.quartz.scheduler.instanceId", "NON_CLUSTERED");
                   prop.put("org.quartz.scheduler.jobFactory.class", "org.quartz.simpl.SimpleJobFactory");
                   prop.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
                   prop.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
                   prop.put("org.quartz.jobStore.dataSource", "quartzDataSource");
                   prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_");
                   prop.put("org.quartz.jobStore.isClustered", "false");
    
                   //MYSQL DATABASE CONFIGURATION
                   //If we do not specify this configuration, QUARTZ will use RAM(in-memory) to store jobs
                   //Once we restart QUARTZ, the jobs will not be persisted
                   prop.put("org.quartz.dataSource.quartzDataSource.driver", "com.mysql.jdbc.Driver");
                   prop.put("org.quartz.dataSource.quartzDataSource.URL", "jdbc:mysql://localhost:3306/quartz");
                   prop.put("org.quartz.dataSource.quartzDataSource.user", "root");
                   prop.put("org.quartz.dataSource.quartzDataSource.password", "mysql");
                   prop.put("org.quartz.dataSource.quartzDataSource.maxConnections", "2");
    
                   SchedulerFactory stdSchedulerFactory = new StdSchedulerFactory(prop);
                   Scheduler scheduler = stdSchedulerFactory.getScheduler();
                   scheduler.start();
    
              } catch (SchedulerException e) {
                   e.printStackTrace();
              }
         }
    }
  5. Executing the above class file will make the Quartz server start.
  6. Now we will create a sample JOB (HelloJob) to run in Quartz server.
  7. package quartz;
    import org.quartz.Job;
    import org.quartz.JobExecutionContext;
    
    public class HelloJob implements Job {
        public void execute(JobExecutionContext context){
            System.out.println("Hello World !!!");
        }
    }
  8. Now we will submit a JOB to the Quartz server.
  9. package quartz;
    
    import static org.quartz.JobBuilder.*;
    import java.util.Properties;
    import org.quartz.JobDetail;
    import org.quartz.Scheduler;
    import org.quartz.SchedulerException;
    import org.quartz.Trigger;
    import org.quartz.impl.StdSchedulerFactory;
    import static org.quartz.TriggerBuilder.*;
    import static org.quartz.SimpleScheduleBuilder.*;
    
    public class test {
    	    public static void main(String[] args) {
    		        try {
                			JobDetail job = newJob(HelloJob.class).withIdentity("myJob", "group1").build();
    
                			Trigger trigger = newTrigger().withIdentity("myTrigger", "group1").startNow().withSchedule(simpleSchedule().withIntervalInSeconds(10).repeatForever()).build();
    
                //Quartz Server Properties
                			Properties prop = new Properties();
                			prop.put("org.quartz.scheduler.rmi.proxy", "true");
                			prop.put("org.quartz.scheduler.rmi.registryHost", "localhost");
                			prop.put("org.quartz.scheduler.rmi.registryPort", "1099");
                			prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
                			prop.put("org.quartz.threadPool.threadCount", "1");
    
                			Scheduler scheduler = new StdSchedulerFactory(prop).getScheduler();
                			scheduler.scheduleJob(job, trigger);
            		} 
            catch (SchedulerException e) {
                e.printStackTrace();
            		}
        	}
    }
  10. Execute the above class will submit a JOB to the Quartz server.
  11. Now we can see Hello World !!! in the Quartz server console for every 10 seconds.
  12. Since we have configured MYSQL database with Quartz, the jobs will be persisted in MYSQL and the jobs will continue to work according the schedule even after restarting the Quartz server. i.e. Quartz will pull the job details from the database when each time it is started.
  13. So we will have Quartz sever and Job together in the server side and the details to schedule the job in the client side.
  14. Enjoy !!!

[ YOU MAY ALSO LIKE ]

Leave a Reply