Quartz Scheduler Using Cron Trigger


ListenerStart.Java
--------------------------
 package com.AutomaticFileProcessing;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ListenerStart implements ServletContextListener{
      public void contextDestroyed(ServletContextEvent arg0)
    {
        System.out.println("Stopping Application successfully");
    }
  
    public void contextInitialized(ServletContextEvent arg0)
    {
       System.out.println("Initializing Application successfully");
     
       try{
           new CronTriggerExample().CronTriggerExample();
       }
       catch(Exception e)
       {
           System.out.println("The exception is:"+e);
           e.printStackTrace();
       }
     }        
}

-----------------------------------------------
CronTriggerExample.java

package com.AutomaticFileProcessing;
import java.text.ParseException;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
/**
 *
 * @author bgdit
 */
public class CronTriggerExample {
//     public static void main( String[] args ) throws Exception
    public void CronTriggerExample() throws ParseException, SchedulerException
    {
        try{
        //Quartz 1.6.3
        //JobDetail job = new JobDetail();
        //job.setName("dummyJobName");
        //job.setJobClass(HelloJob.class);  
                   System.out.println("Intializing Job");
                     JobDetail job = JobBuilder.newJob(CallingAllJob.class)
                         .withIdentity("dummyJobName", "group1").build();
      

    //Quartz 1.6.3
        //CronTrigger trigger = new CronTrigger();
        //trigger.setName("dummyTriggerName");
        //trigger.setCronExpression("0/5 * * * * ?");
       
                      Trigger trigger = TriggerBuilder
                     .newTrigger()
                        .withIdentity("dummyTriggerName", "group1")
                          .withSchedule(
                         CronScheduleBuilder.cronSchedule("0 10 12,23 * * ?"))
                          .build();
       
        //schedule it
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        scheduler.scheduleJob(job, trigger);
        }
        catch(Exception e){
            System.out.println("The exception is:"+e);
        }
    }
}
---------------
CallingAllJob.java
 
package com.AutomaticFileProcessing;

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

public class CallingAllJob implements Job {
     // Required public empty constructor for job initialization
    FTPClientFiles fc = new FTPClientFiles();
    LastModifiedDate lmd = new LastModifiedDate();
//    MissingIsbnReprocess mirp = new MissingIsbnReprocess();
    public CallingAllJob() {
    }
  
    /* execute() method is called by the org.quartz.Scheduler when a org.quartz.Trigger
    * fires that is associated with the org.quartz.Job
    */
    public void execute(JobExecutionContext context) throws JobExecutionException {
       try{
        System.out.println("Calling Job Time:: "+new java.util.Date());

        lmd.LastModifiedDate();
       
       }catch(Exception e){
           System.out.println("The exception is:"+e);
           e.printStackTrace();
       }
    }
}
---------------------------------------------------------------------------------------
Web.xml is used to call the Listener Class
------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
   
     <listener>
         <listener-class>com.AutomaticFileProcessing.ListenerStart</listener-class>
     </listener>
</web-app>
----------------------------------------------------------------------------------------------
quartz_data.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">
  
    <pre-processing-commands>
        <delete-jobs-in-group>*</delete-jobs-in-group>  <!-- clear all jobs in scheduler -->
        <delete-triggers-in-group>*</delete-triggers-in-group> <!-- clear all triggers in scheduler -->
    </pre-processing-commands>
  
    <processing-directives>
        <!-- if there are any jobs/trigger in scheduler of same name (as in this file), overwrite them -->
        <overwrite-existing-data>true</overwrite-existing-data>
        <!-- if there are any jobs/trigger in scheduler of same name (as in this file), and over-write is false, ignore them rather then generating an error -->
        <ignore-duplicates>false</ignore-duplicates>
    </processing-directives>
  
    <schedule>
        <job>
            <name>Job1</name>
            <job-class>CallingAllJob</job-class>
        </job>
      
        <trigger>
            <simple>
                <name>Trigger1</name>
                <job-name>Job1</job-name>
                <repeat-count>-1</repeat-count> <!-- repeat indefinitely  -->
                <repeat-interval>1000</repeat-interval>  <!--  every 1 seconds -->
            </simple>
        </trigger>

    </schedule>  
</job-scheduling-data>
---------------------------------------------------------------------------------------------
quartz.properties
----------------------------------------
# To change this license header, choose License Headers in Project Properties.
# To change this template file, choose Tools | Templates
# and open the template in the editor.
#============================================================================
# Configure Main Scheduler Properties
#============================================================================

org.quartz.scheduler.instanceName: TestScheduler
org.quartz.scheduler.instanceId: AUTO

org.quartz.scheduler.skipUpdateCheck: true

#============================================================================
# Configure ThreadPool
#============================================================================

org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 3
org.quartz.threadPool.threadPriority: 5

#============================================================================
# Configure Plugins
#============================================================================

org.quartz.plugin.triggHistory.class: org.quartz.plugins.history.LoggingJobHistoryPlugin

org.quartz.plugin.jobInitializer.class: org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames: quartz_data.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound: true
org.quartz.plugin.jobInitializer.scanInterval: 120
org.quartz.plugin.jobInitializer.wrapInUserTransaction: false

Comments

Popular posts from this blog

How do i Increase the capacity of the Eclipse Output console?

How to set max connections in mysql?

An internal error occurred during: "Building workspace". Java heap space