1. 程式人生 > >Quartz框架使用(任務排程)

Quartz框架使用(任務排程)

目錄

實現簡單的觸發demo

simpleTrigger版

CronTrigger版

spring的任務排程


觸發分為 simpleTrigger和cronTrigger

實現簡單的觸發demo

simpleTrigger版

新建專案並匯入jar包

<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>1.8.6</version>
</dependency>

新建任務類

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

//任務類要實現job介面以及重寫execute方法
public class jobDemo implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("這是任務類要做的任務");
    }
}

新建觸發類(這裡任務排程類也寫在一起了)

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

//觸發類,觸發方法
public class traggerDemo {
    public static void main(String[] args) {
        //要構造例項化  第一個是工作名稱 第二個是任務組   第三個是任務類
        JobDetail jobDetail=new JobDetail("工作名稱","A",jobDemo.class);
        //建立觸發器   第一個引數是觸發器的名稱(myTrigger)   第二個引數是執行觸發的間隔(無限觸發)   第三個引數是間隔的時長(3秒)
        SimpleTrigger simpleTrigger=new SimpleTrigger("myTrigger",SimpleTrigger.REPEAT_INDEFINITELY,3000);
        //建立完觸發器要設定觸發器的開始時間  現在開始
        simpleTrigger.setStartTime(new Date());

        //開始例項化排程類的排程工廠
        SchedulerFactory schedulerFactory=new StdSchedulerFactory();
        try {
            //獲得排程核心
            Scheduler scheduler=schedulerFactory.getScheduler();
            //開始整合任務類和觸發類 第一個引數是任務類    第二個引數是觸發類
            scheduler.scheduleJob(jobDetail,simpleTrigger);
            //執行整合並開始任務
            scheduler.start();
            //休眠15秒後關閉
            Thread.sleep(15000);
            scheduler.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}


CronTrigger版

這是任務類

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

//還是實現並重寫
public class jobDemo implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("這是表示式觸發的例子");
    }
}

新建觸發類(這裡任務排程類也寫在一起了)

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.text.ParseException;
import java.util.Date;

public class triggerDemo {
    public static void main(String[] args) {
        //這是任務類,分組到B組,名字是ctg
        JobDetail jobDetail=new JobDetail("ctg","B",jobDemo.class);

        try {
            //設定每年每月每日的14:17:00秒觸發,這是cron表示式
            CronTrigger cronTrigger=new CronTrigger("mytrigger2","B","0 17 14 ? * * ");
            //立刻開啟
            cronTrigger.setStartTime(new Date());

            SchedulerFactory schedulerFactory=new StdSchedulerFactory();
            Scheduler scheduler=schedulerFactory.getScheduler();
            scheduler.scheduleJob(jobDetail,cronTrigger);
            scheduler.start();
            Thread.sleep(30000);
            scheduler.shutdown();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (SchedulerException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

這是cron表示式的詳解
這是線上生成cron表示式的網址

spring的任務排程

整體demo結構圖

還是要帶入jar包

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <!-- spring版本號 -->
        <spring.version>4.2.5.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

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

        <!-- 新增spring核心依賴 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		   http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		   http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"
       xmlns:p="http://www.springframework.org/schema/p"
>

    <!-- Enables the Spring Task @Scheduled programming model -->
    <!-- 開啟定時任務 -->
    <task:annotation-driven />
    <!--spring掃描全部包的註解加入到Spring容器內-->
    <context:component-scan base-package="com.bdqn.text" />


</beans>

任務觸發排程類

package com.bdqn.text;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
//定義一個bean名叫ssaa方便後續呼叫
@Component("ssaa")
public class SpringCronDemo1 {

    @Scheduled(cron = " * * * ? * * ")
    public void show(){
        System.out.println("這是Spring的Quartz整合測試");
    }
}

測試方法

package com.bdqn.text;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Text {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        applicationContext.getBean("ssaa",SpringCronDemo1.class).show();
    }
}