閱讀617 返回首頁    go 京東網上商城


抽象模型,嚴謹代碼,開源分享

前言
    首先,感謝Eric對我代碼上的建議,感謝Stone在FTP Lab環境部署上對我的指導。

    今年4月份的時候,做了一個小的項目,當時也沒有去總結整理,現在想想總結整理是很有必要的,這也是一個很好的工作研究的習慣。

    關於項目,不論大小,其實做到極致也不是一件容易的事。隻有做到極致,才算真正的項目經驗;隻有做到極致,才能讓編程真正成為一門藝術;隻有體會編程是一門有趣的藝術時,你的職業生涯才經久不衰,常青不老。

    當然,我現在也隻是一個走在編程藝術道路上的小孩,不停探索,充滿好奇,我也希望誌同道合的同仁們給我指教與分享。

    我相信,隻有開源了,才會有更多更大的進步,才會有活力,有創造力。我支持開源與自由,我唾棄陳舊死板的開發與管理!


項目簡介
    略......

編程點滴

1. 同樣一個文件能隻打開一次搞定的,就隻打開一次,避免頻繁的I/O流操作。比如:讀一個屬性文件,文件中有很多屬性項目,我們可以將屬性項預定義在一個數組裏,當打開屬性文件時,遍曆屬性數組,將得到的屬性鍵值對預存在一個哈希表裏,這樣之後在用到屬性值時,隻需從哈希表裏獲取就可以了。

    String[] propertyArray = new String[] { "templateDirectory", "sourceDirectory", "cm",  
            "filereadyfile", "xmlCMIRPVersion", "expireHour",  
            "meLimitCount", "fileNamePrefix", "subObjectLimitCount" };  
      
    Map<String, String> propertyMap = CommonTool.getPropertyMap(ConstantPool.CM_CONFIGRATION_FILE, propertyArray);  

    public static Map<String, String> getPropertyMap(String cmccCmConfigurationFile, String[] propertyArray) {  
        InputStream inputStream = null;  
      
        Map<String, String> propertyMap = new HashMap<String, String>();  
        int propertyArrayLength = propertyArray.length;  
      
        Properties properties = new Properties();  
      
        try {  
            inputStream = Files.newInputStream(Paths.get(cmccCmConfigurationFile));  
            properties.load(inputStream);  
        } catch (IOException ioe) {  
            LOG.error(ioe.getMessage());  
            ioe.printStackTrace();  
        } finally {  
            IOUtils.closeQuietly(inputStream);  
        }  
      
        for (int index = 0; index < propertyArrayLength; index++) {  
            String propertyName = propertyArray[index];  
            String propertyValue = properties.getProperty(propertyName);  
      
            if (StringUtils.trimToEmpty(propertyValue).equals("")) {  
                throw new NullPointerException("There is no property " + propertyName + " in the configuration file " + cmccCmConfigurationFile);  
            } else {  
                propertyValue = StringUtils.trim(propertyValue);  
            }  
      
            propertyMap.put(propertyName, propertyValue);  
        }  
      
        return propertyMap;  
    }  

2. 我們能計算一次搞定的,就隻計算一次,比如:我們會經常遍曆數組,往往會讓數組長度的計算在循環中計算多次,這是低效的。

int propertyArrayLength = propertyArray.length;  
  
for (int index = 0; index < propertyArrayLength; index++) {  
    ......  
} 

for (int index = 0; index < propertyArray.length; index++) {  
    ......  
} 

3. 我們處理屬性文件時,一定要嚴謹,比如:有個屬性項是表示數量的,當我們獲取到該屬性項時,我們要對其進行數字正則判斷,要不然你得到一個非數字的屬性,你怎麼拿來運算?

public static boolean isDigit(String str) {  
    boolean isDigitFlag = false;  
  
    if (str.matches("(^[-|+]?)\\d+")) {  
        isDigitFlag = true;  
    }  
  
    return isDigitFlag;  
}

4. 我們經常會拚湊,傳參等方式來定義文件名,所以在這種情況下,有必要對文件名合法性與否作出正則判斷。

