Spring MVC Web服务的Controller的单例模式

2017/10/22 by Jinyang | Filed under 技术相关.

某服务在线上总是只能稳定几天    有时一需要需要重启几次,每次失去响应时  必须要重启才能解决。

同容器下的其他服务正常,此前看过日志   发现服务挂掉前有内存溢出的情况。

但是内存降下来后   其他服务都恢复了,就它没能恢复,总是要手工处理,很不好。

我猜测是因为其他代码比较老,直接是JSP或Servlet等形式,每次请求都生成对象,

看了下SpringMVC的控制器的单例模式,默认其他就是单例模式( Singleton )  为的是减少内存申请的开销和占用,本质是个好的出发点,

在控制器声明出增加注解将其修改为Session模式,因为是web项目,我想Session模式应该是web项目最适合的场景了。

代码就一行:  @Scope(value = “session”) //session模式

分别测试了模式的单例模式, 增加了打印Controller的Object的ID的代码如下 :

_log.info(“loading form page”);
_log.info(“session is :”+req.getSession().getId());
_log.info(“object is :”+ java.lang.System.identityHashCode(this));

 

 

测试得到对象的identityHashCode符合预期:

2017-10-21 20:00:34,191 DEBUG – DispatcherServlet with name ‘springMVC’ processing GET request for [/********/******/****.do]
2017-10-21 20:00:34,192 DEBUG – Looking up handler method for path /******/****.do
2017-10-21 20:00:34,196 DEBUG – Returning handler method [public java.lang.String com.******.controller.******Controller.****(javax.servlet.http.HttpServletRequest)]
2017-10-21 20:00:34,197 DEBUG – Last-Modified value for [/********/******/****.do] is: -1
2017-10-21 20:00:34,198 INFO – loading **** page
2017-10-21 20:00:34,198 INFO – session is :53AAF10B6C89CB00D7920C82756D7289
2017-10-21 20:00:34,198 INFO – object is :1623900576
2017-10-21 20:00:34,199 DEBUG – Rendering view [org.springframework.web.servlet.view.JstlView: name ‘/WEB-INF/view/******/****'; URL [//WEB-INF/view/******/****.jsp]] in DispatcherServlet with name ‘springMVC’
2017-10-21 20:00:34,199 DEBUG – Forwarding to resource [//WEB-INF/view/******/****.jsp] in InternalResourceView ‘/WEB-INF/view/******/****’
2017-10-21 20:00:34,201 DEBUG – Successfully completed request
2017-10-21 20:00:39,808 DEBUG – DispatcherServlet with name ‘springMVC’ processing GET request for [/********/******/****.do]
2017-10-21 20:00:39,808 DEBUG – Looking up handler method for path /******/****.do
2017-10-21 20:00:39,811 DEBUG – Returning handler method [public java.lang.String com.******.controller.******Controller.****(javax.servlet.http.HttpServletRequest)]
2017-10-21 20:00:39,811 DEBUG – Last-Modified value for [/********/******/****.do] is: -1
2017-10-21 20:00:39,811 INFO – loading **** page
2017-10-21 20:00:39,811 INFO – session is :C856C0A003DDCD16253F274F22B774D9
2017-10-21 20:00:39,811 INFO – object is :791139331
2017-10-21 20:00:39,811 DEBUG – Rendering view [org.springframework.web.servlet.view.JstlView: name ‘/WEB-INF/view/******/****'; URL [//WEB-INF/view/******/****.jsp]] in DispatcherServlet with name ‘springMVC’
2017-10-21 20:00:39,811 DEBUG – Forwarding to resource [//WEB-INF/view/******/****.jsp] in InternalResourceView ‘/WEB-INF/view/******/****’
2017-10-21 20:00:39,811 DEBUG – Successfully completed request
2017-10-21 20:00:55,486 DEBUG – DispatcherServlet with name ‘springMVC’ processing GET request for [/********/******/****.do]
2017-10-21 20:00:55,486 DEBUG – Looking up handler method for path /******/****.do
2017-10-21 20:00:55,490 DEBUG – Returning handler method [public java.lang.String com.******.controller.******Controller.****(javax.servlet.http.HttpServletRequest)]
2017-10-21 20:00:55,490 DEBUG – Last-Modified value for [/********/******/****.do] is: -1
2017-10-21 20:00:55,490 INFO – loading **** page
2017-10-21 20:00:55,490 INFO – session is :CEE5CE124DFF45ED237EBA9B4690678B
2017-10-21 20:00:55,491 INFO – object is :217793580
2017-10-21 20:00:55,491 DEBUG – Rendering view [org.springframework.web.servlet.view.JstlView: name ‘/WEB-INF/view/******/****'; URL [//WEB-INF/view/******/****.jsp]] in DispatcherServlet with name ‘springMVC’
2017-10-21 20:00:55,492 DEBUG – Forwarding to resource [//WEB-INF/view/******/****.jsp] in InternalResourceView ‘/WEB-INF/view/******/****’
2017-10-21 20:00:55,493 DEBUG – Successfully completed request

 

 

上面是session模式的日志,也测试了单例模式下的,打印出来的对象的identityHashCode每次都一样的,也应证了此前问题可能存在的一个风险点。

因为并没有去分析Java内存,以上的处理方式需要生产系统的进一步验证,刚巧项目又要迁移服务器环境,可能后续不会再出现此问题了。


Leave a Reply

Your email address will not be published. Required fields are marked *

返回顶部