定时器任务 TimerTask

来源:互联网 发布:windows lts 编辑:程序博客网 时间:2024/06/01 10:31

1. 继承 TimerTask 类

public class GhDoctorStoppedTimeTask extends TimerTask 


2.在run(){} 方法里执行

public void run() {

int num = 0;
int numstop = 0;
try {
// 扫描省中医停诊
numstop = GhCheckScheduleSZYGuangGu();


} catch (Exception e1) {
e1.printStackTrace();
}
if (0 == num) {
System.out.println("无需更新");
} else {
System.out.println("更新成功");
}
if (0 == numstop) {
System.out.println("无需更新停诊");
} else {
System.out.println("停诊更新成功");
}
}


3.  在类 中调用方法

public class GhDoctorStoppedTimeTaskMain {
    
   private Timer timer = null;         
   public void contextInitialized(ServletContextEvent event){     
    timer = new Timer();  
    timer.schedule(new GhDoctorStoppedTimeTask(),0,1*10000);  //一分钟
//   timer.schedule(new GhDoctorStoppedTimeTask(),0,50*10000);  

  }  
       
    
   
   public void simpleJobTest() {
    timer = new Timer();  
    timer.schedule(new GhDoctorStoppedTimeTask(),0,60*10000);   
   }
   //销毁监听器,停止执行任务     
   public void contextDestroyed(ServletContextEvent event){           
          timer.cancel();     
    }     
}


TimerTask 配置:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc  
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
">
    
    <bean id="MailsendTask" class="com.hy.ghservice.task.MailsendTask"></bean> 
    <bean id="GhOrderTimeTask" class="com.hy.ghservice.task.GhOrderTimeTask"></bean> 
    <bean id="GhDoctorStoppedTimeTask" class="com.hy.ghservice.task.GhDoctorStoppedTimeTask"></bean> 
    <!-- 定时处理订单超过20分钟未确认的 -->
<bean id="autoGettask"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="GhOrderTimeTask" />
<property name="targetMethod" value="run" />
</bean>

<bean id="autoGettaskmail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="MailsendTask" />
<property name="targetMethod" value="run" />
</bean>

<bean id="autoGhDoctorStopped"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="GhDoctorStoppedTimeTask" />
<property name="targetMethod" value="run" />
</bean>




<!-- 配置调度触发器 -->
<bean id="autorun"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="autoGettask" />
<property name="cronExpression"> 
<value>0 */59 * * * ?</value> 
</property>
</bean>
 
<bean id="autorunmail"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="autoGettaskmail" />
<property name="cronExpression"> 
<value>0 0/20 7 * * ?</value> 
</property>
</bean>

<bean id="GhDoctorStoppedTimeTaskAuto"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="autoGhDoctorStopped" />
<property name="cronExpression"> 
<value>0 */1 * * * ?</value> 
</property>
</bean>
    
<!-- 定义核心调度器 调度工厂 -->
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
  <!-- <ref bean="autoTrigger2" />  --> 
  <!-- <ref bean="taskRule"/>  --> 
   <ref bean="autorun" />  
   <ref bean="autorunmail" /> 
   <ref bean="GhDoctorStoppedTimeTaskAuto" />  
</list>
</property>
</bean>


</beans>








原创粉丝点击