public static boolean isInvalidFileName(String fileName) {  
    boolean isInvalidFileNameFlag = false;  
  
    if (fileName.matches(".*[\\/:\\*\\?\"<>\\|].*")) {  
        isInvalidFileNameFlag = true;  
    }  
  
    return isInvalidFileNameFlag;  
} 

5. 我們對特殊字符的處理,也要嚴謹,考慮方方麵麵,比如:在文件中將'&'轉換成'&amp;','<'轉換成'&lt;','>'轉換成'&gt;',在這種情況下,你就不能將'&amp;'和'&lt;',以及'&gt;'中的'&'再轉換成'&amp;'了。

    public static String replaceReserveSymbel(String convertPartStr) {  
        if (convertPartStr != null) {  
            if (convertPartStr.contains("&")) {  
                StringBuffer sb = new StringBuffer("");  
                convertPartStr = replaceReserveSymbel(convertPartStr, sb);  
            }  
      
            convertPartStr = convertPartStr.replace("<", "<");  
            convertPartStr = convertPartStr.replace(">", ">");  
      
            convertPartStr = convertPartStr.replace("\\", "\\\\");  
            convertPartStr = convertPartStr.replace("{", "\\{");  
            convertPartStr = convertPartStr.replace("}", "\\}");  
            convertPartStr = convertPartStr.replace("(", "\\(");  
            convertPartStr = convertPartStr.replace(")", "\\)");  
            convertPartStr = convertPartStr.replace(",", "\\,");  
        }  
      
        return convertPartStr;  
    }  
      
    public static String replaceReserveSymbel(String convertPartStr, StringBuffer sb) {  
        int index = convertPartStr.indexOf("&");  
      
        String leftStr = "";  
        String rightStr = "";  
      
        if (index >= 0) {  
            leftStr = convertPartStr.substring(0, index + 1);  
            rightStr = convertPartStr.substring(index + 1);  
      
            sb.append(leftStr);  
      
            if (!rightStr.startsWith("amp;") && !rightStr.startsWith("lt;") && !rightStr.startsWith("gt;")) {  
                sb.append("amp;");  
            }  
      
            replaceReserveSymbel(rightStr, sb);  
        } else {  
            sb.append(convertPartStr);  
        }  
      
        return sb.toString();  
    }  

