Quartz helps to schedule a job to run at a specific time.
- Download quartz-2.1.5.jar from this location and add to buildpath of your project.
 - For maven Project:
 
<dependencies>
 <dependency>
  <groupId>org.quartz-scheduler</groupId>
  <artifactId>quartz</artifactId>
  <version>2.1.5</version>
 </dependency>
</dependencies>
- File > New > Other > Dynamic Web Project
 
- Copy quartz.2.1.5.jar under WEB-INF/lib
 
- File > New > Class
 
| package
  code.more.talk.less; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class QuartzJOB implements Job{ public void execute(JobExecutionContext context)throws JobExecutionException { System.out.println("Hello Quartz!"); } }  | 
 
File > New > Folder >> resources
- Right click > properties > Java Build Path > Source
 
- Right click on resources folder > New > Other > Properties File
 
- Content of quartz.properties file:
 
| org.quartz.scheduler.instanceName = QuartzJOB org.quartz.threadPool.threadCount = 3 org.quartz.plugin.jobInitializer.class =org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin org.quartz.plugin.jobInitializer.fileNames = quartz.xml org.quartz.plugin.jobInitializer.failOnFileNotFound = true  | 
 
- Right click on resources folder > New > Other > Xml file
 
    <?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>QuartzJOB</name> 
                  <job-class>code.more.talk.less.QuartzJOB</job-class> 
            </job> 
            <trigger> 
                  <cron> 
                        <name>QuartzTrigger</name> 
                        <job-name>QuartzJOB</job-name> 
                        <!-- It
  will run every 5 seconds --> 
                        <cron-expression>0/5 * * * * ?</cron-expression> 
                  </cron> 
            </trigger> 
      </schedule> 
</job-scheduling-data> 
 | 
 
Add below code to the web.xml:
    <context-param> 
         <param-name>quartz:config-file</param-name> 
         <param-value>quartz.properties</param-value> 
         <!--
  <param-value>/some/path/my_quartz.properties</param-value> --> 
     </context-param> 
     <context-param> 
         <param-name>quartz:shutdown-on-unload</param-name> 
         <param-value>true</param-value> 
     </context-param> 
     <context-param> 
         <param-name>quartz:wait-on-shutdown</param-name> 
         <param-value>true</param-value> 
     </context-param> 
     <context-param> 
         <param-name>quartz:start-on-load</param-name> 
         <param-value>true</param-value> 
     </context-param> 
     <listener> 
          <listener-class> 
             
  org.quartz.ee.servlet.QuartzInitializerListener 
         </listener-class> 
     </listener> 
 | 
 
No comments:
Post a Comment