Configure web. xml
<! -- Used to load variables --> <listener-class> com. cs2c. Bus. listener. loadsysvariables </listener-class> </listener>
The listenerCode:
Package COM. cs2c. cobra. common. listener; import Java. io. ioexception; import Java. util. properties; import javax. servlet. servletcontextevent; import javax. servlet. servletcontextlistener;/***** @ author wangz ** is used to initialize system variables, such as various URLs. The jar package is retrieved from sys, therefore, set ** 2012-05-30 */public class loadsysvariables implements servletcontextlistener {Private Static final string sys_config_file_name = "/config. properties "; Private Static final string cobra_server_url_name =" Cobra. server. URL "; Private Static final string cobra_esbserver_url_name =" sys_cobra_esbserver_url "; Private Static final string gluster_esbserver_url_name =" gluster. server. URL "; @ overridepublic void contextdestroyed (servletcontextevent arg0) {}@ overridepublic void contextinitialized (servletcontextevent arg0) {properties props = new properties (); try {props. load (this. getclass (). getresourceasstream (sys_config_file_name);} catch (ioexception e) {e. printstacktrace ();} system. setproperty (cobra_esbserver_url_name, props. getproperty (cobra_esbserver_url_name ). tostring (); system. setproperty (gluster_esbserver_url_name, props. getproperty (gluster_esbserver_url_name ). tostring (); system. setproperty ("sys_cobra_conf_server_url", props. getproperty ("sys_cobra_conf_server_url "). tostring ());}}
The config. properties file can be stored in the root directory of the class.
The client reads the configuration from the system. If the configuration is read, The read value is used. If the configuration cannot be read, the default value is used. For example:
Import java.net. uri; import javax. WS. rs. core. uribuilder; import COM. cs2c. cobra. core. constants. cobrarestconstants; import COM. cs2c. restful. jersey. client. abstracthttpclient; import COM. cs2c. restful. jersey. utils. clientconstants;/*** ESB client for monitoring data * @ author WZ * 2012-05-24 */public abstract class abstractcobraesbclient extends acthttpclient {public URI getserverbaseuri () {return uribuilder. fromuri (getbaseurl ()). build ();} private string getbaseurl () {// remove the platform path (e.g. /Linux. GTK. x86_64) from the urlreturn system. getproperty (cobrarestconstants. resource_base_url_name, "http: // 10.1.83.29: 8083 ");}}