0

How to implement a clean up method after request finish process in a spring boot application for clean data such as thread locals

I try with ServletRequestListener.requestDestroyed api, but it does not get hit after the request finish

Harshana
  • 7,297
  • 25
  • 99
  • 173

1 Answers1

1

One possible answer is that you have neglected to register the listener. The Servlet javadocs say:

In order to receive these notification events, the implementation class must be either declared in the deployment descriptor of the web application, annotated with WebListener, or registered via one of the addListener methods defined on ServletContext.

There may be other ways to implement this using Spring; e.g. using a handler intercepter; see Remove ThreadLocal object within a Spring MVC website?

If you were using plain servlets (without the Spring MVC infrastructure), another approach would be to do the cleanup in your servlet's service method or the doXxx methods. Or in a Filter in front of the servlet.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • you are correct. i had to register it as a bean.. What is the best approach ServletRequestListener or HandlerInterceptorAdapter? I think ServletRequestListener is simple. Also i found that all api's in HandlerInterceptorAdapter do the work after request finish in the controller know. – Harshana Jan 28 '19 at 02:23
  • I don't have an opinion on that. – Stephen C Jan 28 '19 at 02:56