java定时任务Timer,Quartz
来源:互联网 发布:餐饮如何分析营业数据 编辑:程序博客网 时间:2024/05/16 09:26
1.一般可以使用数据库的定时任务来完成需求,比较方便
2.Timer
TimerTask
package test;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.TimerTask;public class TimeTask extends TimerTask{private String Name;private int n=0;public TimeTask(String name) {this.Name=name;}@Overridepublic void run() {if(n<=2){Calendar c=Calendar.getInstance();SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(f.format(c.getTime()));System.out.println(this.Name);n++;}else{
//暂停当前的线程cancel();System.out.println("cancel");}}}TimerTest
package test;import java.util.Timer;public class TimeTest {public static void main(String[] args) {Timer timer=new Timer();TimeTask timeTask=new TimeTask("hello");timer.schedule(timeTask, 2000, 1000);//设置重复的时间,以及距离什么时候开始}}
缺点:不能处理并发,不能对异常进行处理
Quartz
maven配置
<dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version></dependency><!--跟xml配置后面有关的东西-->
<dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz</artifactId><version>2.2.3</version></dependency>配置的xml
<bean id="simpleJobDetail"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject" ref="myBean" /><property name="targetMethod" value="printMessage" /></bean><!-- <bean id="firstComplexJobDetail"class="org.springframework.scheduling.quartz.JobDetailFactoryBean"><property name="jobClass"value="com.imooc.springquartz.quartz.FirstScheduledJob" /><property name="jobDataMap"><map><entry key="anotherBean" value-ref="anotherBean" /></map></property><property name="Durability" value="true"/></bean>--><!-- 距离当前时间1秒之后执行,之后每隔两秒钟执行一次 --><bean id="mySimpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> <property name="jobDetail" ref="simpleJobDetail"/> <property name="startDelay" value="1000"/> <property name="repeatInterval" value="2000"/></bean><!-- 每隔5秒钟执行一次 --><!-- <bean id="myCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="firstComplexJobDetail"/> <property name="cronExpression" value="0/5 * * ? * *"/></bean>--><bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="jobDetails"> <list> <ref bean="simpleJobDetail"/> <!-- <ref bean="firstComplexJobDetail"/>--> </list> </property> <property name="triggers"> <list> <ref bean="mySimpleTrigger"/> <!-- <ref bean="myCronTrigger"/>--> </list> </property></bean>
MyBean类
package com.imooc.springquartz.quartz;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.stereotype.Component;@Component("myBean")public class MyBean {public void printMessage() {// 打印当前的执行时间,格式为2017-01-01 00:00:00Date date = new Date();SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println("MyBean Executes!" + sf.format(date));}}
FirstScheduledJob
package com.imooc.springquartz.quartz;import java.text.SimpleDateFormat;import java.util.Date;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.springframework.scheduling.quartz.QuartzJobBean;public class FirstScheduledJob extends QuartzJobBean{ private AnotherBean anotherBean; public void setAnotherBean(AnotherBean anotherBean){ this.anotherBean = anotherBean; }@Overrideprotected void executeInternal(JobExecutionContext arg0)throws JobExecutionException {Date date = new Date();SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println("FirstScheduledJob Executes!" + sf.format(date));this.anotherBean.printAnotherMessage();}}
AnotherBean类
package com.imooc.springquartz.quartz;import org.springframework.stereotype.Component;@Component("anotherBean")public class AnotherBean {public void printAnotherMessage() {System.out.println("AnotherMessage");}}
阅读全文
0 0
- java定时任务Timer,Quartz
- Java定时任务(Timer、Quartz、Spring、LinuxCron)
- Java执行定时任务(Timer、Quartz)
- JAVA定时任务Timer、Spring Task、Quartz
- Java之旅--定时任务(Timer、Quartz、Spring、LinuxCron)
- java 定时任务 (timer和timetask,quartz,spring,LinuxCron)
- Java之旅--定时任务(Timer、Quartz、Spring、LinuxCron)
- Java之旅--定时任务(Timer、Quartz、Spring、LinuxCron)
- Spring+Quartz && Java.util.Timer 实现定时任务
- java定时任务调度工具Timer和Quartz
- java Timer 定时任务
- Java定时任务Timer
- JAVA定时任务Timer
- Java 定时任务Timer
- Java定时任务Timer
- java Timer 定时任务
- java 定时任务Timer
- java定时任务 timer
- Redis Scan的使用方式以及Spring redis的坑
- com.alibaba.druid.sql.parser.ParserException: syntax error,expect IS, actual EQ id
- php基于laravel框架的批量插入操作
- 作业10.17
- java方法调用中的单分派与多分派
- java定时任务Timer,Quartz
- flush privileges提示Table 'mysql.servers' doesn't exist解决办法
- kurento 6.6.0 安装部署 + 运行官网demo
- html布局
- 【杨辉三角】
- 纯CSS写的加载动画--圆球搜索加载动画
- 2017-10-15练习题
- Android RxActivityResult:优雅的方式实现startActivityForResult
- uva11809 紫书第三章习题