Web服务里,常需要调用定时任务,已知方法有两种:
第一种:Servlet侦听器(Listener)结合Java定时器 (Timer)
eg:
(1) Servlet侦听器,代码如下:
import
java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyListener implements ServletContextListener ... {
private Timer timer;//定时器
public void contextInitialized(ServletContextEvent event) ...{
// TODO Auto-generated method stub
timer = new Timer(true);
event.getServletContext().log("--------timer start--------");
timer.schedule(new MyTask(event.getServletContext()), 0, 60000);
event.getServletContext().log("---------schedule load---------");
}
public void contextDestroyed(ServletContextEvent event) ...{
// TODO Auto-generated method stub
timer.cancel();
event.getServletContext().log("------timer cancel---------");
}
}
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyListener implements ServletContextListener ... {
private Timer timer;//定时器
public void contextInitialized(ServletContextEvent event) ...{
// TODO Auto-generated method stub
timer = new Timer(true);
event.getServletContext().log("--------timer start--------");
timer.schedule(new MyTask(event.getServletContext()), 0, 60000);
event.getServletContext().log("---------schedule load---------");
}
public void contextDestroyed(ServletContextEvent event) ...{
// TODO Auto-generated method stub
timer.cancel();
event.getServletContext().log("------timer cancel---------");
}
}
(2)web.xml文件里配置
<
listener
>
< listener-class > com.listener.test.MyListener </ listener-class >
</ listener >
< listener-class > com.listener.test.MyListener </ listener-class >
</ listener >
(3)任务定制
import
java.util.TimerTask;
import java.util.Calendar;
import javax.servlet.ServletContext;
public class MyTask extends TimerTask ... {
private ServletContext sc;
private boolean running=false;
private int timer_hour=15;//下午3点
public MyTask()...{}
public MyTask(ServletContext sc)...{
this.sc=sc;
}
@Override
public void run() ...{
// TODO Auto-generated method stub
Calendar calendar= Calendar.getInstance();
int calendar_hour=calendar.get(calendar.HOUR_OF_DAY);//当前小时数
if(!running)...{
running=true;//任务运行中
sc.log("--task--start--");
if(timer_hour == calendar_hour)...{
System.out.println("--go--go--go--");//定时打印语句
}
running=false;//任务结束
sc.log("--task--end--");
}else...{
sc.log("--task is running--");
}
}
}
import java.util.Calendar;
import javax.servlet.ServletContext;
public class MyTask extends TimerTask ... {
private ServletContext sc;
private boolean running=false;
private int timer_hour=15;//下午3点
public MyTask()...{}
public MyTask(ServletContext sc)...{
this.sc=sc;
}
@Override
public void run() ...{
// TODO Auto-generated method stub
Calendar calendar= Calendar.getInstance();
int calendar_hour=calendar.get(calendar.HOUR_OF_DAY);//当前小时数
if(!running)...{
running=true;//任务运行中
sc.log("--task--start--");
if(timer_hour == calendar_hour)...{
System.out.println("--go--go--go--");//定时打印语句
}
running=false;//任务结束
sc.log("--task--end--");
}else...{
sc.log("--task is running--");
}
}
}
第二种:通过spring来管理控制
(1) 定制task
import
java.util.TimerTask;
public class MyTask extends TimerTask ... {
public void run() ...{
System.out.println("--go--go--go--");//定时打印语句
}
}
public class MyTask extends TimerTask ... {
public void run() ...{
System.out.println("--go--go--go--");//定时打印语句
}
}
(2)配置spring配置文件applicationContext.xml
先配置Task
<
bean
id
="myTask"
class
="MyTask"
>
</ bean >
</ bean >
然后配置时间
<!--
将目标任务包装成可调度任务
-->
< bean id ="myTimerTask" class ="org.springframework.scheduling.timer.ScheduledTimerTask" >
<!-- 配置任务调度任务的延迟时间 -->
< property name ="delay" >
< value > 60000 </ value >
</ property >
<!-- 配置两次任务调度之间的时间间隔 -->
< property name ="period" >
< value > 3600000 </ value >
</ property >
<!-- 确定任务调度的目标任务 -->
< property name ="timerTask" >
< ref local ="myTask" />
</ property >
</ bean >
< bean id ="myTimerTask" class ="org.springframework.scheduling.timer.ScheduledTimerTask" >
<!-- 配置任务调度任务的延迟时间 -->
< property name ="delay" >
< value > 60000 </ value >
</ property >
<!-- 配置两次任务调度之间的时间间隔 -->
< property name ="period" >
< value > 3600000 </ value >
</ property >
<!-- 确定任务调度的目标任务 -->
< property name ="timerTask" >
< ref local ="myTask" />
</ property >
</ bean >
最后
<
bean
id
="timerFactory"
class
="org.springframework.scheduling.timer.TimerFactoryBean"
>
< property name ="scheduledTimerTasks" >
<!-- 下面用于配置一系列的任务调度 -->
< list >
< ref local ="myTimerTask" />
</ list >
</ property >
<!-- 设置该任务调度器是否为后台线程,设置为false表示不是后台线程 -->
< property name ="daemon" >
< value > false </ value >
</ property >
</ bean >
< property name ="scheduledTimerTasks" >
<!-- 下面用于配置一系列的任务调度 -->
< list >
< ref local ="myTimerTask" />
</ list >
</ property >
<!-- 设置该任务调度器是否为后台线程,设置为false表示不是后台线程 -->
< property name ="daemon" >
< value > false </ value >
</ property >
</ bean >