spring framework schedule annotation problem - spring

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.

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.

How to run a Spring Boot application for exactly 24 hours?

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>

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

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.

How to make JobDetailBean run non concurrently?

I am running some scheduled jobs using the Quartz Scheduler. All the jobs in my application are JobDetailBean jobs.
I know that using MethodInvokingJobDetailFactoryBean, we can make a job non-concurrent like this:
<property name="concurrent"><value>false</value></property>
I wonder how we can make the JobDetailBean jobs non-concurrent. JobDetailBean jobs run concurrently by default.
The requirement is: jobA needs to run every 5 min but only if the previous instance of it has completed.
Thanks,
Shikha
Your job class needs to implement StatefulJob, or alternatively, if you are using Quartz 2.x, be annotated with #DisallowConcurrentExecution.
This is how concurrent execution can be controlled in Quartz, and in particular MethodInvokingJobDetailFactoryBean just uses different job classes depending on the value of concurrent.

Resources