Java定时任务(Timer、Quartz、Spring、LinuxCron)

转载自: http://blog.csdn.net/puma_dong/article/details/38032487?utm_source=tuicool&utm_medium=referral

在Java中,实现定时任务有多种方式,本文介绍4种,Timer和TimerTask、Spring、QuartZ、Linux Cron。

以上4种实现定时任务的方式,Timer是最简单的,不需要任何框架,仅仅JDK就可以,缺点是仅仅是个时间间隔的定时器,调度简单;Spring和QuartZ都支持cron,功能都很强大,Spring的优点是稍微简单一点,QuartZ的优点是没有Spring也可使用;Linux Cron是个操作系统级别的定时任务,适用于所有操作系统支持的语言,缺点是精度只能到达分钟级别。



Timer和TimerTask

关于Timer定时器的实现原理,如果我们看过JDK源码,就会发现,是使用的Object.wait(timeout),来进行的线程阻塞,timeout是根据下次执行实际和当前实际之差来计算。实际上,这可以归结为一个多线程协作(协作都是在互斥下的协作)问题。

在java.util.concurrent中,有个ScheduledThreadPoolExecutor,也可以完全实现定时任务的功能。

而其他的框架,无非是功能的增强,特性更多,更好用,都是在基础的java之上的包装。

代码示例如下:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. import java.util.Date;    
  2. import java.util.Timer;    
  3. import java.util.TimerTask;     
  4. public class TimerTest extends TimerTask    
  5. {    
  6.     private Timer timer;         
  7.     public static void main(String[] args)    
  8.     {    
  9.         TimerTest timerTest= new TimerTest();    
  10.         timerTest.timer = new Timer();              
  11.         //立刻开始执行timerTest任务,只执行一次    
  12.         timerTest.timer.schedule(timerTest,new Date());              
  13.         //立刻开始执行timerTest任务,执行完本次任务后,隔2秒再执行一次    
  14.         //timerTest.timer.schedule(timerTest,new Date(),2000);              
  15.         //一秒钟后开始执行timerTest任务,只执行一次    
  16.         //timerTest.timer.schedule(timerTest,1000);              
  17.         //一秒钟后开始执行timerTest任务,执行完本次任务后,隔2秒再执行一次    
  18.         //timerTest.timer.schedule(timerTest,1000,2000);              
  19.         //立刻开始执行timerTest任务,每隔2秒执行一次    
  20.         //timerTest.timer.scheduleAtFixedRate(timerTest,new Date(),2000);             
  21.         //一秒钟后开始执行timerTest任务,每隔2秒执行一次    
  22.         //timerTest.timer.scheduleAtFixedRate(timerTest,1000,2000);    
  23.   
  24.         try    
  25.         {    
  26.             Thread.sleep(10000);    
  27.         } catch (InterruptedException e)    
  28.         {    
  29.             e.printStackTrace();    
  30.         }  
  31.         //结束任务执行,程序终止    
  32.         timerTest.timer.cancel();    
  33.         //结束任务执行,程序并不终止,因为线程是JVM级别的    
  34.         //timerTest.cancel();    
  35.     }      
  36.     @Override    
  37.     public void run()    
  38.     {    
  39.         System.out.println("Task is running!");    
  40.     }    
  41. }   


使用spring @Scheduled注解执行定时任务

这种方式非常简单,却能使用cron完成和QuartZ一样的功能,值得推荐一下。


ApplicationContext.xml:


beans根节点增加内容:xmlns:task="http://www.springframework.org/schema/task"

beans根节点中,xsi:schemaLocation属性下增加:

http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd

<!-- 开启注解任务 -->

<task:annotation-driven/>

实现类:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. @Component  //import org.springframework.stereotype.Component;    
  2. public class MyTestServiceImpl  implements IMyTestService {    
  3.       @Scheduled(cron="0/5 * *  * * ? ")   //每5秒执行一次    
  4.       @Override    
  5.       public void myTest(){    
  6.             System.out.println("进入测试");    
  7.       }    
  8. }  


注意几点:


spring的@Scheduled注解  需要写在实现上;

定时器的任务方法不能有返回值;

实现类上要有组件的注解@Component,@Service,@Repository


QuartZ


QuartZ With Spring


applicationContext-schedule.xml


