quartz定时任务

Posted 栖梧

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了quartz定时任务相关的知识,希望对你有一定的参考价值。

1.结构

2.执行业务的代码:

  

package com.bst.express;

import java.text.SimpleDateFormat;
import java.util.Date;

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

public class PrintJob {

    /**
     * 定时任务一
     */
    public void run() {
        Date date=new Date();
        SimpleDateFormat df=new SimpleDateFormat("HH:mm:ss");
        System.out.println("执行targetObject中的targetMethod方法,开始。。。。。。。。。。。。。"+df.format(date));
    }
    
    /**
     * 定时任务二
     */
    public void excute()
    {
        Date date=new Date();
        SimpleDateFormat df=new SimpleDateFormat("HH:mm:ss");
        System.out.println(">>>>>执行任务二<<<<<<<<"+df.format(date));
    }
    
    public static void main(String[] args) {
            System.out.println("----begin---");
            ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml");
      }

}

3.配置spring的文件

<?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"
    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">
    <!--实例化执行定时任务的类  -->
    <bean id="task" class="com.bst.express.PrintJob"/>
    <bean id="job"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="task" />
        <property name="targetMethod" value="run" />
    </bean>
    <bean id="job1"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="task" />
        <property name="targetMethod" value="excute" />
    </bean>
    <!--定义触发的时机:方法一:指定时间执行定时任务  -->
    <bean id="cronTrigger"
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="job" />
        <property name="cronExpression" value="0/4 * * * * ?" />
    </bean>
    <!--定义触发的时机:方法二:每隔一段时间执行 -->
    <bean id="initSyncJob"
        class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="job1" />
        <property name="repeatInterval">
            <value>4000</value>
        </property>
    </bean>
    <!--将触发规则加入到调度任务中去  -->
    <bean id="scheduler"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTrigger" />
                <ref bean="initSyncJob" />
            </list>
        </property>
        <property name="autoStartup" value="true" />
    </bean>

</beans>

3.pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bst</groupId>
    <artifactId>express</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>express</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

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

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <!--必须加不然会报错  -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>
        
    </dependencies>
</project>

4.测试的时候运行前面的main函数即可

 

 5.测试结果

 

以上是关于quartz定时任务的主要内容,如果未能解决你的问题,请参考以下文章

spring定时任务quartz

spring + quartz定时任务,以及修改定时任务

以代码的方式管理quartz定时任务的暂停重启删除添加等

Hosted Services+Quartz实现定时任务调度

quartz spring 实现动态定时任务

Quartz-定时任务框架