商城网站建设课设,网页编辑工具是什么,wordpress 含演示数据库,十大网络推广公司排名在项目中需要用到定时器的功能#xff0c;比如在特定的时间定时或者在每个一段时间执行一个任务#xff0c;这里就需要使用到定时器。
本文提供两种方式来实现定时器#xff1a;jdk的Timer以及使用spring的触发器
使用Timer定时器方式#xff1a;
第一、编写任务类继承T…在项目中需要用到定时器的功能比如在特定的时间定时或者在每个一段时间执行一个任务这里就需要使用到定时器。
本文提供两种方式来实现定时器jdk的Timer以及使用spring的触发器
使用Timer定时器方式
第一、编写任务类继承TimerTask重写run方法
package com.syc.timer;import java.util.TimerTask;public class MyTimerTask extends TimerTask{private String name;public MyTimerTask(String name) {this.name name;}Overridepublic void run() {System.out.println(输入的姓名是name);}}
第二、测试类中创建Timer对象设置任务及调度策略
package com.syc.timer;import java.util.Timer;public class TestTimerTask {public static void main(String[] args) {MyTimerTask myTimerTask new MyTimerTask(李沁);Timer timer new Timer();timer.schedule(myTimerTask, 2000,1000);}
}
本文重点不在介绍Timer的使用项目中Quartz集成spring的方式才是最为常用的即QuartzJobBean的使用。关键对象分为三个
1、调度工作类org.springframework.scheduling.quartz.JobDetailBean该对象通过jobClass属性指定调度工作类
2、调度触发器org.springframework.scheduling.quartz.CronTriggerBean该对象通过jobDetail属性指定工作类通过
cronExpression属性指定调度频率
3、调度工厂类org.springframework.scheduling.quartz.SchedulerFactoryBean该对象通过triggers属性指定单个或多个触发器。
一、所需的jar包
将spring需要用到的41先全部导入4个核心一个依赖spring-core-3.2.0.RELEASE.jarspring-beans-3.2.0.RELEASE.jar spring-context-3.2.0.RELEASE.jar spring-expression-3.2.0.RELEASE.jarcom.springsource.org.apache.commons.logging-1.1.1.jar
再导入另外几个包spring-support-2.0.8.jarspring-tx-3.2.0.RELEASE.jarcommons-collections-3.2.jarcommons-logging-1.1.jarquartz-1.7.3.jar
二、创建项目导入jar包
将上述包导入其中mysql和ojdbc是我的项目中两个数据库的驱动可以不用导入 三、编写工作类
package com.syc.springTrigger;import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;//工作类
public class Job extends QuartzJobBean{private int timeout 10;//间隔多长时间调度开始private static int i 0;//JobDetailBean通过jobDataAsMap注入此属性值指定当服务器启动后过多少秒钟首次调用工作类public void setTimeout(int timeout) {this.timeout timeout;}//需要调度的任务Overrideprotected void executeInternal(JobExecutionContext arg0)throws JobExecutionException {System.out.println(QuartzJobBean调度 i 进行中...);i;}}四、spring配置文件
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd!-- JobDetailBean 1、工作类配置 --bean namejob classorg.springframework.scheduling.quartz.JobDetailBeanproperty namejobClass valuecom.syc.springTrigger.Job /property namejobDataAsMapmapentry keytimeout value10 /!-- Job中的 timeout属性10秒后启用调度--/map/property/bean!-- CronTriggerBean 2、触发器配置--bean idcronTrigger classorg.springframework.scheduling.quartz.CronTriggerBeanproperty namejobDetail refjob /!-- 注入1步骤中的工作类 --!-- 每隔一秒调度一次 --property namecronExpression value0/1 * * * * ? //bean!-- SchedulerFactoryBean 3、配置调度工厂 --bean classorg.springframework.scheduling.quartz.SchedulerFactoryBeanproperty nametriggerslistref beancronTrigger /!-- 注入2步骤中触发器 --/list/property/bean
/beans
五、测试类只需加载spring的配置文件即可
package com.syc.springTrigger;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestJob {public static void main(String[] args) {String xmlPath com/syc/springTrigger/beans.xml;ApplicationContext applicationContext new ClassPathXmlApplicationContext(xmlPath);}}调度结果 六、补充时间调度方法
具体时间设定可参考 0/10 * * * * ? 每10秒触发 0 0 12 * * ? 每天中午12点触发 0 15 10 ? * * 每天上午10:15触发 0 15 10 * * ? 每天上午10:15触发 0 15 10 * * ? * 每天上午10:15触发 0 15 10 * * ? 2005 2005年的每天上午10:15触发 0 * 14 * * ? 在每天下午2点到下午2:59期间的每1分钟触发 0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发 0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发 0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发 0 15 10 ? * MON-FRI 周一至周五的上午10:15触发 0 15 10 15 * ? 每月15日上午10:15触发 0 15 10 L * ? 每月最后一日的上午10:15触发 0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发 0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发 0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发 每隔5秒执行一次*/5 * * * * ? 每隔1分钟执行一次0 */1 * * * ? 每天23点执行一次0 0 23 * * ? 每天凌晨1点执行一次0 0 1 * * ? 每月1号凌晨1点执行一次0 0 1 1 * ? 每月最后一天23点执行一次0 0 23 L * ? 每周星期天凌晨1点实行一次0 0 1 ? * L 在26分、29分、33分执行一次0 26,29,33 * * * ? 每天的0点、13点、18点、21点都执行一次0 0 0,13,18,21 * * ?
参考https://blog.csdn.net/dwz538878/article/details/81563588