[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.  xmlns:context="http://www.springframework.org/schema/context"  
  4.  xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  6.            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  7.  <bean id="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  8.   <property name="triggers">  
  9.    <list>  
  10.     <!-- 启动的Trigger列表 -->  
  11.     <ref local="startThriftTrigger" />  
  12.    </list>  
  13.   </property>  
  14.   <property name="quartzProperties">  
  15.    <props>  
  16.     <prop key="org.quartz.threadPool.threadCount">5</prop>  
  17.    </props>  
  18.   </property>  
  19.   <!-- 启动时延期3秒开始任务 -->  
  20.   <property name="startupDelay" value="3" />  
  21.   <property name="autoStartup" value="${scheduler.autoStartup}" />  
  22.  </bean>  
  23.   
  24.   
  25.  <!-- 启动Thrift,立即启动 -->  
  26.  <bean id="startThriftTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">    
  27.   <property name="startDelay" value="0" />    
  28.         <property name="repeatInterval" value="1000" />  
  29.         <property name="repeatCount" value="0" />  
  30.         <property name="jobDetail" ref="startThriftTask" />   
  31.  </bean>  
  32.  <bean id="startThriftTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  33.   <property name="targetObject" ref="startThrift" />  
  34.   <property name="targetMethod" value="execute" />  
  35.   <!-- 同一任务在前一次执行未完成而Trigger时间又到时是否并发开始新的执行, 默认为true. -->  
  36.   <property name="concurrent" value="true" />  
  37.  </bean>  
  38. </beans>  


实现类


[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package xx.schedule;  
  2.   
  3. @Component  
  4. public class StartThrift {  
  5.  /** 
  6.   * 调度入口 
  7.   */  
  8.  public void execute() {  
  9.   // to do something  
  10.  }   
  11. }  


QuartZ No Spring


使用的QuartZ jar是1.8.5,如下:

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <dependency>  
  2.     <groupId>org.quartz-scheduler</groupId>  
  3.     <artifactId>quartz</artifactId>  
  4.     <version>1.8.5</version>  
  5. </dependency>  


调用类实现代码如下:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. import org.quartz.CronExpression;  
  2. import org.quartz.CronTrigger;  
  3. import org.quartz.JobDetail;  
  4. import org.quartz.Scheduler;  
  5. import org.quartz.SchedulerException;  
  6. import org.quartz.SchedulerFactory;  
  7. import org.quartz.impl.StdSchedulerFactory;  
  8.   
  9. public class InvokeStatSchedule {     
  10.     public void start() throws SchedulerException  
  11.     {  
  12.         SchedulerFactory schedulerFactory = new StdSchedulerFactory();  
  13.         Scheduler scheduler = schedulerFactory.getScheduler();  
  14.   
  15.         //InvokeStatJob是实现了org.quartz.Job的类  
  16.         JobDetail jobDetail = new JobDetail("jobDetail""jobDetailGroup", InvokeStatJob.class);  
  17.         CronTrigger cronTrigger = new CronTrigger("cronTrigger""triggerGroup");  
  18.         try {  
  19.             CronExpression cexp = new CronExpression("0 0 * * * ?");  
  20.             cronTrigger.setCronExpression(cexp);  
  21.         } catch (Exception e) {  
  22.             e.printStackTrace();  
  23.         }          
  24.         scheduler.scheduleJob(jobDetail, cronTrigger);  
  25.         scheduler.start();  
  26.     }     
  27. }  

定时任务类代码如下:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. import org.quartz.Job;  
  2. import org.quartz.JobExecutionContext;  
  3. import org.quartz.JobExecutionException;  
  4.   
  5. public class InvokeStatJob implements Job {  
  6.   
  7.     @Override  
  8.     public void execute(JobExecutionContext arg0) throws JobExecutionException {  
  9.         //...要定时操作的内容     
  10.     }  
  11. }  


Linux Cron

这其实也是一种非常普遍的实现定时任务的方式,实际是操作系统的定时任务。Linux Cron只能到达分钟级,到不了秒级别。

一般我们是设置定时执行一个sh脚本,在脚本里面写一些控制代码,例如,有如下的脚本:

3,33 * * * * /usr/local/log_parser/run_log_parser.sh &


run_log_parser.sh的内容大致如下:


[plain]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. #!/bin/sh  
  2.   
  3. log_parser_dir=/usr/local/log_parser  
  4. tmp_file=/usr/local/run_parser_tmp.txt  
  5. parser_log=/usr/local/access_parser.log  
  6. tmpDir=/data/applogs/access_logs_bp  
  7.   
  8. date >> "$parser_log"  
  9.   
  10.   
  11. if [! -f "$tmp_file"]; then  
  12.         echo '访问日志解析正在进行,尚未完成' >> "$parser_log"  
  13.         echo '' >> "$parser_log"  
  14. else  
  15.         echo '开始解析访问日志' >> "$parser_log"  
  16.         touch "$tmp_file"  
  17.         cd "$log_parser_dir"  
  18.         python access_log_parser.py >> "$parser_log"  
  19.         rm "$tmp_file"  
  20.         echo '解析访问日志完成' >> "$parser_log"  
  21.         echo '' >> "$parser_log"  
  22.         cd "$tmpDir"  
  23.         gzip gzip WEB0*  
  24.         mv *.gz gz/  
  25.         echo '压缩备份日志及移动到压缩目录成功' >> "$parser_log"  
  26. fi  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值