Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Application Shutdown Event in Spring #3871

Open
hamidonos opened this issue Feb 13, 2025 · 0 comments
Open

Handle Application Shutdown Event in Spring #3871

hamidonos opened this issue Feb 13, 2025 · 0 comments

Comments

@hamidonos
Copy link
Collaborator

hamidonos commented Feb 13, 2025

Situtaiton

Currently we use @PreDestroyto handle application shutdowns which is not optimal because @PreDestroy can also be called when the bean is destroyed manually.

Solution

Use ApplicationListener<ContextClosedEvent> for such cases.

@Component
public class AppShutdownListener implements ApplicationListener<ContextClosedEvent> {

    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        System.out.println("Application is shutting down. Cleaning up resources...");
    }
}

Use the existing ShutdownSupport for this.

Change predestroy() to onShutdown()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant