定时器任务 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>
- 定时器任务 TimerTask
- 详解TimerTask定时器任务
- 定时器任务 TimerTask
- 定时器Timer、TimerTask时间任务类
- TimerTask 定时器
- 定时器Timer和定时任务TimerTask的用法
- 定时器 (TimerTask)
- Timer TimerTask 定时器线程
- 定时器TimerTask入门Demo
- java TimerTask定时器
- listener 定时器TimerTask
- timer+timertask 制作定时器
- 定时器Timer以及TimerTask
- Java 中Timer和TimerTask 定时器和定时任务使用的例子
- Java 中Timer和TimerTask 定时器和定时任务使用的例子
- Java 中Timer和TimerTask 定时器和定时任务使用的例子
- Java 中Timer和TimerTask 定时器和定时任务使用的例子
- Java 中Timer和TimerTask 定时器和定时任务使用的例子
- web 前端
- 连接数据库登录 -详细介绍基本的JSP结构- 不使用任何框架
- Java中抽象类与接口的区别
- 13.属性选择器、emment快捷键
- 日期:在原有的时间上加一秒/分钟等
- 定时器任务 TimerTask
- 百度地图API详解之地图坐标系统
- textarea限制字数
- Eclipse中安装Springtools插件的步骤
- Hadoop入门之Mapreduce部分流程解析
- iOS开发之HTTP的请求头和响应头 以及常见的响应状态码
- 正则表达式的贪婪模式与非贪婪模式
- TCP和UDP
- caffe随记(九)---利用FCN和已有的model进行图像语义分割