APDPlat的系统启动和关闭流程剖析
2019獨角獸企業重金招聘Python工程師標準>>>
?APDPlat接管了Spring的啟動關閉權,為各種運行其上的開源框架和類庫的無縫集成提供了支持。
?
當然,大家都知道,一個JAVA EE Web應用的入口點是web.xml,APDPlat當然也不例外,我們看看APDPlat是如何接管Spring的啟動關閉權的:
?
<listener><description>經過定制的spring監聽器</description><listener-class>org.apdplat.platform.spring.APDPlatContextLoaderListener</listener-class> </listener>?
/***?自定義Spring的ContextLoaderListener*?@author?楊尚川*/ public?class?APDPlatContextLoaderListener?extends?ContextLoaderListener?{????@Overridepublic?void?contextInitialized(ServletContextEvent?event)?{//接管系統的啟動SystemListener.contextInitialized(event);super.contextInitialized(event);}@Overridepublic?void?contextDestroyed(ServletContextEvent?event)?{//接管系統的關閉SystemListener.contextDestroyed(event);super.contextDestroyed(event);} }?
在Spring啟動和關閉之前,都會先調用org.apdplat.module.system.service.SystemListener來做預處理。
?
這只是接管啟動和關閉權,關于無縫集成所做的定制請看org.apdplat.platform.spring、org.apdplat.platform.struts、org.apdplat.platform.compass這三個包里面的類,這里不做說明。
?
本文主要分析SystemListener在系統啟動和關閉的時候都分別做了什么處理。
?
系統啟動流程:
?
1、獲取ContextPath
?
contextPath=sce.getServletContext().getContextPath();?
public?static?String?getContextPath()?{return?contextPath; }?
在部署APDPlat的時候,可能會有兩種情況:一是部署在ROOT目錄下,ContextPath為空,則地址為http://192.168.0.100;二是部署在非ROOT目錄下,假設ContextPath為APDPlat_Web,則地址為http://192.168.0.100/APDPlat_Web。前端EXT JS和JSP以及后臺服務在處理絕對路徑和記錄日志等情況的時候需要知道ContextPath的值,該值在系統啟動的時候從應用服務器中獲得,保存為靜態變量,并通過靜態方法暴露給系統使用。
?
2、獲取RealPath
?
basePath=sce.getServletContext().getRealPath("/"); FileUtils.setBasePath(basePath);?
整個APDPlat系統中的文件操作都以basePath為基礎,通過basePath的值,我們可以得知Web應用存放在服務器上面的本地磁盤絕對路徑,如:D:\Workspaces\NetBeansProjects\APDPlat2.5\APDPlat_Web\target\APDPlat_Web-2.5\,這樣我們就可以對Web應用中的所有文件進行IO操作。
?
3、改變系統屬性user.dir的值
?
userDir=FileUtils.getAbsolutePath("/WEB-INF/classes/data/"); System.setProperty("user.dir",?userDir);?
把user.dir重新指定到Web應用的/WEB-INF/classes/data/目錄,此目錄會存放索引文件,初始導入的數據文件。
?
4、為spring的配置做預處理
?
public?static?void?prepareForSpring(){//供spring掃描組件用String?basePackage=PropertyHolder.getProperty("basePackages");String?localBasePackage=PropertyHolder.getProperty("basePackages.local");if(StringUtils.isNotBlank(localBasePackage)){basePackage=basePackage+","+localBasePackage;}System.setProperty("basePackage",?basePackage);???????? }?
這里為用戶在項目中自定義掃描組件的范圍提供支持,用戶可在config.local.properties配置文件中定義變量basePackages.local的值為自己的包名稱。
?
5、注冊模塊
?
Enumeration<URL>?ps?=?Thread.currentThread().getContextClassLoader().getResources("META-INF/services/module.xml");?
根據模塊描述文件module.xml識別類路徑下的所有模塊,注冊模塊,提取web資源和數據,為后續的組件掃描、模塊初始化、數據庫同步做準備。
?
6、解析所有的dic.xml文件,并生成供客戶端EXT JS調用的文件
?
DictionaryGenerator.generateDic(basePath);?
7、記錄服務器啟動日志(如啟用)
?
//保存服務器啟動日志 BufferLogCollector.collect(runingTime);?
8、啟動內存監視線程(如啟用)
?
int?circle=PropertyHolder.getIntProperty("monitor.memory.circle"); memoryMonitorThread=new?MemoryMonitorThread(circle); memoryMonitorThread.start();?
?
?
系統關閉流程:
?
1、記錄用戶注銷日志(如啟用)
?
UserLoginListener.forceAllUserOffline();?
2、記錄服務器關閉日志(如啟用)
?
//保存服務器關閉日志 BufferLogCollector.collect(runingTime);?
3、停止內存監視線程(如啟用)
?
memoryMonitorThread.running=false; memoryMonitorThread.interrupt();?
4、處理緩沖區中的日志
?
//在關閉系統之前,處理緩沖區中的日志 BufferLogCollector.close();?
5、卸載JDBC驅動
?
????private?static?void?deregisterDrivers()?{Enumeration<Driver>?drivers=DriverManager.getDrivers();while(drivers.hasMoreElements()){Driver?driver=drivers.nextElement();try?{DriverManager.deregisterDriver(driver);}?catch?(SQLException?e)?{LOG.warn("卸載JDBC驅動失敗:"+driver,?e);LOG.warn("Fail?to?uninstall?JDBC?driver:"+driver,?e,?Locale.ENGLISH);}}}?
APDPlat托管在Github
轉載于:https://my.oschina.net/apdplat/blog/197067
總結
以上是生活随笔為你收集整理的APDPlat的系统启动和关闭流程剖析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【语法】NSString
- 下一篇: jquery键盘事件全记录