公司部分项目提出将某些项目进行静态化...所以在测试页面命中率和数据库访问量之后针对某些写的频率比较低和访问比较大页面进行静态化。当然也不是进行实时的静态化.这里需要使用定时器来进行静态化的控制.下面了解一下定时器的应用!
1.具体方法的了解
(1)timer.schedule(timertask task,date time)//安排在制定的时间执行指定的任务。
(2)timer.schedule(timertask task,date firsttime ,long period)//安排指定的任务在指定的时间开始进行重复的固定延迟执行.
(3)timer.schedule(timertask task,long delay)//安排在指定延迟后执行指定的任务.
(4)timer.schedule(timertask task,long delay,long period)//安排指定的任务从指定的延迟后开始进行重复的固定延迟执行.
(5)timer.scheduleatfixedrate(timertask task,date firsttime,long period)//安排指定的任务在指定的时间开始进行重复的固定速率执行.
(6)timer.scheduleatfixedrate(timertask task,long delay,long period)//安排指定的任务在指定的延迟后开始进行重复的固定速率执行.
2.简单的测试方法
java代码
import java.io.ioexception;
import java.util.timer;
public class timertest {
public static void main(string[] args){
timer timer = new timer();
timer.schedule(new mytask(), 1000, 2000);//在1秒后执行此任务,每次间隔2秒
while(true){//这个是用来停止此任务的,否则就一直循环执行此任务了
try {
int ch = system.in.read();
if(ch-'c'==0){
timer.cancel();//使用这个方法退出任务
}
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
static class mytask extends java.util.timertask{
@override
public void run() {
// todo auto-generated method stub
system.out.println("我是来测试的。。我两秒出来一次");
}
这样,每隔两秒钟就会在控制台输出语句。
3.根据实际需要进行例子测试
由于我们刷新一些页面或者数据的时候.都会选择在一天的某个时候或者几天!这里就需要针对某个固定时间来制作这个定时器
/**
* 每天固定时间更新
* 每天9点14分
*/
@test
public void testregular() {
new date();
timer timer = new timer();
timer.schedule(new mytask(getregulardate("hh:mm", "11:21")),getregulardate("hh:mm", "11:21"));//每天固定时间更新,每天11点21分
*
* @author chenjinjie
*
static class mytask extends java.util.timertask{
private date time;
public mytask() {
}
public mytask(date time) {
this.time = time;
@override
public void run() {
if(new date().after(time))//这里主要是防止服务器重启之后发觉时间旧了会自动刷新一次
system.out.println("我是来测试的。。固定时间"+time);
}
/**
* 根据日期字符串获取日期对象,精确到当天凌晨零点零分,字符串格式:"yyyy-mm-dd"
* @param fomatstr 如(yyyy年mm月dd日 hh:mm)
* @param datesource 源日期(string)
* @return
public static date getregulardate(string formatdata,string datestring){
simpledateformat formatter = new simpledateformat (formatdata);
date d = null;
try {
d = formatter.parse(datestring);
} catch (parseexception e) {
e.printstacktrace();
calendar calendar = calendar.getinstance();
calendar.set(calendar.hour_of_day, d.gethours());
calendar.set(calendar.minute, d.getminutes());
calendar.set(calendar.second, d.getseconds());
date time = calendar.gettime();
return time;
以上只是初步简单的使用..
4.定时器在jsp中的应用。在jsp中可以靠监听器和定时器的结合来解决某些问题
4.1 先来一个定时器的任务:
public class task extends timertask {
private static final int c_schedule_hour = 0;
private static boolean isrunning = false;
private servletcontext context = null;
public task() {
}
public task(servletcontext context) {
this.context = context;
public void run() {
calendar cal = calendar.getinstance();
if (!isrunning) {
context.log("开始执行指定任务");
system.out.println("执行定时任务");
context.log("指定任务执行结束");
}
else {
context.log("上一次任务执行还未结束");
}
4.2然后监听器(注意实现的接口):
public class contextlistener
extends httpservlet
implements servletcontextlistener {
public contextlistener() {
}
private java.util.timer timer = null;
public void contextinitialized(servletcontextevent event) {
timer = new java.util.timer(true);
event.getservletcontext().log("定时器已启动");
timer.schedule(new task(event.getservletcontext()), 0, 10*60*1000); //开启后.每10分钟刷新一次
event.getservletcontext().log("已经添加任务调度表");
public void contextdestroyed(servletcontextevent event) {
timer.cancel();
event.getservletcontext().log("定时器销毁");
}
4.3最后配置xml文件。
web-app 标签内添加
<listener>
<listener-class>cn.read.web.listener.contextlistener </listener-class>
</listener>
这样启动tomcat后就会在控制台输出当前日期。。
5.项目的具体使用和感想
这只是在项目的初步规划.可能实行过程中还会出现很多问题.比如网络和服务器重启等...不过会在后面开发中继续努力改进.使其功能更加完善.