spring4.2 定时任务

来源:互联网 发布:觉醒字幕组知乎 编辑:程序博客网 时间:2024/06/08 01:05
一.实现定时任务的方式主要有三种 
  1.通过JDK自带的类实现,即:java.util.Timer结合java.util.TimerTask,使用这种方式可以让你的程序按照某一个频度执行,但调度控制非常不方便,需要大量代码,不推荐 获取【下载地址】   
  2.使用Quartz,这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂,推荐 
  3.Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多,稍后会介绍。 

二.实现代码 

第一种,java自带的 
  定时任务执行类 
Java代码  收藏代码
  1. import java.util.TimerTask;  
  2.   
  3. public class TestTask extends TimerTask{  
  4.   
  5.     @Override  
  6.     public void run() {  
  7.         System.out.println("定时任务测试");  
  8.     }  
  9.   
  10. }  

调用定时任务类 
Java代码  收藏代码
  1. import java.util.Date;  
  2. import java.util.Timer;  
  3.   
  4. import javax.servlet.ServletContextEvent;  
  5. import javax.servlet.ServletContextListener;  
  6.   
  7. import org.springframework.web.context.ContextLoaderListener;  
  8.   
  9. public class TimerContext extends ContextLoaderListener implements ServletContextListener{  
  10.   
  11.     @Override  
  12.     public void contextDestroyed(ServletContextEvent arg0) {  
  13.         // TODO Auto-generated method stub  
  14.           
  15.     }  
  16.   
  17.     @Override  
  18.     public void contextInitialized(ServletContextEvent arg0) {  
  19.         Timer timer = new Timer();  
  20.         timer.scheduleAtFixedRate(new TestTask(), new Date(), 2000);  
  21.     }  
  22.   
  23. }  


如果spring框架,可以继承ContextLoaderListener类,在实现ServletContextListener,然后把web.xml文件配置ContextLoaderListener改为: 
Java代码  收藏代码
  1. <listener>  
  2.     <listener-class>com.hebradio.wechat.task.TimerContext</listener-class>  
  3. </listener>  

如果直接用servlet,那就直接实现ServletContextListener,再在web.xml文件配置上面的监听器 

第二种,使用Quartz 

第一步:.配置job,有两种方式 
(1).继承org.springframework.scheduling.quartz.QuartzJobBean 
Java代码  收藏代码
  1. package com.test.task;  
  2.   
  3. import org.quartz.JobExecutionContext;  
  4. import org.quartz.JobExecutionException;  
  5. import org.springframework.scheduling.quartz.QuartzJobBean;  
  6.   
  7. public class TestTask extends QuartzJobBean{  
  8.   
  9.     @Override  
  10.     protected void executeInternal(JobExecutionContext arg0)  
  11.             throws JobExecutionException {  
  12.         System.out.println("定时任务测试");  
  13.           
  14.     }  
  15.   
  16. }  

   配置文件中配置作业类 
   spring4.2没有JobDetailBean,改为:JobDetailFactoryBean 
Java代码  收藏代码
  1. <bean id="jobDetailFactory"  
  2.      class="org.springframework.scheduling.quartz.JobDetailFactoryBean">  
  3.      <property name="jobClass" value="com.test.task.TestTask" />    
  4.      <!--  
  5.      <property name="jobDataMap">  
  6.          <map>    
  7.              <entry key="timeout" value="5" />  
  8.          </map>  
  9.      </property>  -->  
  10.  </bean>  


(2).使用MethodInvokingJobDetailFactoryBean,此时类不用继承任何接口或者类,方法名称可以随便定义 
Java代码  收藏代码
  1. public class TestTask{  
  2.   
  3.     public void test(){  
  4.         System.out.println("定时任务测试");  
  5.     }  
  6.   
  7. }  

