免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1809 | 回复: 0
打印 上一主题 下一主题

自动运行类运行不正确 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-17 10:05 |只看该作者 |倒序浏览
写了一个自动运行类,让tomcat启动之后自动调用。

程序逻辑是这样的:
1、tomcat调用一个定时器;
2、定时器调用run方法;
3、run方法首先到表里去查是否还有没有提醒的内容;
4、如果有,则给相关的人员发送一条短信;
内容涉及三个表,能够保证需要的内容都存在。

运行结果短信内容没有插到相关的表里去,高手帮看看,谢谢!

从tomcat的日志来看,服务器已经找到这个类,出现了"定时器已启动321"   等字样。但是里面的内容好像没有执行。

public   class   TimerListener   implements   ServletContextListener   
{         
private   Timer   timer   =   null;   

public   void   contextInitialized(ServletContextEvent   event)   
{
long   rate;   
try   
{
      rate   =   7*24*60*60*1000;
      timer   =   new   Timer(true);
      event.getServletContext().log("定时器已启动321");
      timer.scheduleAtFixedRate(new   RemindTask(),   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();
}
    }   

}

[ 本帖最后由 jiangeb 于 2008-1-17 10:06 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP