0

I am hosting an application in Tomcat. I have some cleanup logic that needs to run before application shuts down, which I have implemented using spring @Predestroy methods. After I send SIGINT to the process, predestroyer logic starts running, which is exactly what I want. But as part of this predestroy logic, there are a few API calls and other stuff that needs 8080 to be still open. As soon as I send kill signal, the ports are closed, but the process still runs. Is there a way I can still keep the ports open until all beans are destroyed? Can someone please help?

( I have used graceful shutdown tag with a sufficient timeout period, it does not work for me even though the prerequisites are met - https://dzone.com/articles/configuring-graceful-shutdown-readiness-and-livene My spring boot version is 2.6 and tomcat is 9.0.60 )

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
AP_22
  • 53
  • 5
  • Are you running an embedded tomcat or deploying in Tomcat? Very different. And also why would you need to call yourself over HTTP that seems overly complicated, why not just call the beans/methods directly from the `@PreDestroy` method(s)? – M. Deinum Mar 21 '22 at 12:24
  • I will be deploying in Tomcat & I'm also trying to test the behaviour in local by sending a SIGINT. he calls are not http calls exactly. Basically it's a calling application (where users can call each other). The use case is whenever I am shutting down the application, I need to keep the call alive for sometime(there's some logic) and call should not get disconnected. The "keeping call alive" part is taken care of in @Predestroy. But for the calls to be alive, I need 8080 as it requires a couple of self pings and multiple calls to/from other services even when the predestoy method is running. – AP_22 Mar 21 '22 at 14:14
  • An external tomcat is something you cannot control, so you are at the mercy how things are configured. Those `server.*` properties don't apply when using an external tomcat. – M. Deinum Mar 21 '22 at 14:15
  • Just to clarify, the moment it receives SIGINT, the port is closed and there's no way I can keep them open for a little longer? – AP_22 Mar 21 '22 at 14:17
  • As stated if you are using an external tomcat there isn't. Graceful shutdown only works for an embedded tomcat instance (although regular tomcat shuts down grafecull it only does so for a short period). You could delay the unload but that requires configuration (see https://stackoverflow.com/questions/51675180/how-to-gracefully-shutdown-servelts-in-tomcat-in-docker-container#:~:text=Yes%2C%20tomcat%20itself%20shuts%20down%20gracefully%2C%20but%20not,in%20the%20middle%20of%20processing%20a%20reguest%20%28%21%21%29) – M. Deinum Mar 21 '22 at 14:21
  • For embedded tomcat, do you know any alternative to https://dzone.com/articles/configuring-graceful-shutdown-readiness-and-livene ? I'm trying in local and this doesn’t seem to be working for me – AP_22 Mar 21 '22 at 14:24
  • What do you mean with local? If you are deploying to a local tomcat that isn't an embedded tomcat. Embedded means running `java -jar your.jar` or running it from within your IDE. However as you aren't allowed anymore incoming connections those self pings will obviously not work as no incoming requests are allowed. – M. Deinum Mar 21 '22 at 14:26
  • i'm running it within my IDE, using IntelliJ – AP_22 Mar 21 '22 at 14:28
  • 1
    The graceful shutdown isn't what you think it is. There is a graceful shutdown but you cannot make new connections. You require new connections in your shutdown logic and that is where your flaw is, you cannot self ping or do anytning with connections as those aren't allowed. Regardless of using Graceful shutdown or not. – M. Deinum Mar 21 '22 at 14:30
  • Thank you. Appreciate the clarification! :) – AP_22 Mar 21 '22 at 14:35

0 Answers0