The project needs to read the Index Update address from the configuration file. Therefore, a method is provided to read and parse the file to initialize the address list.Javax. servlet. servletcontextParameter, used to obtain the file path, becauseJavax. servlet. servletcontextlistenerIs called inContextinitializedTheEvent. getservletcontext ()You can easily obtain this parameter. The code for this method is as follows:
/*** Initialize multi-server Index Update interface list * this method is called in updateindexlistener when the server is started **/public static void initserverslist (servletcontext event) {documentbuilderfactory DBF = documentbuilderfactory. newinstance (); try {documentbuilder DB = DBF. newdocumentbuilder (); document DOC = dB. parse (New fileinputstream (event. getrealpath ("WEB-INF/kbs-UpdateIndex-AllServers.xml"); // element root = Doc. getdocumentelement (); nodelist serve Rslist = Doc. getelementsbytagname ("server"); If (serverslist! = NULL) {for (INT I = 0; I <serverslist. getlength (); I ++) {string updateindexurl = Doc. getelementsbytagname ("Update-index-URL "). item (I ). getfirstchild (). getnodevalue (); serverlist. add (updateindexurl) ;}} catch (exception e) {log. error (E. getmessage ());}}
My configuration kbs-UpdateIndex-AllServers.xml files are placed under the WEB-INF:
<? XML version = "1.0" encoding = "UTF-8"?> <! -- ===================================================== ============================================= --> <! -- KBS server information configuration file --> <! -- @ Author linshutao --> <! -- Server: represents a server --> <! -- Update-index-URL: KBS Index Update interface --> <! -- ===================================================== ===================================== --> <KBS-servers> <server> <Update-index-URL> HTTP: // localhost: 8080/im800kbs/API/updateindex. DO </update-index-URL> </Server> </KBS-servers>