Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3341048
  • 博文数量: 1450
  • 博客积分: 11163
  • 博客等级: 上将
  • 技术积分: 11101
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-25 14:40
文章分类

全部博文(1450)

文章存档

2017年(5)

2014年(2)

2013年(3)

2012年(35)

2011年(39)

2010年(88)

2009年(395)

2008年(382)

2007年(241)

2006年(246)

2005年(14)

分类: 系统运维

2008-11-27 09:18:05

今天奉命写一个监听器监听tomcat,在tomcat启动后隔半个小时生成一些要求的静态页面,因为没写过监听器,所以在网上看了一些资料后觉得不是很难就很兴奋地开始动手,
   用了ServletContextListener,可没想到监听是监听到了,但是搞得tomcat启动的时候一路在监听,以为是这个监听器不适合就换成了session的,以为快成功的
   时候老大又说不行(后来一想也确实不行),所以又换回ServletContextListener来继续搞,后来上网看来看去原来用这个监听器才是符合要求的,一开始那种
   只不过是因为我把定时器Timer定义在了ServletContextListener的初始化函数里面了,搞得tomcat启动时老是在监听而启动不了,后来我把定时器Timer定义在
   另外一个类里面,再在ServletContextListener的初始化函数里面调用就搞定了。还是没经验啊。

        不过这里还有个问题:如何在这个初始化函数里面获得tomcat的IP和端口呢?也许很难,也许很easy,还不知道!

   Listener:

   
Java代码 复制代码
  1. public class GenPagesListener implements ServletContextListener   
  2. {   
  3.         private DoSomething ds = null;   
  4.            
  5.     public void contextInitialized(ServletContextEvent sce) {   
  6.                
  7.             String rootPath = new File(sce.getServletContext().getRealPath("/")).getParentFile().toString();   
  8.                 if(rootPath.contains("\\"))   
  9.                 {   
  10.                         rootPath = rootPath.replace("\\", "/")+"/ROOT";   
  11.                 }   
  12.                 String hostPath = AppPropertyUtil.getProperty("webUrl");   
  13.                    
  14.                    
  15.                 GlobeContext.setRootPath(rootPath);   
  16.                 GlobeContext.setHostPath(hostPath);   
  17.                
  18.             ds = new DoSomething(1000,1800000);   
  19.     }   
  20.   
  21.     public void contextDestroyed(ServletContextEvent sce) {   
  22.        ds.destoryTimer();   
  23.     }   
  24.   
  25.   
  26. }  


   中间类:

  
Java代码 复制代码
  1.  public class DoSomething {   
  2.   
  3.         private java.util.Timer timer;   
  4.         public DoSomething(int a,int b)   
  5.         {   
  6.                 timer = new Timer();   
  7.                 timer.schedule(new GenPageTask(), a,b);   
  8.         }   
  9.            
  10.         public void destoryTimer(){   
  11.             timer.cancel();   
  12.             }   
  13. }  


   要执行的任务:

  
Java代码 复制代码
  1. public class GenPageTask extends TimerTask{   
  2.            
  3.         public void run()   
  4.         {   
  5.                 ApplicationContext context = GlobeContext.getApplicationContext();   
  6.                 IChannelManager channelManager = (IChannelManager)context.getBean("channelManager");   
  7.                 List list = channelManager.getObjectList(Restrictions.eq("type"1));           
  8.                                    
  9.                 for(Channel ch : list)   
  10.                 {   
  11.                         Long lid = ch.getId();   
  12.                         try{   
  13.                         publishChannel(lid);   
  14.                         }   
  15.                         catch(Exception e)   
  16.                         {   
  17.                            e.printStackTrace();           
  18.                         }   
  19.                 }   
  20.         }   
  21.            
  22.         public void publishChannel(Long lid) throws Exception   
  23.         {   
  24.   
  25.                 String root = GlobeContext.getRootPath();   
  26.                 String rootpath = GlobeContext.getHostPath();   
  27.                 ApplicationContext context = GlobeContext.getApplicationContext();   
  28.                 IChannelManager channelManager = (IChannelManager)context.getBean("channelManager");   
  29.                 ChannelView chan = channelManager.getView(lid);   
  30.                 String puppath = root + chan.getFullPath();   
  31.                 File file = new File(puppath);   
  32.                 if (!file.exists()) {   
  33.                         file.mkdirs();   
  34.                 }   
  35.   
  36.                 int pageSize = Integer.valueOf(AppPropertyUtil   
  37.                                 .getProperty("listPageSize"));   
  38.                 if (chan.getPageSize() != null) {   
  39.                         pageSize = chan.getPageSize();   
  40.                 }   
  41.                 try {   
  42.                         List cris = new ArrayList();   
  43.                         cris.add(Restrictions.eq("channelId", chan.getId()));   
  44.                         cris.add(Restrictions.eq("struts"1));   
  45.                         IDocumentManager documentManager = (IDocumentManager)context.getBean("documentManager");   
  46.                         int totalPage = documentManager.getPageObject(0, pageSize, cris)   
  47.                                         .getTotalPage();   
  48.   
  49.                         for (int page = 1; page <= totalPage; page++) {   
  50.                                 String index = "";   
  51.                                 if (page == 1) {   
  52.                                         index = "index";   
  53.                                 } else {   
  54.                                         index = "list_" + page + "";   
  55.                                 }   
  56.                                 String url = rootpath   
  57.                                                 + "admin/channel/docList.htm?data.channelId="  
  58.                                                 + chan.getId() + "&data.page=" + page;   
  59.                                 HtmlGenUtil.writeHtml(puppath + index + ".shtml", HtmlGenUtil   
  60.                                                 .getHtmlCode(url), "YES");   
  61.                         }   
  62.                 } catch (Exception e) {   
  63.                         e.printStackTrace();   
  64.                 }   
  65.         }   
  66. }  
阅读(1401) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~