Solution for frequent restart of Application Server due to increasing client memory when using axis2 to build WebService

Source: Internet
Author: User

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 ");

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.