Quartz 1.8.5 – JAVA編程語言程序開發技術文章

首先編寫Job類,隻需要實現Job接口
[html] 
import java.util.Date; 
 
import org.quartz.Job; 
import org.quartz.JobExecutionContext; 
import org.quartz.JobExecutionException; 
 
public class MyFirstQuartz implements Job { 
 
    public void execute(JobExecutionContext arg0) throws JobExecutionException { 
        // TODO Auto-generated method stub 
        System.out.println(arg0.getJobRunTime()); 
        System.out.println(arg0.hashCode()); 
        System.out.println(arg0.getTrigger().getGroup()); 
        System.out.println(arg0.getTrigger().getName()); 
        System.out.println("Hello world"); 
 
    } 
    public void test(){ 
        System.out.println("xxxxxxxxxxxxxxxxxxxxx"+new Date().getTime()); 
    } 

quartz_jobs.xml:
[html]
<?xml version="1.0" encoding="UTF-8"?> 
<job-scheduling-data 
    xmlns="https://www.quartz-scheduler.org/xml/JobSchedulingData" 
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="https://www.quartz-scheduler.org/xml/JobSchedulingData https://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd" 
    version="1.8"> 
    <schedule> 
        <job> 
            <name>hello</name> 
            <group>group</group> 
            <job-class>org.han.quartz.MyFirstQuartz</job-class> 
        </job> 
        <trigger> 
            <cron> 
                <name>trigger</name> 
                <group>group</group> 
                <job-name>hello</job-name> 
                <job-group>group</job-group> 
                <cron-expression>0/5 * * * * ?</cron-expression> 
            </cron> 
        </trigger> 
    </schedule> 
</job-scheduling-data> 

quartz.properties:
#===============================================================    
#Configure Main Scheduler Properties    
#===============================================================     
org.quartz.scheduler.instanceName = QuartzScheduler     
org.quartz.scheduler.instanceId = AUTO    
 
#===============================================================    
#Configure ThreadPool    
#===============================================================     
org.quartz.threadPool.threadCount = 5     
org.quartz.threadPool.threadPriority = 5     
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool    
 
#===============================================================    
#Configure JobStore    
#===============================================================     
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore    
 
#===============================================================    
#Configure Plugins    
#===============================================================     
org.quartz.plugin.jobInitializer.class =org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin     
    
org.quartz.plugin.jobInitializer.fileNames = quartz_jobs.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown =true

最後記得在web.xml進行配置:
[html] 
<servlet>   
         <servlet-name>   
             QuartzInitializer   
         </servlet-name>   
         <servlet-class>   
             org.quartz.ee.servlet.QuartzInitializerServlet   
         </servlet-class>   
         <init-param>   
             <param-name>config-file</param-name>   
             <param-value>/quartz.properties</param-value>   
         </init-param>   
         <init-param>   
             <param-name>shutdown-on-unload</param-name>   
             <param-value>true</param-value>   
         </init-param>   
         <load-on-startup>1</load-on-startup>   
     </servlet> 

當web容器啟動的時候Quartz也開始運作瞭
作者:hanzhou4519

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。