大哥 大姐们 小弟需要定时去取数据库的某个字段的值 然后再做别的处理 查了好像要用到timer 不太懂 不知可否指点一下  最好给点源码谢谢

解决方案 »

  1.   

    我在网上查到的代码
    import java.util.Timer; 
    import java.util.TimerTask; public class VarTimer { 
    private Timer timer; 
    private int iFreq; 
    private boolean bRuning; public VarTimer() { 
    timer = null; 
    bRuning = false; iFreq = 10; 
    } public void start(){ 
    if(!bRuning){ 
    timer = new Timer(); 
    timer.schedule(new TimerTask() { 
    public void run() { 
    bRuning = false; 
    timer.cancel(); 

    } , iFreq * 60 * 1000); 
    bRuning = true; 

    } public boolean getCanSendDo(){ 
    return !bRuning; 


    我以前也写过定时器的代码,回家我给你找找,然后贴上来
      

  2.   

    啊,幸好我的代码存在了我的邮箱里,现在就可以给你
    用Timer类的方法其实很简单,你就new一个Timer,然后
    timer = new Timer(); 
    timer.schedule(new TimerTask() , 60 * 1000);
    这样用,其中new TimerTask() 是实例化一个 TimerTask类,你要定时做什么,就写在TimerTask类的run方法里面,后面60 * 1000是定时时间,以毫秒为单位
    下面是我的详细代码
    import java.util.Timer;
    import javax.servlet.ServletException;
    import org.apache.struts.action.ActionServlet;
    import org.apache.struts.action.PlugIn;
    import org.apache.struts.config.ModuleConfig;/**
     * 服务器启动时,自动运行,获得最新帖子,最新试题,点击率最高试题信息,存到全局变量中,并定时刷新。
     * @author Administrator
     */
    public class GetFivePlugIn implements PlugIn{
      Timer timer=null;
      /**
       * 空构造方法
       */
      public GetFivePlugIn (){
      }
      /**
       * 服务器关闭时,停止Timer的运行。
       */
      public void destroy () {
        timer.cancel ();
      }
      /**
       * 开启服务器,自动运行这个方法,实例化一个Timer对象,开始任务,定时刷新设置为4小时一次。
       * @param servlet 服务器句柄
       * @param config 没用到
       * @throws javax.servlet.ServletException 抛出服务器异常
       */
      public void init (ActionServlet servlet, ModuleConfig config) throws ServletException {
        timer = new Timer ();
        timer.schedule (new DoOnTime (servlet.getServletContext ()),0,14400000);
      } 
    }
    下面是实现一个TimerTask类,当然也可以象我给出的前面那个代码那样直接用
    import com.btsoft.teacheranswer.bean.Exam;
    import com.btsoft.teacheranswer.bean.Ques;
    import com.btsoft.teacheranswer.dao.SelectQues;
    import com.btsoft.teacheranswer.dao.SelectTest;
    import javax.servlet.ServletContext;/**
     * 得到全局信息
     * 最新的5个问题帖子
     * 最新的5个试卷
     * 点击率最高的5个试卷
     * 本类继承于java.util.TimerTask
     * @author Administrator
     */
    public class DoOnTime extends java.util.TimerTask{
      /**
       * ServletContext变量,由GetFivePlugIn传过来
       */
      private ServletContext sc;
      /**
       * Creates a new instance of DoOnTime 
       * @param sc 构造方法,传入一个ServletContext类型的参数
       */
      public DoOnTime (ServletContext sc) {
        setSc (sc);
      }
      
      /**
       * 覆盖方法,当类被实例化时自动运行的方法,获得数据库信息在这里实现
       */
      public void run () {
        //这里写你想定时运行的代码
        //下面是application级别的全局对象,你可以把信息存在里面。当然也可以不用
        //ServletContext sc = getSc ();
        //sc.setAttribute (***,***);
      }
      
      /**
       * 返回ServletContext参数
       * @return ServletContext参数
       */
      public ServletContext getSc () {
        return sc;
      }  
      /**
       * 设置ServletContext参数
       * @param sc ServletContext参数
       */
      public void setSc (ServletContext sc) {
        this.sc = sc;
      }  
    }
      

  3.   

    啊,忘了说明,这个是我以前一个项目的代码,所以注释有点奇怪,呵呵,什么问题帖子什么的是针对我那个项目的,具体代码我去掉了,注释忘了去
    还有这段代码是我用在struts的plugin里面的,这样可以在系统运行时自动加载,如果你不用struts,可以不要导入那些struts包,并且不implements PlugIn就可以了
    希望我的代码对你有帮助
      

  4.   

    当然是用quartz,比timer 好太多
      

  5.   

    quartz是个开源框架,搞定定时非常简单,连思科都用quartz。