scheduling task execution in Java - java

How can I execute task in Java every x months?,
and how can I execute task in given days? (for example I want to execute my task every week in Sunday Tuesday and Saturday)
Thanks, Michal

You can do this thing easily with third party APIs. Quartz scheduler is one of it. Have a look over below links :
1) http://quartz-scheduler.org/
2) http://www.mkyong.com/tutorials/quartz-scheduler-tutorial/

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.

Spring scheduled task after server reboot

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 * ?")

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.

Java Executor in timerange at specific time

I need to call a task every x hour in a defined time range and repeat this every day.
My only idea is to calculate the executions times as delay from current time and start each in a seperated thread via scheduleAtFixedRate and a rate of 24 hours.
Do anyone has a better idea?
Regards
Use your machines in built task schedule to call a script and call your java program from that script.
You can use a library dedicated to schedule code: quartz
It use cron like syntax and can schedule Job implementations.

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.

Categories

Resources