When using axis2 to build a WebService, it is found that the memory usage of the client continuously increases after the system runs for a period of time, resulting in the failure of the application server and the restart. A solution is provided here.
Cause analysis: the client requests the service as stub. The stub object is a very important object. Every time the client requests the service from the server, the stub object is constantly created, occupying a large amount of system resources.
Solution: considering that all client stub inherits from Org. apache. axis2.client. stub class, consider using the simple factory mode + Simple Object pool to provide stub object instances, So that you only need to instantiate the stub object for the first time, and get it from the stub Object pool for later use, the factory class itself is made into a singleton model.
Code implementation:
Public class stubfactory {<br/> // obtain the absolute path of the client application context from the configuration file <br/> Public static final string Path = serviceconfig. getpath (); <br/> // retrieve the service URL from the configuration file <br/> Public static final string serviceurl = serviceconfig. getserviceurl (); </P> <p> Private Static map = new hashmap (); // This can be a synchronized map, you can also directly use hashtable <br/> Private Static configurationcontext = NULL; <br/> Private Static stubfactory = new stubfactory (); </P> <p> private stubfactory () {}; </P> <p> static {<br/> try {<br/> configurationcontext = configurationcontextfactory. createconfigurationcontextfromfilesystem (path + "/WEB-INF", path + "/WEB-INF/CONF/axis2.xml"); <br/>} catch (axisfault e) {<br/> E. printstacktrace (); <br/>}</P> <p> Public stub getstub (string type) {<br/> stub = NULL; <br/> If (map. get (type) = NULL) {<br/> stub = (stub) This. createstub (type); <br/> map. put (type, Stub); <br/>}< br/> return (stub) map. get (type); <br/>}</P> <p> private stub createstub (string type) {<br/> stub = NULL; <br/> string targetendpoint = NULL; <br/> try {<br/> If ("searchservicestub ". equals (type) {<br/> targetendpoint = serviceurl + "Services/searchservice. searchservicehttpsoap12endpoint/"; <br/> stub = new searchservicestub (configurationcontext, targetendpoint); <br/>}< br/> If (" calculateservicestub ". equals (type) {<br/> targetendpoint = serviceurl + "Services/calculateservice. calculateservicehttpsoap12endpoint/"; <br/> stub = new calculateservicestub (configurationcontext, targetendpoint); <br/>}< br/> If (" messageservicestub ". equals (type) {<br/> targetendpoint = serviceurl + "Services/messageservice. messageservicehttpsoap12endpoint/"; <br/> stub = new messageservicestub (configurationcontext, targetendpoint); <br/>}< br/>} catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> return stub; <br/>}</P> <p> Public static stubfactory getinstance () {<br/> return stubfactory; <br/>}< br/>}
The following code is used to retrieve the stub object:
Searchservicestub stub = (searchservicestub) stubfactory. getinstance (). getstub ("searchservicestub ");