Spring-Quartz java定时器

Quartz:定时器

不多说直接上配置 --- 代码说话

 

1.首先是需要的jar包  大家还需把spring的核心包补上 

<dependencies>
<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz-jobs -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
</dependencies>

 

2.配置自定义任务类  按时去触发的类

package com.yangyu.job;

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

public class MyJob {

    public void getNowTime() {
        System.out.println("当前系统时间 : " + new SimpleDateFormat("HH:mm:ss").format(new Date()));
    }

}

 

3. 配置spring-quartz.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"
       xsi:schemaLocation="
                    http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- job  任务对象-->
    <bean id="myJob" class="com.yangyu.job.MyJob"/>

    <!-- 使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法-->
    <!-- jobDetail 任务细节-->
    <bean id="jobWork" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="myJob"/>
        <property name="targetMethod" value="getNowTime"/>
        <!--false表示等上一个任务执行完后再开启新的任务-->
        <property name="concurrent" value="false"/>
    </bean>

    <!-- Trigger 触发器-->
    <bean id="jobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="jobWork"/>
        <property name="cronExpression" value="* * * * * ?"/>
    </bean>

    <!--Scheduler 调度器-->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="jobTrigger"/>
            </list>
        </property>
    </bean>

</beans>

 

4. 启动项目,读取spring-quartz.xml

package com.yangyu.job;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("spring-quartz.xml");
    }
}

 

5. 结果

Spring-Quartz  java定时器

 

上一篇:SpringBoot2.0整合Quartz实现动态设置定时任务时间


下一篇:字符串转码【String.getBytes()和new String()】