6. 在多線程編程中,我們很多時候要實現接口Callable<?>, 而非實現接口Runnable,接口Callable<?>給我們帶來的方便是不言而喻的,因為,可以重寫它的方法call(),獲得我們想要的返回值。

    /** 
     * @author shengshu 
     *  
     */  
    public class ConverterTask implements Callable<File> {  
        private static final Logger LOG = Logger.getLogger(ConverterTask.class);  
      
        private File templateFile = null;  
        private File fragmentFile = null;  
        private String exportDirectoryStr = null;  
        private String notificationDirectoryStr = null;  
      
        public String fileNamePrefix = null;  
      
        private static final Date date = new Date();  
      
        public ConverterTask(File fragmentFile, File templateFile, String destDirectoryStr, String notificationDirectoryStr, String fileNamePrefix) {  
            this.fragmentFile = fragmentFile;  
            this.templateFile = templateFile;  
            this.exportDirectoryStr = destDirectoryStr;  
            this.notificationDirectoryStr = notificationDirectoryStr;  
      
            this.fileNamePrefix = fileNamePrefix;  
        }  
      
        public File convertFile(File fragmentFile, File templateFile, String exportDirectoryStr, String notificationDirectoryStr, String fileNamePrefix) {  
      
            ......  
      
            return exportFile;  
        }  
      
        public void convertFile(File fragmentFile, File templateFile, File exportDirectory, File notificationDirectory, String fileNamePrefix) throws Throwable {  
            convertFile(fragmentFile, templateFile, exportDirectory.getAbsolutePath(), notificationDirectory.getAbsolutePath(), fileNamePrefix);  
        }  
      
        @Override  
        public File call() throws Exception {  
            File exportFile = convertFile(fragmentFile, templateFile, exportDirectoryStr, notificationDirectoryStr, fileNamePrefix);  
      
            return exportFile;  
        }  
      
    }  

    private static void initConverter() {  
        LOG.info("Converter thread pool size: " + threadPoolSize);  
        converterThreadPool = Executors.newFixedThreadPool(threadPoolSize);  
      
        converterFutureList = new ArrayList<Future<File>>();  
      
        if (sourceDirectory != null && sourceDirectory.isDirectory()) {  
            fragmentFileCollection = FileUtils.listFiles(sourceDirectory, new String[] { "xml", "XML" }, false);  
        } else {  
            LOG.warn("The directory " + sourceDirectoryStr + " doesn't exist or is invalid!");  
        }  
      
        if (templateDirectory != null && templateDirectory.isDirectory()) {  
            templateFileCollection = FileUtils.listFiles(templateDirectory, new String[] { "xsl", "XSL" }, false);  
        } else {  
            LOG.warn("The directory " + templateDirectoryStr + " doesn't exist or is invalid!");  
        }  
    }  
      
    public void converter() {  
        LOG.info("==================Converter Start==================");  
        startTime = System.currentTimeMillis();  
      
        initConverter();  
      
        if ((fragmentFileCollection != null && !fragmentFileCollection.isEmpty()) && (templateFileCollection != null && !templateFileCollection.isEmpty())) {  
      
            Iterator<File> fragmentFileIterator = fragmentFileCollection.iterator();  
      
            while (fragmentFileIterator.hasNext()) {  
                File fragmentFile = fragmentFileIterator.next();  
                LOG.info("Fragment file: " + fragmentFile);  
      
                Iterator<File> templateFileIterator = templateFileCollection.iterator();  
      
                while (templateFileIterator.hasNext()) {  
                    File templateFile = templateFileIterator.next();  
                    LOG.info("Template file: " + templateFile);  
      
                    ConverterTask converterTask = new ConverterTask(fragmentFile, templateFile, exportDirectoryStr, notificationDirectoryStr, fileNamePrefix);  
      
                    Future<File> converterFuture = converterThreadPool.submit(converterTask);  
      
                    converterFutureList.add(converterFuture);  
                }  
            }  
        }  
      
        destroyConverter();  
      
        endTime = System.currentTimeMillis();  
        deltaTime = endTime - startTime;  
        LOG.info("Converter spend " + deltaTime / 1000 + " seconds");  
        LOG.info("==================Converter End==================");  
    }  
      
    private static void destroyConverter() {  
        LOG.info("===Start to destroy thread pool and delete files under source directory===");  
      
        for (Future<File> future : converterFutureList) {  
            try {  
                File exportFile = future.get();  
      
                if (exportFile.exists()) {  
                    LOG.info("Invoke method call() and return : " + exportFile);  
                    exportFileCollection.add(exportFile);  
                }  
            } catch (InterruptedException ie) {  
                ie.printStackTrace();  
            } catch (ExecutionException ee) {  
                ee.printStackTrace();  
            }  
        }  
      
        converterThreadPool.shutdown();  
        LOG.info("Thread pool is changed as status: SHUTDOWN");  
      
        while (!converterThreadPool.isTerminated())  
            ;  
      
        LOG.info("Thread pool is changed as status: STOP");  
      
        if (!CommonTool.deleteFileCollection(fragmentFileCollection)) {  
            LOG.error("Fail to delete fragment files under directory " + sourceDirectoryStr);  
        }  
      
        LOG.info("===End to destroy thread pool and delete files under source directory===");  
    }  


7. 我們會經常處理多線程,高並發的問題,往往會用到線程池,所以我們既要保證任務都能被執行,又能保證線程池正常終止。

subSpliterThreadPool.shutdown();  
  
while (!subSpliterThreadPool.isTerminated())  
    ; 

8. 在處理XML文件時,對XSLT的應用將會大大簡化我們的代碼邏輯與數量,實現代碼與文本處理的解耦。


待續...

最後更新:2017-04-03 07:57:00

  上一篇:go Linux檔案權限
  下一篇:go 讓Editplus支持sql語法高亮顯示