springmvc+quartz简单实现定时调度

简介: 一、简介:Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。

一、简介:Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。Jobs可以做成标准的Java组件或 EJBs。Quartz的最新版本为Quartz 2.3.0。

二、因为定时调度,在很多业务上面都会涉及,想要根据自己的规则来生成自己想要的达到的目的。所以利用quartz来时间定时任务的触发。是非常有必要的。

三、springmvc下需要的jar包

   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.quartz-scheduler</groupId>
      <artifactId>quartz</artifactId>
      <version>2.2.3</version>
    </dependency>

说明:quartz是基础包。spring-context-support包是调度设置的依赖包。spring-context可以不用添加。

四、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"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
       
    <!-- 加入需要执行的类 -->
    <bean id="timingSchedule" class="com.troy.jpa.schedule.TimingSchedule"/>
    <!-- 加入定时执行的方法 -->
    <bean id="timingScheduleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <!-- 定时执行的类 -->
        <property name="targetObject" ref="timingSchedule"/>
        <!-- 具体的方法 -->
        <property name="targetMethod" value="execute"/>
    </bean>
    <!-- 调度触发器,设置自己想要的时间规则 -->
    <bean id="timingScheduleTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!-- 加入相关的执行类和方法 -->
        <property name="jobDetail" ref="timingScheduleJobDetail"/>
        <!-- 设置时间规则 (为了方便测试,设置成一分钟一次。具体的规则见详情)-->
        <property name="cronExpression" value="00 * * * * ?"/>    
    </bean>
    <!-- 加入调度工厂 ,设置调度触发器即可-->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="timingScheduleTrigger"/>
            </list>
        </property>
    </bean>
</beans>

五、需要执行的类和方法

package com.troy.jpa.schedule;

import java.util.Date;

public class TimingSchedule {
    
    //定时执行的方法
    public void execute(){
        System.out.println("执行时间"+ new Date());
    }
}

六、执行效果

执行时间Wed Jul 12 21:01:00 CST 2017
执行时间Wed Jul 12 21:02:00 CST 2017
执行时间Wed Jul 12 21:03:00 CST 2017
执行时间Wed Jul 12 21:04:00 CST 2017

七、时间设置规则

1  秒  是  0-59    , - * / 
2  分  是  0-59   , - * / 
3 小时  是  0-23   , - * / 
4  日  是  1-31   , - * ? / L W 
5  月  是  1-12 or JAN-DEC   , - * / 
6  周  是  1-7 or SUN-SAT   , - * ? / L # 
7  年  否  empty 或 1970-2099  , - * / 

 

相关文章
|
1月前
|
存储 NoSQL Java
【十】springboot整合quartz实现定时任务优化
【十】springboot整合quartz实现定时任务优化
28 0
|
4月前
|
SQL Java 调度
SpringBoot 整合 Quartz 定时任务框架
SpringBoot 整合 Quartz 定时任务框架
59 0
|
Java 调度
Springboot 使用Quartz定时器执行多个定时任务 配置篇
Springboot 使用Quartz定时器执行多个定时任务 配置篇
625 0
Springboot 使用Quartz定时器执行多个定时任务 配置篇
|
存储 Java 数据库连接
这种方式整合Quartz你见过吗?
`Quartz`是一款优秀的任务调度框架,支持内存、JDBC的形式来存储未执行的任务列表,支持多个任务节点同时执行任务,支持任务漂移到不同的节点执行。
|
XML 调度 数据格式
SSM 最简单的使用Quartz实现定时任务
SSM 最简单的使用Quartz实现定时任务
249 0
SSM 最简单的使用Quartz实现定时任务
|
SQL Java 数据库
springboot整合Quartz实现动态配置定时任务
在我们日常的开发中,很多时候,定时任务都不是写死的,而是写到数据库中,从而实现定时任务的动态配置,下面就通过一个简单的示例,来实现这个功能。
3539 0
|
XML 调度 数据格式
Spring4+Springmvc+quartz实现多线程动态定时调度
Spring4+Springmvc+quartz实现多线程动态定时调度scheduler定时调度系统是大多行业项目都需要的,传统的spring-job模式,个人感觉已经out了,因为存在很多的问题,特别是定时调度的追加、修改、删除等,需要修改xml,xml的配置生效无非是热部署灰度发布方案或者直接停止、重启服务器,完全不能做到自动启动、修复方式。
904 0
|
druid Java 数据库
精通SpringBoot——第十篇:使用Quartz实现动态配置定时任务
spring boot 整合quartz实现数据库动态配置定时任务
18731 0
|
关系型数据库 Java 数据库连接
spring框架使用Quartz执行定时任务实例详解
版权声明:本文为博主原创文章,如需转载,请标明出处。 https://blog.csdn.net/alan_liuyue/article/details/80382324 Quartz简介   1.Quartz,是一个完全由java编写的开源作业调度框架。
1143 0