Spring scheduled task after server reboot - java

I'm working with #Scheduled annotation in Spring 3. I need to run some tasks every week or every two weeks, so i'm using cron expression as parameter e.g.
#Scheduled("0 0 2 */7 * *")
My question is if i will create scheduled task that must run every 7 days and on the 6-th day i will restart server (with war redeploy) will it reset this scheduled task (and i need to wait 7 days again) or it saves its state and will trigger this task on 7-th anyway?

I'm pretty certain it won't survive a JVM restart.
If you want the job to run every seven days you're probably better scheduling it from cron ( or similar external scheduling mechanism ) rather than getting Spring to do it.

Not sure what */7 means but I'm sure that 1/7 in the following cron means:
Fires at 2am every 7 days every month starting on the first day of the month
#Scheduled("0 0 2 1/7 * ?")

Related

Cron expression for biweekly scheduler job on Monday in Windows 10 using Java

I have created a scheduler with two triggers to implement a biweekly scheduler Java program on Windows 10 using Cron expression. The two jobs are executed individually one each on alternate Monday of every month at 2pm, so maybe on even alternates or odd alternates. The current Cron expressions in use are as follows -
"0 0 14 ? * MON#1"
"0 0 14 ? * MON#3"
I want to implement both these jobs with a single trigger and thus a single Cron expression, but not able to club these two Cron expressions. Need help!
Quartz cron triggers do not support multiple "Nth" days.
However, if you do not mind, you can use a CalendarIntervalTrigger that is more suitable for this type of a schedule. The following screenshot (from QuartzDesk GUI) shows you a CalendarIntervalTrigger with settings that cover your use-case.

Kafka Streams - Processor schedule punctuator by cron expression

ProcessorContext.schedule(..) accepts duration interval parameter, and we could specify that some action (punctuator) will be executed every X time interval (e.g. every hour). With such scheduling, execution time depends on the last application restart time. And additionally, if we have multiple instances of application, and only app on single node was restarted (e.g. due to some prod issue), punctuators will be executed at absolutely different times on each node. For some use cases it might be not applicable, and it's needed to execute punctuator by cron expression, like with "0 0 * * * ?" execute every hour, approximately at time XX:00:00.
Is it possible to schedule punctuator by cron expression? or does exist any workaround?
As a dirty workaround, we could calculate time interval from now till upcoming first execution of cron inside Processor.init(..), and on first triggering of punctuator, cancel existing scheduling and create a new one, converting from cron expression to duration instance. seems such workaround is error prone.

What is the best way for softcoding schedule tasks in Spring Boot?

In my Spring program, I have a Scheduled task.
#Scheduled(cron = "0 0 0 2 * *") // hardcoded schedule
public void executeBatchJob() {
batchJob.execute();
}
I have a specification change and now have to let the user freely configure the date and time of execution via an API.
One way I came up was to run a scheduled task every morning at 0:00 and check if the date is indeed the date of execution. If true, check the time of execution and schedule the batch job to run at that time of the day.
Is there a "Spring" way of achieving this?
Triggers can be used to configure the scheduled jobs.
From the docs
The basic idea of the Trigger is that execution times may be determined based on past execution outcomes or even arbitrary conditions.
Check out this answer for detailed explanation.

How to configure a thread that execute on a given time

I am making a meeting broadcast application which will broadcast the message to the participant of meeting.
There is a meeting data checker thread which execute after 5 minute.
I have done this using this code :
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
scheduledExecutorService.scheduleWithFixedDelay(new DataAccessSchedule(), 0, 5,TimeUnit.MINUTES);
Now the in DataAccessScedule the query get the data of meeting which are expected to be started after 15 minutes,so I get the data before 15 minutes and the time in table are: 01:45 ,... 01:50 , ... 01:52 ,..
so i have calculated the time of all in millisecond by taking difference from current time so now the time in milis are x,y and z.
I want to fire the thread on x , y and z time and these thread destroy itself after execution.I do not want to run scheduler here like above did using Executors.
Please tell me how should i do that ?
You need a scheduler library for that, for example Quartz Scheduler, that supports cron expressions to specify exactly when the task should be launched.
You can also configure it to run the task finite number of times.
An alternative for writing sheduler library would be to write the similar functionality yourself. You'll need one task running periodically, with the precision you require (it could be 1 minute, but also 1 second) and checking all entries in the data structure, if their start condition matches current date (for example, by matching the date/time against cron expression). It would be, however, reinventing the wheel so don't do as long if you don't want some extra funktionality the existing libraries doesn't provide.

How to adjust the quartz scheduler timing?

I am using quartz scheduler for scheduling a particular job. I am using both trigger types simple and cron. I have set the interval time as 4 minutes for testing purpose.
If the first run takes 2 minutes to execute then second run starts after 2 minutes the first run finishes. This should not happen. In this case interval time gets reduced to 2 minutes. This should not happen. The second job should start 4 minutes after the first job finishes. Is there any way to do this.
You should schedule the job once with 4 minutes delay, after job completes (use job listener to catch the moment), reschedule it again at the same 4 minutes interval.
Also, if it is not necessary to use the quartz, it could be enough to use java.util.concurrent.ScheduledExecutorService.

Categories

Resources