监听器ServletContextListener小记

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

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

   Listener:
   Java代码:
public class GenPagesListener implements ServletContextListener
{
        private DoSomething ds = null;
       
    public void contextInitialized(ServletContextEvent sce) {
           
            String rootPath = new File(sce.getServletContext().getRealPath("/")).getParentFile().toString();
                if(rootPath.contains("//"))
                {
                        rootPath = rootPath.replace("//", "/")+"/ROOT";
                }
                String hostPath = AppPropertyUtil.getProperty("webUrl");
               
               
                GlobeContext.setRootPath(rootPath);
                GlobeContext.setHostPath(hostPath);
           
            ds = new DoSomething(1000,1800000);
    }

    public void contextDestroyed(ServletContextEvent sce) {
       ds.destoryTimer();
    }


}
==============
中间类:
 public class DoSomething {

        private java.util.Timer timer;
        public DoSomething(int a,int b)
        {
                timer = new Timer();
                timer.schedule(new GenPageTask(), a,b);
        }
       
        public void destoryTimer(){
            timer.cancel();
            }
}

==========
  要执行的任务:
public class GenPageTask extends TimerTask{
       
        public void run()
        {
                ApplicationContext context = GlobeContext.getApplicationContext();
                IChannelManager channelManager = (IChannelManager)context.getBean("channelManager");
                List<Channel> list = channelManager.getObjectList(Restrictions.eq("type", 1));       
                               
                for(Channel ch : list)
                {
                        Long lid = ch.getId();
                        try{
                        publishChannel(lid);
                        }
                        catch(Exception e)
                        {
                           e.printStackTrace();       
                        }
                }
        }
       
        public void publishChannel(Long lid) throws Exception
        {

                String root = GlobeContext.getRootPath();
                String rootpath = GlobeContext.getHostPath();
                ApplicationContext context = GlobeContext.getApplicationContext();
                IChannelManager channelManager = (IChannelManager)context.getBean("channelManager");
                ChannelView chan = channelManager.getView(lid);
                String puppath = root + chan.getFullPath();
                File file = new File(puppath);
                if (!file.exists()) {
                        file.mkdirs();
                }

                int pageSize = Integer.valueOf(AppPropertyUtil
                                .getProperty("listPageSize"));
                if (chan.getPageSize() != null) {
                        pageSize = chan.getPageSize();
                }
                try {
                        List<Criterion> cris = new ArrayList<Criterion>();
                        cris.add(Restrictions.eq("channelId", chan.getId()));
                        cris.add(Restrictions.eq("struts", 1));
                        IDocumentManager documentManager = (IDocumentManager)context.getBean("documentManager");
                        int totalPage = documentManager.getPageObject(0, pageSize, cris)
                                        .getTotalPage();

                        for (int page = 1; page <= totalPage; page++) {
                                String index = "";
                                if (page == 1) {
                                        index = "index";
                                } else {
                                        index = "list_" + page + "";
                                }
                                String url = rootpath
                                                + "admin/channel/docList.htm?data.channelId="
                                                + chan.getId() + "&data.page=" + page;
                                HtmlGenUtil.writeHtml(puppath + index + ".shtml", HtmlGenUtil
                                                .getHtmlCode(url), "YES");
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值