Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Spring 中配置定时调度两种方法介绍

Spring 中配置定时调度两种方法介绍方法一:直接用jdk api的Timer类,无需配置spring文件1、用@compent注解,实现InitializingBean接口 ,spirng会自动去查找afterPropertiesSet()方法,2、在afterPropertiesSet方法中写业务实现,调用timer的schedule方法或者scheduleAtFixedRate方法          schedule(TimerTask task, Date time)
          安排在指定的时间执行指定的任务。          scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
          安排指定的任务在指定的时间开始进行重复的固定速率执行。代码实现如下:import java.util.Calendar;
import java.util.Date;
import java.util.Timer;import java.util.TimerTask;
import javax.annotation.Resource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import com.cssweb.payment.account.service.support.AccountServiceSupport;@Component
public class GetTimer implements InitializingBean{
 
 private Date date;
 
 @Override
 public void afterPropertiesSet() throws Exception {
  init();//初始化参数,每天凌晨00:00:00开始作业
  //System.out.println("时间是:============="+date);
  new Timer().schedule(test(), date);  //test()为自己要处理的业务实现方法
 }
 
 /** 
 * 初始化参数 
 *
 */
  public void init(){ 
   Calendar cal = Calendar.getInstance();
   cal.set(Calendar.HOUR_OF_DAY, 0);
   cal.set(Calendar.SECOND, 0);
   cal.set(Calendar.MINUTE, 0);
   cal.add(Calendar.DAY_OF_MONTH, 1);
   date = cal.getTime();
 }
 public static TimerTask test(){
  return new TimerTask() {
 @Override
 public void run() {
    System.out.println(new Date()+"开始了------------------------------");
 }
  };
 }
}方法二:用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:jee="http://www.springframework.org/schema/jee"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
   
 <bean id="remindSchedule" class="com.checking_in_remind.controller.Remind" /> //找到对应的类名
 
 <!-- 定义调用对象和调用对象的方法(jobDetail) -->
  <bean id="remindtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <!-- 注册定时任务执行时调用的类 -->
  <property name="targetObject" ref="remindSchedule" />
  <!-- 注册定时任务执行时调用的方法 -->
 <property name="targetMethod" value="getPersonInfo" />
  <!-- 此参数为false等于JobDetail对象实现了Stateful接口,jobs不会并发运行-->
 <property name="concurrent" value="false" />
 </bean>
 
 <bean id="remindCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail" ref="remindtask" />
  <!-- 每5分钟执行一次任务调度 -->
  <property name="cronExpression" value="0 */5 * * * ?"/>
 </bean>
</beans>Spring中如何配置Hibernate事务 http://www.linuxidc.com/Linux/2013-12/93681.htmStruts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm基于 Spring 设计并实现 RESTful Web Services http://www.linuxidc.com/Linux/2013-10/91974.htmSpring-3.2.4 + Quartz-2.2.0集成实例 http://www.linuxidc.com/Linux/2013-10/91524.htm使用 Spring 进行单元测试 http://www.linuxidc.com/Linux/2013-09/89913.htm运用Spring注解实现Netty服务器端UDP应用程序 http://www.linuxidc.com/Linux/2013-09/89780.htmSpring 3.x 企业应用开发实战 PDF完整高清扫描版+源代码 http://www.linuxidc.com/Linux/2013-10/91357.htmSpring 的详细介绍:请点这里
Spring 的下载地址:请点这里本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-11/108905.htm