Spring boot app gracefully catch the SIGTERM signal and invoke the predestroy method
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a spring boot application which needs to clear or clean up the resources when we kill the process using kill pid. @predestroy annotated method is not working and not getting called. Please suggest how can we catch the SIGTERM and invoke the predestroy method
spring spring-boot
add a comment |
I have a spring boot application which needs to clear or clean up the resources when we kill the process using kill pid. @predestroy annotated method is not working and not getting called. Please suggest how can we catch the SIGTERM and invoke the predestroy method
spring spring-boot
add a comment |
I have a spring boot application which needs to clear or clean up the resources when we kill the process using kill pid. @predestroy annotated method is not working and not getting called. Please suggest how can we catch the SIGTERM and invoke the predestroy method
spring spring-boot
I have a spring boot application which needs to clear or clean up the resources when we kill the process using kill pid. @predestroy annotated method is not working and not getting called. Please suggest how can we catch the SIGTERM and invoke the predestroy method
spring spring-boot
spring spring-boot
edited Jan 4 at 3:51
Michael Petch
27.3k557106
27.3k557106
asked Jan 4 at 3:01
DeveloperDeveloper
317
317
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Ideally, it should work but make sure SIGTERM
is called and not SIGKILL
I have a few cases where we run spring boot application
Docker
When you execute docker stop
what happens behind the scene is
Stop one or more running containers The main process inside the
container will receive SIGTERM, and after a grace period, SIGKILL
kill
kill 9955
=> SIGTERM is called
kill -9 9955
=> SIGKILL is called
Refer here for more detail on kill.
Now coming back to @PreDestroy
I have added following line in my SpringBoot Application
@PreDestroy
public void tearDown() {
System.out.println("Shutting Down...............the ");
}
I get following output when I do kill portno
2019-01-04 10:52:44.776 INFO o.s.s.c.ThreadPoolTaskScheduler / shutdown - 208 : Shutting down ExecutorService 'taskScheduler'
2019-01-04 10:52:44.783 INFO o.s.s.c.ThreadPoolTaskExecutor / shutdown - 208 : Shutting down ExecutorService 'applicationTaskExecutor'
2019-01-04 10:52:44.785 INFO o.s.o.j.LocalContainerEntityManagerFactoryBean / destroy - 597 : Closing JPA EntityManagerFactory for persistence unit 'default'
2019-01-04 10:52:44.792 INFO c.z.h.HikariDataSource / close - 350 : HikariPool-1 - Shutdown initiated...
2019-01-04 10:52:44.800 INFO c.z.h.HikariDataSource / close - 352 : HikariPool-1 - Shutdown completed.
Shutting Down...............
As you can see in Log I have 2 Scheduler Task running and JPA Entity Manager.
On receiving SIGTERM
it closes all that first and finally call preDestory.
I am not sure what more clean up you need to do but do make sure if its already cleaned.
Shut down embedded servlet container gracefully
You can also customize and write your own shutdown gracefully hook.
Refer to this good discussion and sample code on this
Restructure embedded web server packages
Pls be mindful of the sample code from above link. If you are using newer version of Springboot changes are package might be missing.
Refer this for more detail on changed methods and classes
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54032609%2fspring-boot-app-gracefully-catch-the-sigterm-signal-and-invoke-the-predestroy-me%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Ideally, it should work but make sure SIGTERM
is called and not SIGKILL
I have a few cases where we run spring boot application
Docker
When you execute docker stop
what happens behind the scene is
Stop one or more running containers The main process inside the
container will receive SIGTERM, and after a grace period, SIGKILL
kill
kill 9955
=> SIGTERM is called
kill -9 9955
=> SIGKILL is called
Refer here for more detail on kill.
Now coming back to @PreDestroy
I have added following line in my SpringBoot Application
@PreDestroy
public void tearDown() {
System.out.println("Shutting Down...............the ");
}
I get following output when I do kill portno
2019-01-04 10:52:44.776 INFO o.s.s.c.ThreadPoolTaskScheduler / shutdown - 208 : Shutting down ExecutorService 'taskScheduler'
2019-01-04 10:52:44.783 INFO o.s.s.c.ThreadPoolTaskExecutor / shutdown - 208 : Shutting down ExecutorService 'applicationTaskExecutor'
2019-01-04 10:52:44.785 INFO o.s.o.j.LocalContainerEntityManagerFactoryBean / destroy - 597 : Closing JPA EntityManagerFactory for persistence unit 'default'
2019-01-04 10:52:44.792 INFO c.z.h.HikariDataSource / close - 350 : HikariPool-1 - Shutdown initiated...
2019-01-04 10:52:44.800 INFO c.z.h.HikariDataSource / close - 352 : HikariPool-1 - Shutdown completed.
Shutting Down...............
As you can see in Log I have 2 Scheduler Task running and JPA Entity Manager.
On receiving SIGTERM
it closes all that first and finally call preDestory.
I am not sure what more clean up you need to do but do make sure if its already cleaned.
Shut down embedded servlet container gracefully
You can also customize and write your own shutdown gracefully hook.
Refer to this good discussion and sample code on this
Restructure embedded web server packages
Pls be mindful of the sample code from above link. If you are using newer version of Springboot changes are package might be missing.
Refer this for more detail on changed methods and classes
add a comment |
Ideally, it should work but make sure SIGTERM
is called and not SIGKILL
I have a few cases where we run spring boot application
Docker
When you execute docker stop
what happens behind the scene is
Stop one or more running containers The main process inside the
container will receive SIGTERM, and after a grace period, SIGKILL
kill
kill 9955
=> SIGTERM is called
kill -9 9955
=> SIGKILL is called
Refer here for more detail on kill.
Now coming back to @PreDestroy
I have added following line in my SpringBoot Application
@PreDestroy
public void tearDown() {
System.out.println("Shutting Down...............the ");
}
I get following output when I do kill portno
2019-01-04 10:52:44.776 INFO o.s.s.c.ThreadPoolTaskScheduler / shutdown - 208 : Shutting down ExecutorService 'taskScheduler'
2019-01-04 10:52:44.783 INFO o.s.s.c.ThreadPoolTaskExecutor / shutdown - 208 : Shutting down ExecutorService 'applicationTaskExecutor'
2019-01-04 10:52:44.785 INFO o.s.o.j.LocalContainerEntityManagerFactoryBean / destroy - 597 : Closing JPA EntityManagerFactory for persistence unit 'default'
2019-01-04 10:52:44.792 INFO c.z.h.HikariDataSource / close - 350 : HikariPool-1 - Shutdown initiated...
2019-01-04 10:52:44.800 INFO c.z.h.HikariDataSource / close - 352 : HikariPool-1 - Shutdown completed.
Shutting Down...............
As you can see in Log I have 2 Scheduler Task running and JPA Entity Manager.
On receiving SIGTERM
it closes all that first and finally call preDestory.
I am not sure what more clean up you need to do but do make sure if its already cleaned.
Shut down embedded servlet container gracefully
You can also customize and write your own shutdown gracefully hook.
Refer to this good discussion and sample code on this
Restructure embedded web server packages
Pls be mindful of the sample code from above link. If you are using newer version of Springboot changes are package might be missing.
Refer this for more detail on changed methods and classes
add a comment |
Ideally, it should work but make sure SIGTERM
is called and not SIGKILL
I have a few cases where we run spring boot application
Docker
When you execute docker stop
what happens behind the scene is
Stop one or more running containers The main process inside the
container will receive SIGTERM, and after a grace period, SIGKILL
kill
kill 9955
=> SIGTERM is called
kill -9 9955
=> SIGKILL is called
Refer here for more detail on kill.
Now coming back to @PreDestroy
I have added following line in my SpringBoot Application
@PreDestroy
public void tearDown() {
System.out.println("Shutting Down...............the ");
}
I get following output when I do kill portno
2019-01-04 10:52:44.776 INFO o.s.s.c.ThreadPoolTaskScheduler / shutdown - 208 : Shutting down ExecutorService 'taskScheduler'
2019-01-04 10:52:44.783 INFO o.s.s.c.ThreadPoolTaskExecutor / shutdown - 208 : Shutting down ExecutorService 'applicationTaskExecutor'
2019-01-04 10:52:44.785 INFO o.s.o.j.LocalContainerEntityManagerFactoryBean / destroy - 597 : Closing JPA EntityManagerFactory for persistence unit 'default'
2019-01-04 10:52:44.792 INFO c.z.h.HikariDataSource / close - 350 : HikariPool-1 - Shutdown initiated...
2019-01-04 10:52:44.800 INFO c.z.h.HikariDataSource / close - 352 : HikariPool-1 - Shutdown completed.
Shutting Down...............
As you can see in Log I have 2 Scheduler Task running and JPA Entity Manager.
On receiving SIGTERM
it closes all that first and finally call preDestory.
I am not sure what more clean up you need to do but do make sure if its already cleaned.
Shut down embedded servlet container gracefully
You can also customize and write your own shutdown gracefully hook.
Refer to this good discussion and sample code on this
Restructure embedded web server packages
Pls be mindful of the sample code from above link. If you are using newer version of Springboot changes are package might be missing.
Refer this for more detail on changed methods and classes
Ideally, it should work but make sure SIGTERM
is called and not SIGKILL
I have a few cases where we run spring boot application
Docker
When you execute docker stop
what happens behind the scene is
Stop one or more running containers The main process inside the
container will receive SIGTERM, and after a grace period, SIGKILL
kill
kill 9955
=> SIGTERM is called
kill -9 9955
=> SIGKILL is called
Refer here for more detail on kill.
Now coming back to @PreDestroy
I have added following line in my SpringBoot Application
@PreDestroy
public void tearDown() {
System.out.println("Shutting Down...............the ");
}
I get following output when I do kill portno
2019-01-04 10:52:44.776 INFO o.s.s.c.ThreadPoolTaskScheduler / shutdown - 208 : Shutting down ExecutorService 'taskScheduler'
2019-01-04 10:52:44.783 INFO o.s.s.c.ThreadPoolTaskExecutor / shutdown - 208 : Shutting down ExecutorService 'applicationTaskExecutor'
2019-01-04 10:52:44.785 INFO o.s.o.j.LocalContainerEntityManagerFactoryBean / destroy - 597 : Closing JPA EntityManagerFactory for persistence unit 'default'
2019-01-04 10:52:44.792 INFO c.z.h.HikariDataSource / close - 350 : HikariPool-1 - Shutdown initiated...
2019-01-04 10:52:44.800 INFO c.z.h.HikariDataSource / close - 352 : HikariPool-1 - Shutdown completed.
Shutting Down...............
As you can see in Log I have 2 Scheduler Task running and JPA Entity Manager.
On receiving SIGTERM
it closes all that first and finally call preDestory.
I am not sure what more clean up you need to do but do make sure if its already cleaned.
Shut down embedded servlet container gracefully
You can also customize and write your own shutdown gracefully hook.
Refer to this good discussion and sample code on this
Restructure embedded web server packages
Pls be mindful of the sample code from above link. If you are using newer version of Springboot changes are package might be missing.
Refer this for more detail on changed methods and classes
answered Jan 4 at 5:38
MyTwoCentsMyTwoCents
3,4222930
3,4222930
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54032609%2fspring-boot-app-gracefully-catch-the-sigterm-signal-and-invoke-the-predestroy-me%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown