公司部分项目提出将某些项目进行静态化...所以在测试页面命中率和数据库访问量之后针对某些写的频率比较低和访问比较大页面进行静态化。当然也不是进行实时的静态化.这里需要使用定时器来进行静态化的控制.下面了解一下定时器的应用!
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)
(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.简单的测试方法
- 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);
-
- while(true){
- try {
- int ch = System.in.read();
- if(ch-'c'==0){
- timer.cancel();
- }
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- }
- }
-
- static class MyTask extends java.util.TimerTask{
- @Override
- public void run() {
-
- System.out.println("我是来测试的。。我两秒出来一次");
- }
- }
- }
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.根据实际需要进行例子测试
由于我们刷新一些页面或者数据的时候.都会选择在一天的某个时候或者几天!这里就需要针对某个固定时间来制作这个定时器
- public class TimerTest {
-
-
-
-
- @Test
- public void testRegular() {
- new Date();
- Timer timer = new Timer();
- timer.schedule(new MyTask(getRegularDate("HH:mm", "11:21")),getRegularDate("HH:mm", "11:21"));
- }
-
-
-
-
-
- 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);
- }
- }
-
-
-
-
-
-
- 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;
- }
- }
public class TimerTest {
/**
* 每天固定时间更新
* 每天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("上一次任务执行还未结束");
- }
- }
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);
- event.getServletContext().log("已经添加任务调度表");
- }
-
- public void contextDestroyed(ServletContextEvent event) {
- timer.cancel();
- event.getServletContext().log("定时器销毁");
- }
- }
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.项目的具体使用和感想
这只是在项目的初步规划.可能实行过程中还会出现很多问题.比如网络和服务器重启等...不过会在后面开发中继续努力改进.使其功能更加完善.