I need to gracefully start and shutdown the spring boot application using a script . Is there any other way to do it except using an actuator module in the application. Currently i have to stop the spring boot process manually in the task manager.
Ultimately the spring boot application spins off a java process which needs to be killed. At present you are killing it manually.
You have a few options:
You can use SpringApplication.exit(ApplicationContext, ExitCodeGenerator...) method.
If your application is not a long running application then do you have some exit point where your application should stop. At that point you can System.exit(0)
You can use external manager tools, for example on unix you can use supervisor, here is a blog you can read about this.
SpringApplication.run gives you ApplicationContext which you can close.
Related
Related to this article:
https://www.baeldung.com/spring-cloud-task
and this example:
https://github.com/spring-cloud/spring-cloud-task/blob/master/spring-cloud-task-samples/timestamp
How does the Spring Boot app exit after it runs the task? Where is the code/configuration to tell the Spring Boot application that once the task is finished it should shut down gracefully?
I'm looking at the Timestamp example:
https://github.com/spring-cloud/spring-cloud-task/blob/master/spring-cloud-task-samples/timestamp/src/main/java/org/springframework/cloud/task/timestamp/TaskApplication.java
which runs the task, prints the timestamp, and then shuts down, but I'm not understanding how the application (Spring Boot) shuts down after the task completes?
A JVM will automatically shut down when there are no daemon threads running. So with Spring Cloud Task (and any Java application), once your processing is complete, unless you have another non-daemon thread running, the JVM will automatically terminate.
I have Spring boot mongo db app, when i start running this application if mongo host is alive, am able to see the my app is up and running successfully,
if my mongo host is down when i start my application my app failed to start.
is there any way even thought if mongo host is down my application should be up and running.
could someone please help me on this?
am using spring boot mongo properties in my application
spring.data.mongodb.repositories.enabled=true
spring.data.mongodb.port=27017
spring.data.mongodb.database=db
spring.data.mongodb.uri=mongodb://mongo-node-1.ballu/db
have same problem with spring boot kafka also.
Sorry for the previous comment. It was for excluding auto config beans, anyway
Is there any way even though if mongo host is down my application
should be up and running.
Yes,
spring.datasource.continue-on-error=true #Whether to stop if an error occurs while initializing the database.
as per spring doc
By default, Spring Boot enables the fail-fast feature of the Spring
JDBC initializer. This means that, if the scripts cause exceptions,
the application fails to start. You can tune that behavior by setting
spring.datasource.continue-on-error.
and as of spring kafka try this(i'm not sure if this meets your requirement)
spring.kafka.admin.fail-fast=true # Whether to fail fast if the broker is not available on startup.
How to create automatic shutdown in spring integration after finish all process for all files?
My application download undefined number of files and process all files sequentially, the flow is like this:
file:inbound-channel-adapter --> Transformer --> Splitter -->
http-outbound-gateway --> int:aggregator --> mail:outbound-channel-adapter.
At the End, i should shutdown my app.
How to know that all files are processed?
Anyone has an experience about it ?
Thanks
In you case you need to track your inbound directory.
You have to add another step to your flow (probably to track the last file).
I would suggest an ETL technique: Read file descriptions then process them.
When it will be processed you can send event to shutdown the app.
Spring boot application can be stopped by using ConfigurableApplicationContext:
SpringApplication app = new SpringApplication(Application.class);
ConfigurableApplicationContext context = app.run(args);
context.close();
Also there is another way using Spring Actuator shutdown endpoint:
See How to shutdown a Spring Boot Application in a correct way?
Spring Boot documentation
I'd like to know how to implement stop() method as Apache Commons Daemon provides on Spring Boot.
I read the related documentation from https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html, and it seems that the Spring Boot 1.3.x or later version supports being started as a service/daemon using systemctl command on Linux (especially CentOS, which is my current environment for testing).
A Spring Boot Application seems being able to be stated/stopped with a script and the systemctl command, but there is no description about the my question. The reason I need the function is because Apache Kafka is used on the application so that it should do close() when it stopped.
Thanks in advance!
I wrote a Spring Boot webservice that uses an embedded tomcat as container.
In case the system reboots I want to backup some information to a mysql database.
In my webservice I use #Scheduled() and #PreDestroy to run the backup.
This goes well when I stop the server with ^C.
But when I kill the process with an sysV skript (/etc/init.d) and the kill command - even though the daemon has a dependency on mysql, the mysql server is shut down before the backup is finished (resulting in SQL Exceptions in my log).
The reason for this is of course, that kill only sends a signal to stop the process.
How can I (from my sysv skript) synchroneously stop the running spring boot tomcat server?
If you include spring-boot-starter-actuator then that provides a REST endpoint for management. One of the endpoints provided is /shutdown. By hitting that endpoint, you will get a controlled shutdown of all resources, which ensures that #PreDestroy will be called. As this could be dangerous to have enabled by default, to use it you will need to add the following to your application.properties file:
endpoints.shutdown.enabled=true
Of course, once you have exposed that endpoint you need to ensure that there's a teeny bit of security applied to prevent just anybody shutting down your server.
On a related note, you may find my answer to Spring Boot application as a Service useful, where I provided the code for a full init.d script which makes use of this.
As an alternative to the "/shutdown" endpoint the Actuator also has an ApplicationPidListener (not enabled by default) that you can use to create a pidfile (which is commonly used in "init.d" style scripts to kill a process when you want to stop it). The JVM should respond to a kill (sigint) and Spring will shutdown gracefully.