Java代码  收藏代码
  1. <bean id="jobDetailFactory"  
  2. class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">   
  3.        <!-- 这个就是具体实现类,如果是注解,则必须为component指定value -->  
  4.        <property name="targetObject">  
  5.         <bean class="com.test.task.TestTask"/>  
  6.        </property>  
  7.        <!-- targetMethod是Spring定时器的特殊属性 -->  
  8.        <property name="targetMethod" value="test" />  
  9.        <!-- false表示job不会并发执行,默认为true -->  
  10.        <property name="concurrent" value="false" />  
  11.    </bean>  


第二步:配置调度任务执行的触发的时间,两种方式 
   (1) 使用SimpleTriggerFactoryBean 
Java代码  收藏代码
  1. <bean id="simpleTrigger"   
  2.     class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">    
  3.     <property name="jobDetail" ref="jobDetailFactory" />    
  4.     <!-- 第一次执行时间多少毫秒之后  -->  
  5.     <property name="startDelay" value="1000" />    
  6.     <!-- 每过多少毫秒执行一次定时任务  -->  
  7.     <property name="repeatInterval" value="5000" />    
  8. </bean>  

   (2) 使用CronTriggerFactoryBean 
Java代码  收藏代码
  1. <bean id="cronTrigger"    
  2.         class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">    
  3.         <property name="jobDetail" ref="jobDetailFactory" />  
  4.        <!-- 每天早上六点执行一次   
  5.         <property name="cronExpression" value="0 0 6 * * ?" /> -->   
  6.        <!--每分执行一次  
  7.         <property name="cronExpression" value="0 0/1 * * * ?" /> -->  
  8.        <!--每5秒执行一次-->  
  9.         <property name="cronExpression" value="0/5 * * * * ?" />   
  10.     </bean>  

  第三步:配置Quartz的调度工厂,调度工厂只能有一个,多个调度任务在list中添加 
Java代码  收藏代码
  1. <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">    
  2.     <property name="triggers">    
  3.         <list>    
  4.             <!-- <ref bean="cronTrigger" /> -->  
  5.             <ref bean="simpleTrigger" />  
  6.         </list>    
  7.     </property>    
  8. </bean>   

第三种,spring自带的 
这种方式在一台电脑上配置成功,但是将代码拷贝到另外两台电脑后,定时任务不执行了,没有找到原因,后来改为上面第二种,如果有知晓的朋友,请告知一下,谢谢 
在spring配置文件头引入下面两行 

Java代码  收藏代码
  1. xmlns:task="http://www.springframework.org/schema/task"  

Java代码  收藏代码
  1. xsi:schemaLocation="...  
  2. http://www.springframework.org/schema/task  
  3. http://www.springframework.org/schema/task/spring-task-4.2.xsd"  


定时任务配置 
<!-- 定时任务执行 driven--> 
Java代码  收藏代码
  1. <task:annotation-driven/>  


Java代码  收藏代码
  1. <context:annotation-config/>  


<!-- 定时任务所在包 --> 
Java代码  收藏代码
  1. <context:component-scan base-package="com.test.task"/>  


Java代码  收藏代码
  1. package com.test.task;  
  2.   
  3. import org.springframework.scheduling.annotation.Scheduled;  
  4. import org.springframework.stereotype.Component;  
  5.   
  6. @Component("testTask")  
  7. public class TestTask{  
  8.       
  9.     @Scheduled(cron="0/5 * * * * ?")//每5秒钟执行一次  
  10.     public void testCron(){  
  11.         System.out.println("定时任务测试");  
  12.     }  
  13.       
  14.     @Scheduled(fixedRate=5000, initialDelay=1000)//第一次服务器启动一秒后执行,以后每过5秒执行一次,具体情况请查看Scheduled的方法  
  15.     public void test() {  
  16.         System.out.println("定时任务测试");  
  17.     }  
  18.   
  19. }  


记得在类上面加@Component,在方法上加@Scheduled 
Scheduled的表达式有两种,即代码中,一种即可,用这种方式,定时任务与定时任务之间是顺序执行的,并没有为每一个定时任务分一个线程
0 0
原创粉丝点击