zoukankan      html  css  js  c++  java
  • tomcat 5.5 动态加载类

    转载于:http://www.itxuexiwang.com/a/javadianzishu/tomcat/2016/0225/161.html?1456480735

    开发使用的是tomcat5.5.27,对于WEB-INF/classes下面的类做了修改,tomcat就会就会自动重启,然后紧接着就是内存溢出。调试比较麻烦。昨天研究了一下tomcat的源代码,对类的加载机制作了一点点修改,让它动态加载类,这样调试的时候如果修改了java文件就不用重启 tomcat了。具体步骤如下:

    修改WebappClassLoader.java文件中的modify()方法,
    在745行的
        return (true);注销,修改为:
        // return (true);
        //做了修改,重新装载
        this.reloadresource(i);
        return false;

    然后添加一个对方法reloadresource的实现:
        /**
         * 重新装载的动作包括:
         * 1。重新装载
         * 2。更新最新时间
         * @param i
         */
        private void reloadresource(int i)throws NamingException{
            String path = paths[i];
            String tclassName = path.replaceAll(this.repositories[0], "");
            String name = tclassName.replaceAll("/", ".").replaceAll(".class", "");
            
            resourceEntries.remove(name);
            this.findResourceInternal(name, tclassName); 
            System.out.println("重新加载了类文件:"+tclassName);
            long lastModified =
                ((ResourceAttributes) resources.getAttributes(path))
                .getLastModified();
            lastModifiedDates[i] = lastModified;
            
            //从这里加载的相当于多在paths和lastModifiedDates中多添加了冗余记录,需要删除最后一个
            synchronized (allPermission) {
                int j;
                long[] result2 = 
                    new long[lastModifiedDates.length - 1];
                for (j = 0; j < (lastModifiedDates.length - 1); j++) {
                    result2[j] = lastModifiedDates[j];
                    }
                lastModifiedDates = result2;

                String[] result = new String[paths.length - 1];
                for (j = 0; j < (paths.length - 1); j++) {
                    result[j] = paths[j];
                    }
                paths = result;

            }
        }
    这样就可以动态的由tomcat来加载类了。

  • 相关阅读:
    241. Different Ways to Add Parentheses java solutions
    89. Gray Code java solutions
    367. Valid Perfect Square java solutions
    46. Permutations java solutions
    116. Populating Next Right Pointers in Each Node java solutions
    153. Find Minimum in Rotated Sorted Array java solutions
    判断两颗树是否相同
    求二叉树叶子节点的个数
    求二叉树第k层的结点个数
    将二叉排序树转换成排序的双向链表
  • 原文地址:https://www.cnblogs.com/itxuexiwang/p/5221220.html
Copyright © 2011-2022 走看看