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");}}





原创粉丝点击