写了一个自动运行类,让tomcat启动之后自动调用。但是总运行不成功,高手帮看看,谢谢!程序逻辑是这样的:
1、tomcat调用一个定时器;
2、定时器调用run方法;
3、run方法首先到表里去查是否还有没有提醒的内容;
4、如果有,则给相关的人员发送一条短信;
内容涉及三个表,能够保证需要的内容都存在。
另外,还想知道,在调试的时候如何把一些结果打印在屏幕上?从tomcat的日志来看,服务器已经找到这个类,出现了"定时器已启动321" 等字样。但是里面的内容好像没有执行。
public class TimerListener implements ServletContextListener 
{    
private Timer timer = null;  public void contextInitialized(ServletContextEvent event) 
{
int rate; 
try 
{
   rate = 7*24*60*60*1000;
   timer = new Timer(true);
   event.getServletContext().log("定时器已启动321");
   timer.scheduleAtFixedRate(new RemindTask(), 0, rate);
//    timer.scheduleAtFixedRate(new TimedoTask(), 0, rate);
   event.getServletContext().log("已经添加任务调度表321");   
}
catch(Exception e) 
{
e.printStackTrace();
}
}
public void contextDestroyed(ServletContextEvent event) 
{
timer.cancel();   
event.getServletContext().log("定时器销毁244"); 
}}
****************************************************************************public class RemindTask extends TimerTask 
{
  public void run() 
  {    
    try 
{
      Connection conn = JdbcPool.getConnection();      
      Statement stmt = conn.createStatement(); 
      String sqlstr= "select * from sys_mentioninfo where hasmention='no'"; 
      ResultSet rs = stmt.executeQuery(sqlstr); 
      while(rs.next()) 
      {       
        String mentiontime = rs.getString("mentiontime").substring(0,10);
        SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd"); 
        Date mentionTime = new java.util.Date();
        mentionTime = formatter.parse(mentiontime);  // 字符串转换为时间类型
        Date currentTime = new Date();
        String sourcetime = formatter.format(currentTime);  //将日期时间格式化
        long diffDays = (mentionTime.getTime()- currentTime.getTime())/(24*60*60*1000); 
        System.out.print(diffDays);
        if(diffDays<=0)
        {
          stmt = conn.createStatement();
       sqlstr="select loginname from sys_personauth where sdoi='" + rs.getString("sdoi") + "' and serialcode='" + rs.getString("serialcode") + "'";
       ResultSet rs1= stmt.executeQuery(sqlstr);
      if(rs1.next()) 
  {
    rs1.beforeFirst();
    while(rs1.next())
    {
  stmt = conn.createStatement();
  sqlstr = "insert into sys_duanxin(sender,receiver,content,sendtime,ifdo) values('SYSTEM','" + rs1.getString("loginname") + "','" + rs.getString("content") + "','" + sourcetime + "','no')";
  stmt.executeUpdate(sqlstr);
    }
          }
      else
      {
    throw new Exception("no record fit");  
      }
}
        else
    {
    throw new Exception("days not fit");  
      }
      }
    }
catch(Exception ef)
{
   ef.printStackTrace();
}
  } }

解决方案 »

  1.   

    public void scheduleAtFixedRate(TimerTask task,
                                    Date firstTime,
                                    long period)好好看看这个方法第二个参数是什么。。第三个参数是以后每次运行之间的间隔over。
      

  2.   

    你定义int   rate;这里要定义long的.
      

  3.   

    我修改了rate的定义方式为long,但是运行后还是不对,还是没有插入短信。再帮看看吧
      

  4.   

    firstTime 不是Date吗?应该填入一个new Date()吧。。你怎么放了0在那里呢? 
      

  5.   

    但是 同样也有这样的类定义呀。 
    public void scheduleAtFixedRate(TimerTask task,
                                    long delay,
                                    long period)
    0 表示不延迟,马上执行。是不是这么回事呢?我怎么能够知道程序在哪里出了错呢? 没有main函数也不能调试
      

  6.   

    另外,我按你说的把第二个参数改成时间的也不对 scheduleAtFixedRate(new RemindTask(),new Date(System.currentTimeMillis() + 1000),rate); 结果也是一样,调用了定时器,但是里面的内容没有结果
      

  7.   

    有什么错误提示吗?你implements ServletContextListener这个接口
    有加上它的这两个方法吗?    public void contextInitialized(ServletContextEvent arg0) {
                  
        }    public void contextDestroyed(ServletContextEvent arg0) {
                 
        }
      

  8.   

    你这间隔时间太长了,一周才执行一次
    rate   =   7*24*60*60*1000; 
      

  9.   

    知道问题在哪里了,但是不会解决。
    当检索结果是多条记录,如果用while循环就不行;如果只是用if实验第一条,就能够产生短信并插入。不知道这是为什么,似乎和sql exception有关系。
    请问怎么解决呀?对exception一点都不了解