SpringBoot shedlock Without time Interval - spring-boot

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.

Related

There are three instances of the application on the server. but we want to execute the cronjob schedular on one instance of the application

There are three instances of the application on the server. but we want to execute the cronjob schedular logic on one instance of the application.
Using Spring-data, couchbase repository with couchbase database. is there any simple solution to my problem please suggest me. thanks in advance . i suffer from this problem from many days.
Facing similar situations with multiple spring boot instances, we schedule the cron externally, this can be a simple cron script that executes a curl, or a specific external scheduler app/instance. Your load balancer will pick an instance to run on.
You could also consider using quartz or shedlock with couchbase to manage without external trigger.

How to assess whether to use spring batch or scheduler in application?

I have a business logic already developed in spring boot that needs to be run once in every 60 days. I'm little confused on whether convert it to spring batch or use scheduler annotation.
What all factors should I consider to assess the same? Does either of them has any performance upper-hand over the other?
I'm new to the scheduler-batch concept and this is my first time work on the same.
How to assess whether to use spring batch or scheduler in application?
Spring Batch is not a scheduler, so it's not an either or question. You can use both, for example use a scheduler to schedule a Spring Batch job to run at a given time.
The question you should be asking is: is it worth transforming the business logic you already developed in spring boot in a Spring Batch job to benefit from what Spring Batch offers (the whole app could remain a boot app).
As a side note, since your job needs to be run every 60 days, using #Scheduled means you would have a JVM running for two months to run a job. Unless you are planning to use the same JVM for other things in the meantime, this would be an inefficient use of resources. Other scheduling mechanisms like cron is more appropriate in this case.

Spring task:scheduled or #Scheduler to restrict a Job to run in multiple instance

I have one #Scheduler job which runs on multiple servers in a clustered environment. However I want to restrict the job to run in only in one server and other servers should not run the same job once any other server has started it .
I have explored Spring Batch has lock mechanism using some Database table , but looking for any a solution only in spring task:scheduler.
I had the same problem and the solution what I implemented was a the Lock mechanism with Hazelcast and to made it easy to use I also added a proper annotation and a bit of spring AOP for that. So with this trick I was able to enforce a single schedule over the cluster done with a single annotation.
Spring Batch has this nice functionality that it would not run the job with same job arguments twice.
You can use this feature so that when a spring batch job kicks start in another server it does not run.
Usually people pass a timestamp as argument so it will by pass this logic, which you can change it.

When spring-boot app multi-node deploy, how to handle cron job?

When I use spring task handle a simple sync job! But when I deploy multi-node, how I make sure the cron job just run one time.
Maybe you say that:
1. Use distributed-lock control a flag before the crob job run.
2. Integrated quartz cluster function.
But I hope spring task #EnableScheduling can add a flag argument, so as we can set a flag when launch app.
We are using https://github.com/lukas-krecan/ShedLock with success, zookeeper provider in particular.
Spring boot, in a nutshell, doesn't allow any type of coordination between multiple instances
of the same microservice.
All the work of such a coordination is done by the third parties that spring boot gets integrated with.
One example of this is indeed a #Scheduled annotation.
Another is DB migration support via flyway.
When many nodes start and the migration has to be done, flyway is responsible to lock the migration table by itself, spring boot has nothing to do with it.
So, bottom line, there is no such support and all options that you've raised can work.

Spring Boot & Batch Schedule

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

Resources