I would like to create a Spring Boot application and let it run exactly 24 hours. After that the application should exit nicely. I could only think of achieving this goal with an infinate loop and always check current time equals start time + 24 hours. Can anybody please advise?
Please note this is NOT to schedule some tasks...
You can schedule a task to execute once: (Spring scheduling task - run only once) and close application when task is executed (Programmatically shut down Spring Boot application)
If you do this on Linux, you can run your Spring Boot Application, and get the process ID.
java -jar your-app.jar
and use another base script to kill it after 24 hours, like :
sleep 24 * 60 * 60
kill -9 <pid_of_your_app>
Related
I need to create 20 instances of a Spring Boot Application. Is there anyway to automate it so that the below command can be run multiple times and increment the port number each time:
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8001
Furthermore once the instances are all started up, is there a way to send an identical POST request with a JSON body to each instance at an endpoint on the Spring Boot Application /someEndpoint.
Try writing a script (.sh file ) in which run a loop for desired time for the command. Use --server.port=0 which will randomly select the port.
I am running Spring boot application with 2 instances.Here i am going to use scheduler to run my application.For avoiding Scheduler not to run in two instances at same time using schlock .but schlock i have to mention for atleastfor or atmostfor .My problem is i dont want to release the lock based on time because since using batch application with rest call dont know when my scheduler get complete process.Kindly provide any suggestion running my scheduler with one instance at time without time constraint.
ShedLock requires lockAtMostFor for cases when the node dies. You can try KeepAliveLockProvider to automatically extend the lock.
I have several scheduled task in spring application like below:
#Scheduled(cron="0 30 11 4 * ?")
#Scheduled(cron="0 30 11 * * *")
The first one started normally but the second one never start. Is
spring scheduled task allow multiple concurrent task running.
Or task wont start event scheduled time is up if another task is
running?
Thanks,
I do some google and just found the answer.
Default thread pool for scheduler is only ONE thread, so one task is running and the other must wait for available thread.
The solution is increasing thread pool size for scheduler. I follow this link which use java configuration to increase thread pool size. Spring document has example for xml configuration.
Hope this helps.
Approximately for standalone start as java process :
java -jar myspring_boot.jar
it takes around 20 seconds. But if I run it in a docker container which contain more micro services it takes around 3 minutes.
Is there a way to speed up the spring-boot boot time as for an example
if I enable debug longing I notice that there are a lot of unnecessary validations for different spring configurations.
How I can speed up the spring-boot startup time only for dev purpose using Docker containers?
I installed haveged daemon as it says in many answers out there, for example:
https://stackoverflow.com/a/39461346/2748325
And also added -XX:MaxMetaspaceSize=128m to my java CMD in the Dockerfile and the times went down in about 2 minutes.
I have been checking all over to find out how to configure a schedule for Spring Batch. I am using Spring boot with a web ui. The user can go and select a time to run a job and this must be persistence to a database and run when the time comes. A different user can schedule the same job for a different time. Does spring batch have anything similar to this? If not then what is the best way to go about this? need some examples.
There are no out of the box solution which just work. You can do this with minimal coding. Try to persist the schedule in database and on start up your application you can read all the schedule and schedule using batch scheduler. Also you can do the same thing when user enter new schedule with out restart.
Cron based scheduler is already available in spring. Just need to persist and schedule from your side.
scheduler.schedule(task, new CronTrigger("* 15 9-17 * * MON-FRI"));
documentation on scheduler is given here