Spring Boot & Batch Schedule - spring

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

Related

SpringBoot shedlock Without time Interval

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.

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.

Couple of questions about spring quartz

I'm thinking of building spring quartz into my spring mvc web application. I have a few questions about it that I could not properly find an answer.
If I want to use cron triggers for spring quartz does quartz execute the job using java system time or the operating system time?
I am planning to have a properties file to hold all of my cron triggers. If someone goes and changes a cron trigger for an ad-hoc execution of a job will quartz automatically pick up the changes in the file? Or is there a way for me to tell quartz how to do this if it is not default behavior.
I have been reading about spring batch admin console recently. Sounds like a nice gui tool to reschedule jobs. Can it be used to make ad-hoc changes to crontab triggers? Or is there another gui tool I could use to manage the job triggers?
thanks in advance
Quartz
Quartz uses custom thread scheduler (org.quartz.core.QuartzScheduler) which use java system time. It can integrate commonj interface to be JEE (WAS and Weblogic) interoperable.
Reload configuration: read Quartz: How to reload jobs and triggers with org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin?
Spring batch admin console is for spring batch and is bale to monitor batches activity
Reload configuration Using API
Generally speaking you can use quartz API programmatically (I use them):
JobDetail job = new JobDetail();
job.setName("myJob");
job.setJobClass(MyJob.class);
CronTrigger trigger = new CronTrigger();
trigger.setName("myTriggerName");
trigger.setCronExpression("0/30 * * * * ?");
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
these APIs provide you fine control.
Reload configuration JMX way
You can control the Qurtz scheduler through RemoteMBeanScheduler:
An implementation of the Scheduler interface that remotely proxies all method calls to the equivalent call on a given QuartzScheduler instance, via JMX.

Resources