加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 教程 > 正文

Spring 中配置定时调度两种方法详解

发布时间:2021-12-11 12:08:31 所属栏目:教程 来源:互联网
导读:Spring 中配置定时调度两种方法介绍 方法一: 直接用jdk api的Timer类,无需配置spring文件 1、用@compent注解,实现InitializingBean接口 ,spirng会自动去查找afterPropertiesSet()方法, 2、在afterPropertiesSet方法中写业务实现,调用timer的schedule方

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>

(编辑:PHP编程网 - 黄冈站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读