第一步,在pom.xml中引入quartz-scheduler。

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

第二部,配置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:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
default-lazy-init="false"> <!-- 定义目标bean和bean中的方法 -->
<!-- =====================日常任务job========================== --> <bean id="MyTask" class="com.yjl.controller.quartz.AutoTaskController">
</bean> <bean id="MyTaskMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="MyTask"></property>
<property name="targetMethod" value="execute"></property> <!-- 要执行的方法名称 --> </bean> <!-- ======================== 调度触发器 ======================== -->
<bean id="DailyTaskCronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="MyTaskMethod"></property>
<!-- 每分钟的第一秒触发 每天零点 * * ? -->
<property name="cronExpression" value="0 0 0 * * ?"></property>
</bean> <!-- ======================== 调度工厂 ======================== -->
<bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="DailyTaskCronTriggerBean"/>
</list>
</property>
</bean> </beans>

第三步,编写业务代码:

package com.yjl.controller.quartz;

import com.yjl.service.business.StockFeeFlowService;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; public class AutoTaskController { @Resource
StockFeeFlowService stockFeeFlowService; public void execute() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = format.format(new Date());
System.out.println("这是第一个定时任务:" + date);
stockFeeFlowService.updateStockFeeFlow();
} }

附加:别忘了,在web.xml中配置加载启动配置文件代码。(若有,请忽略这一步。)

 <servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springMVC需要加载的配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</init-param>
<!-- 可配置多个servlet按顺序启动,数值越小启动越早最小是0-->
<load-on-startup></load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- 匹配所有请求,此处也可以配置成 *.do 形式 -->
<url-pattern>/</url-pattern>
</servlet-mapping>

或在加载springmvc配置文件的时候,在其中添加如下代码:(web.xml中配置加载所有spring-开头的配置文件,请忽略这一步。)

<import resource="classpath:/spring-quartz.xml" />

quartz-scheduler定时器实现的更多相关文章

  1. springMVC + quartz实现定时器(任务调度器)

    首先我们要知道任务调度器(定时器)有几种,这边我会写三种 第一种是基于JDK的本身的一个定时器(优点:简单,缺点:满足不了复杂的需求) package com.timer1; import java. ...

  2. SpringBoot集成Quartz实现定时器

    SpringBoot+Quartz实现定时器,由于本人也是刚学习,不足之处请各位大神指正 .. 1.pom配置   <dependency>   <groupId>org.sp ...

  3. spring集成quartz scheduler

    创建项目 有两种创建quart配置作业 1.使用MethodInvokingJobDetailFactoryBean  Quartz Scheduler 配置作业(MethodInvokingJobD ...

  4. Table of Contents - Quartz Scheduler

    Getting Started Hello World Integration with Spring Quartz Scheduler Developer Guide Usage of JobDat ...

  5. Quartz Scheduler(2.2.1) - hello world

    简单示例 1. maven 依赖 <dependencies> <dependency> <groupId>org.quartz-scheduler</gro ...

  6. Quartz Scheduler(2.2.1) - Working with JobStores

    About Job Stores JobStores are responsible for keeping track of all the work data you give to the sc ...

  7. Quartz Scheduler 开发指南(1)

    Quartz Scheduler 开发指南(1) 原文地址:http://www.quartz-scheduler.org/generated/2.2.2/html/qtz-all/ 实例化调度程序( ...

  8. 最新 Spring 4.2.2 集成 Quartz Scheduler 2.2.2 任务调度示例

    参考http://blog.csdn.net/defonds/article/details/49496895 本文将演示如何通过 Spring 使用 Quartz Scheduler 进行任务调度. ...

  9. 整合shiro出现【Correct the classpath of your application so that it contains a single, compatible version of org.quartz.Scheduler】

    跑的时候出现错误: Description: An attempt was made to call the method org.quartz.Scheduler.getListenerManage ...

  10. 使用spring整合Quartz实现—定时器

    使用spring整合Quartz实现—定时器(Maven项目做演示) 不基于特定的基类的方法 一,开发环境以及依赖的jar包 Spring 4.2.6.RELEASE Maven 3.3.9 Jdk ...

随机推荐

  1. 搭建Spark高可用集群

      Spark简介 官网地址:http://spark.apache.org/ Apache Spark™是用于大规模数据处理的统一分析引擎. 从右侧最后一条新闻看,Spark也用于AI人工智能 sp ...

  2. P2154 [SDOI2009]虔诚的墓主人 树状数组

    https://www.luogu.org/problemnew/show/P2154 题意 在一个坐标系中,有w(1e5)个点,这个图中空点的权值是正上,正下,正左,正右各取k个的排列组合情况.计算 ...

  3. 复习+dfs

    1.参考:https://www.cnblogs.com/ckxkexing/p/8466097.html 这道题自己写过,还写过blog,但是第二次写还是不会. (于是开坑,想做做dfs的整理.

  4. Codeforces 948D Perfect Security

    Perfect Security 题意:给你一个A[i]数组, 再给你一个B[i]数组, 现在用选取 B[i] 数组中的一个 去和 A[i] 数组里的一个元素去进行异或操作, B[i]数组的元素只能用 ...

  5. springmvc使用JSR-303对复杂对象进行校验

    JSR-303 是JAVA EE 6 中的一项子规范,叫做Bean Validation,官方参考实现是Hibernate Validator.此实现与Hibernate ORM 没有任何关系.JSR ...

  6. springmvc——@InitBinder注解

    转自http://www.cnblogs.com/douJiangYouTiao888/p/6765220.html 有些类型的数据是无法自动转换的,比如请求参数中包含时间类型的数据,无法自动映射到C ...

  7. centos7 kubernetes单机安装

    单机版的kubernetes 适合初学者,对kuber有个很好的入门. 因为centos系统内置了安装源.我们可以直接安装 1.yum install -y etco kubernetes 2.whe ...

  8. springboot使用memcache缓存

    Memcached简介 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的 ...

  9. 2019年江苏高考数学真题LaTeX排版

    文档pdf中点击以下链接,可进行下载! https://hoganbin.top/post/2531000494/2019%E5%B9%B4%E6%B1%9F%E8%8B%8F%E9%AB%98%E8 ...

  10. 为什么有的编程规范要求用 void 0 代替 undefined

    Undefined Undefined 类型表示未定义,它的类型只有一个值,就是 undefined. 任何变量在被赋值前它的值都是 undefined,但是在 JavaScript 引擎中,unde ...