Simple Quartz job, reading cron job expression from xml file. {RamJobStore} 1

This blog is to show how to make a Quartz job, reading cron job expression from xml file. Its a ram job store example i.e. it will not put the information inside any DB.

STEP 1:

Ok, lets’s start first making the quartz-job.xml

<?xml version="1.0" encoding="UTF-8"?>
<job-scheduling-data
    xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData 
    http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
    version="1.8">
 
    <schedule>
        <job>
            <name>FileListenerJob</name>
            <group>ListenerGroup</group>
            <description>This is File Listener Job</description>
            <job-class>com.quartz.module.job.FileListenerJob</job-class>
        </job>
 
        <trigger>
            <cron>
                <name>fileListenerTrigger</name>
                <job-name>FileListenerJob</job-name>
                <job-group>ListenerGroup</job-group>
                <!-- It will run every 10 seconds -->
                <cron-expression>0/10 * * * * ?</cron-expression>
            </cron>
        </trigger>
    </schedule>
</job-scheduling-data>

Now we will make a quartz.properties file, in which we will give the reference of quartz-job.xml and also tell the quartz-module that we want it a ramjob store.

# property file for quartz << Ram job store >>
org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
org.quartz.plugin.jobInitializer.wrapInUserTransaction = true
org.quartz.plugin.jobInitializer.class =org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = quartz-config.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval=0

Ok, here we finished with the configuration part of the quartz-module.

STEP 2:

Now let’s start making the job and factory classes.

Here i am making use of controller class which is responsible for making the SchedulerFactory.
Below is the code:

import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

import com.quartz.module.exception.QuartzModuleException;

public class QuartzController {

    private static final QuartzController controller = new QuartzController();
    private Scheduler scheduler;
    private QuartzController(){}

    public static QuartzController getInstance(){
        return controller;
    }

    public void initialize(){
        try {
            SchedulerFactory factory = new StdSchedulerFactory(<strong>"quartz.properties"</strong>);
            scheduler = factory.getScheduler();
            scheduler.start();
        } catch (Exception e) {
            throw new QuartzModuleException(e.getMessage(), e.getCause());
        }
    }
}

In above code we gave reference of quartz.properties file, which make use of quart-job.xml file to register the job.
Below is the code of FileListenerJob class.

package com.quartz.module.job;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.filemaker.reader.UploadedFileListener;

public class FileListenerJob implements Job{

    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        UploadedFileListener fileListener = new UploadedFileListener("[a-z]");
        System.out.println("File Listener Job : No Of Files Uploaded :: " + 
                fileListener.getNumberOfFiles());
    }

}

One comment on “Simple Quartz job, reading cron job expression from xml file. {RamJobStore}

  1. Reply caleb Aug 12,2013 10:35 pm

    Thank you for this blog. That’s almost all I can declare. You almost certainly have made this web site into some thing thats eye opening and important. You clearly know so much about the subject, youve included so many facets. Great products from this the main internet. Once more, thank you for this web site.

Leave a Reply