首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

jee中兑现定时执行

2012-08-22 
jee中实现定时执行编写一个监听器,并在web.xml中注册.package com.backup.listenerimport java.util.Time

jee中实现定时执行

编写一个监听器,并在web.xml中注册.
package com.backup.listener;
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class AppListener implements ServletContextListener{
?? ?private Timer timer=null;???
?? ?public void contextDestroyed(ServletContextEvent event) {
?? ??? ?if(timer!=null){
?? ??? ??? ?timer.cancel();
?? ??? ?}
?? ??? ?event.getServletContext().log("定时器销毁");
?? ?}
?? ?public void contextInitialized(ServletContextEvent event) {
?? ??? ?timer?? =?? new?? java.util.Timer(true);??? ?
?? ??? ?event.getServletContext().log("定时器已启动");
?? ??? ?timer.schedule(new MyTask(event.getServletContext()),0,1000);??? ?
?? ??? ?event.getServletContext().log("已经添加任务调度表");
?? ?}
}

TimerTask类:
package com.backup.listener;
import java.util.Calendar;
import java.util.Date;
import java.util.TimerTask;
import javax.servlet.ServletContext;
public class MyTask extends TimerTask {
??? private static final int C_SCHEDULE_HOUR = 0;
??? private static boolean isRunning = false;
??? private ServletContext context = null;
??? public static boolean isSunday=false;
??? public MyTask(ServletContext context) {
??? ??? this.context = context;
??? }
??? public?? void?? run(){
??? ?? context.log("看一看是不是不断的执行");
?????????? Calendar?? cal?? =?? Calendar.getInstance();
?????????? if(!isRunning){????
??????? ??? ?System.err.println("开始执行任务");
?????????????? if(C_SCHEDULE_HOUR?? ==?? cal.get(Calendar.HOUR_OF_DAY))?? {
??????????? ??? ?????? isRunning?? =?? true;????????????????????????????????????
?????????????????????? context.log("开始执行指定任务");????
??????????????????????????
?????????????????????? //TODO?? 添加自定义的详细任务,以下只是示例????
?????????????????????? int?? i?? =?? 0;????
?????????????????????? while?? (i++?? <?? 10)?? {????
?????????????????????????????? context.log("已完成任务的"?? +?? i?? +?? "/"?? +?? 10);????
?????????????????????? }????
?????????????????????? isRunning? =? false;????
?????????????????????? context.log("指定任务执行结束");
??????????? ??? ?? }
?????????? }else{????
??????????????? context.log("上一次任务执行还未结束");????
?????????? }????
???? }????
}

从网上找的也不错:

web.xml

<servlet >
???? <servlet-name >taskservlet</servlet-name >
??? <servlet-class >common.timer.ConvergeDataServlet</servlet-class >
??? <init-param >
???? <param-name >startTask</param-name >
???? <param-value >true</param-value >
??? </init-param >
??? <init-param >
???? <param-name >intervalTime</param-name >
???? <param-value >240</param-value >
??? </init-param >
??? <load-on-startup >300</load-on-startup >
</servlet >

?

ConvergeDataServlet.java

package common.timer;

import java.io.IOException;
import java.util.Timer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ConvergeDataServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;

private Timer timer1 = null;

private Task task1;

/**??
* Constructor of the object.??
*/
public ConvergeDataServlet() {
?? super();
}

/**??
* Destruction of the servlet.
*/
public void destroy() {
?? super.destroy();
?? if (timer1 != null) {
??? timer1.cancel();
?? }
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
??? throws ServletException, IOException {

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
??? throws ServletException, IOException {
?? doGet(request, response);
}

// init方法启动定时器???
public void init() throws ServletException {

?? ServletContext context = getServletContext();

?? // (true为用定时间刷新缓存)???
?? String startTask = getInitParameter("startTask");

?? // 定时刷新时间(分钟)???
?? Long delay = Long.parseLong(getInitParameter("intervalTime")) * 60 * 1000;
?? // 启动定时器???
?? if (startTask.equals("true")) {
??? timer1 = new Timer(true);
??? task1 = new Task(context);
??? timer1.schedule(task1, delay, delay);
?? }
}

}

Task.java

package common.timer;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.TimerTask;
import javax.servlet.ServletContext;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import tobacco.dayreport.DayreportDAO;
import tobacco.template.service.TobaccoDAOFactory;

public class Task extends TimerTask {

private Logger logger = LogManager.getLogger(Task.class);
private ServletContext context;
private static boolean isRunning = true;

public Task(ServletContext context) {
?? this.context = context;
}

public Task() {
}

@Override
public void run() {
?? Date d = new Date();
?? if (isRunning) {
?? }
??
?? DayreportDAO dao = (DayreportDAO) TobaccoDAOFactory.getDAO("Dayreport");
?? try {
??? Timestamp t = new Timestamp(d.getTime());
??? dao.dayReports(t);
??? logger.debug("=========== 汇总被执行 ==========" + d.toString());
???
?? } catch (SQLException ex) {
??? ex.printStackTrace();
?? }

}

}

?

?

------------------------------------------------------

Java定时任务的实现
本例依据Java自身提供的接口实现,通过监听器(Listener)和定时器(Timer)定时执行某个任务(Task)。
专业的开源工具可参考Quartz:http://www.opensymphony.com/quartz/

MyListener:
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyListener implements ServletContextListener {
??
??private Timer timer = null;

??public void contextInitialized(ServletContextEvent event) {
???? timer = new Timer(true);
????//设置任务计划,启动和间隔时间
???? timer.schedule(new MyTask(), 0, 86400000);
?? }

??public void contextDestroyed(ServletContextEvent event) {
???? timer.cancel();
?? }
??
}
MyTask:
import java.util.TimerTask;

public class MyTask extends TimerTask {

??public void run() {
????// System.out.println("call at " + (new Date()));
????// TODO 此处添加具体任务代码
?? }

}
web.xml配置:
<listener>
<listener-class>com.fastunit.samples.listener.MyListener</listener-class>
</listener>

?

?

来自:http://love-love-l.blog.163.com/blog/static/2107830420089151561835/

?

?

热点排行