quartz-scheduler定时器实现

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

        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.3.0</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>
        <!-- 每分钟的第一秒触发   每天零点 0 0 0 * * ? -->
        <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>1</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" />

 

posted @ 2018-10-26 14:30  kerala  阅读(1236)  评论(0编辑  收藏  举报