加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用axis2构建webservice时客户端内存不断增长导致应用服务器频

发布时间:2020-12-16 23:15:39 所属栏目:安全 来源:网络整理
导读:? ?使用axis2构建webservice时,发现系统运行一段时间后客户端的内存使用一直不断增高,导致应用服务器无法承受而重启,这里提供了一种解决方案。 ? ?????? 原因分析:客户端以stub方式请求服务,stub对象是一个非常重量级的对象,每次客户端向服务端请求服

? ?使用axis2构建webservice时,发现系统运行一段时间后客户端的内存使用一直不断增高,导致应用服务器无法承受而重启,这里提供了一种解决方案。

?

?????? 原因分析:客户端以stub方式请求服务,stub对象是一个非常重量级的对象,每次客户端向服务端请求服务时,stub对象不断被创建,占用大量系统资源

?????? 解决方案:考虑到所有的客户端stub都继承自org.apache.axis2.client.Stub类,考虑使用简单工厂模式+简单的对象池来提供stub对象实例,这样只需首次需要stub对象时实例化一次,以后需要用的时候从stub对象池中取得,工厂类本身做成单例模式。

?

代码实现:

?

???????

[java] view plain copy print ?
  1. public?class?StubFactory{??
  2. ???//从配置文件中取客户端应用context的绝对路径 ??
  3. ???public?static?final?String?path=ServiceConfig.getPath();??
  4. ???//从配置文件中取服务url ??
  5. ???public?static?final?String?serviceUrl=ServiceConfig.getServiceUrl();??
  6. ?????
  7. ???private?static?Map?map=new?HashMap();//这里可以是一个同步的Map,也可以直接用Hashtable ??
  8. ???private?static?ConfigurationContext?configurationContext=null;??
  9. ???private?static?StubFactory?stubFactory=new?StubFactory();??
  10. ?????
  11. ???private?StubFactory(){};??
  12. ?????
  13. ???static{??
  14. ??????try{??
  15. ????????configurationContext=ConfigurationContextFactory.createConfigurationContextFromFileSystem(path+"/WEB-INF",path+"/WEB-INF/conf/axis2.xml");??
  16. ??????}catch(AxisFault?e){??
  17. ?????????e.printStackTrace();??
  18. ??????}??
  19. ???}??
  20. ??
  21. ???public?Stub?getStub(String?type){??
  22. ?????Stub?stub=null;??
  23. ?????if(map.get(type)==null){??
  24. ???????stub=(Stub)this.createStub(type);??
  25. ???????map.put(type,stub);??
  26. ?????}??
  27. ?????return?(Stub)map.get(type);??
  28. ???}??
  29. ?????
  30. ???private?Stub?createStub(String?type){??
  31. ?????Stub?stub=null;??
  32. ?????String?targetEndpoint=null;??
  33. ?????try{??
  34. ???????if("SearchServiceStub".equals(type)){??
  35. targetEndpoint=serviceUrl+"services/SearchService.SearchServiceHttpSoap12Endpoint/";??
  36. ????????stub=new?SearchServiceStub(configurationContext,targetEndpoint);??
  37. ???????}??
  38. ????????if("CalculateServiceStub".equals(type)){??
  39. targetEndpoint=serviceUrl+"services/CalculateService.CalculateServiceHttpSoap12Endpoint/";??
  40. ????????stub=new?CalculateServiceStub(configurationContext,targetEndpoint);??
  41. ???????}??
  42. ????????if("MessageServiceStub".equals(type)){??
  43. targetEndpoint=serviceUrl+"services/MessageService.MessageServiceHttpSoap12Endpoint/";??
  44. ????????stub=new?MessageServiceStub(configurationContext,targetEndpoint);??
  45. ???????}??
  46. ?????}catch(Exception?e){??
  47. ????????e.printStackTrace();??
  48. ?????}??
  49. ?????return?stub;??
  50. ???}??
  51. ?????
  52. ???public?static?StubFactory?getInstance(){??
  53. ?????return?stubFactory;??
  54. ???}??
  55. }??

??????

取stub对象只要如下代码:

??????

[java] view plain copy print ?
  1. SearchServiceStub?stub=(SearchServiceStub)StubFactory.getInstance().getStub("SearchServiceStub");??

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读