quartz搭建與應用

1.添加依賴html

依賴包括Quartz和logbackjava

<dependencies>
    <dependency>
       <groupId>org.quartz-scheduler</groupId>
       <artifactId>quartz</artifactId>
       <version>2.2.0</version>
    </dependency>

    <dependency>
       <groupId>ch.qos.logback</groupId>
       <artifactId>logback-classic</artifactId>
       <version>1.1.0</version>
    </dependency>
</dependencies>

 

2.簡單搭建web

引入jar包app

 

在quartz.properties中配置quartz框架

org.quartz.scheduler.instanceName = MyScheduler
#個數隨實際狀況而定 org.quartz.threadPool.threadCount = 3 org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

logback.xml,日誌框架logback的配置eclipse

<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder 
            by default -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>D:/logs/quartz_task_application.log</file>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>

</configuration>

 

HelloJob.java,具體要執行任務ide

package No01;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloJob implements Job {
    
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        // 此任務僅打印日誌便於調試、觀察
        this.logger.debug(this.getClass().getName() + " trigger...");
    }

}

定義執行任務的時間和任務內容ui

package No01;

import java.util.concurrent.TimeUnit;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Bootstrap {
    private static Logger logger = LoggerFactory.getLogger(Bootstrap.class);
    public static void main(String[] args) {
      try {
         // 獲取Scheduler實例
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.start();

        // 具體任務
        JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity("job1", "group1").build();

       // 觸發時間點
       SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever();
       Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").startNow().withSchedule(simpleScheduleBuilder).build();
    // 交由Scheduler安排觸發
       scheduler.scheduleJob(job, trigger);
            
 /* 爲觀察程序運行,此設置主程序睡眠3分鐘才繼續往下運行(因下一個步驟是「關閉Scheduler」) */
      try {
           TimeUnit.MINUTES.sleep(3);
          } catch (InterruptedException e) {
                e.printStackTrace();
        }
    // 關閉Scheduler
       scheduler.shutdown();

      } catch (SchedulerException se) {
          logger.error(se.getMessage(), se);
      }
    }

}

3.在web應用中使用quartzthis

 

Quartz常在Web中應用,常見的是交由Spring託管的形式。這裏介紹Quartz在Web應用中單獨使用。spa

通常Web應用啓動時,應註冊已經肯定的定時任務;一些動態的、未肯定觸發時間的定時任務,後續可經過靜態的Scheduler註冊。

使用監聽器在應用啓動時註冊,並在web.xml註冊這個監聽器;在關閉Web應用時,要註銷定時任務。

其餘配置文件、Java類與上例子相同,只是把這裏註冊定時任務的地方換成此監聽器。

package No02;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import No01.HelloJob;

/**
 * Application Lifecycle Listener implementation class AListener
 *
 */
public class ApplicationContextListener implements ServletContextListener {
    
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    public static Scheduler scheduler = null;

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        this.logger.info("The application start...");
        
        /* 註冊定時任務 */
        try {
            // 獲取Scheduler實例
            scheduler = StdSchedulerFactory.getDefaultScheduler();
            scheduler.start();

            // 具體任務
            JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity("job1", "group1").build();

            // 觸發時間點
            SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever();
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").startNow().withSchedule(simpleScheduleBuilder).build();

            // 交由Scheduler安排觸發
            scheduler.scheduleJob(job, trigger);
            
            this.logger.info("The scheduler register...");
        } catch (SchedulerException se) {
            logger.error(se.getMessage(), se);
        }
    }
    
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        this.logger.info("The application stop...");
        
        /* 註銷定時任務 */
        try {
            // 關閉Scheduler
            scheduler.shutdown();
            
            this.logger.info("The scheduler shutdown...");
        } catch (SchedulerException se) {
            logger.error(se.getMessage(), se);
        }
    }

}

<listener>   <listener-class>No02.ApplicationContextListener</listener-class>
</listener>

 在eclipse調試中,可能發現沒法看到contextDestroy方法的執行,請注意使用stop的方式關閉應用,而不是terminate

 圖一

 

圖二

4經常使用的Cron Schedule

 相對於其餘方式定義定時任務的觸發時間,咱們較經常使用Cron Schedule。

// 具體任務
JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity("job1", "group1").build();

// 觸發時間點
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 * * * * ? *");
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1")
        .withSchedule(cronScheduleBuilder).build();

// 交由Scheduler安排觸發
scheduler.scheduleJob(job, trigger);

 本文參考:http://www.cnblogs.com/nick-huang/p/4848843.html

相關文章
相關標籤/搜索