How to adjust the quartz scheduler timing? - java

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.

Related

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

Scheduled task to run every 3 days at given time

I am using Quartz 2 scheduler for scheduling tasks. Below is what I need.
schedule the job when the user click on a button.
The job should be scheduled to run at 8 AM, every 3 days.
Below is how I coded it. It will execute (with its scheduler of course) once the user click on a button in web app)
Trigger passportTrigger1 = newTrigger()
.withIdentity(passportTriggerKey1)
.withSchedule(simpleSchedule().withIntervalInHours(3*24).repeatForever())
.startAt(todayAt(8,0,0)).build();
However you can see that I have used startAt(todayAt(8,0,0)). What happens if the user turned on the scheduler after 8.00 AM today? The job will be scheduled immediately or it will never get scheduled?
You may also check the current time: if it's before 8am startAt(todayAt(8, 0, 0) else startAt(tomorrowAt(8, 0, 0)).

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.

about misfire in quartz -scheduler

I am using quart library for scheduling in java. I have written a trigger like this
trigger = newTrigger().withIdentity("myTrigger", "myGroup").startNow() .withSchedule(cronSchedule(croneExpression).withMisfireHandlingInstructionFireAndProceed()) .forJob("myJob","myGroup") .build();
Now when i set the scheduler with cronexpression which is dynamically generated scheduler runs properly. But when server is shutdown during the period in which scheduler is set to fire , the values in quartz_trigger i.e. the next_fire_time in table is changed after the execution time of the job.Because of this the misfire of the scheduler do not work.So my purpose is not solved.So what can be the problem? Is any property in the quartz.properties to be set. I am not using job.xml(jobInitializer) to set the scheduler.
the values in quartz_trigger i.e. the next_fire_time in table is changed after the execution >time of the job
Yes, this is quartz actually does in case of misfires.
As per the misfire instruction provided while creating a trigger, quartz calculates how many times the misfired execution has to be executed.
As per your code , you have set the misfire instruction as "fireAndProceed" , So Quartz just executes the very first misfired execution ( and neglect all the subsequent remaining misfires). Ex: If you set the trigger to fire between 2Pm to 4pm with interval of 30 min and if scheduler was down during 2:29pm to 3.29pm, then only one trigger execution of time 2.30pm
will be executed ( and executions of 3.pm will be neglected).
Hope this answers your question. :-)

Quartz StatefulJob / non-StatefulJob

Would you please explain to me the exact mean of the StatefulJob in quartz and it's difference with none StatefulJob?
StatefulJob interface, provides 2 things,
first: only one job will be run any time
second: in (SimpleTriggerBean) you will not worry about your job running duration. it means that the next run will be done after delay time after ending of previous one.
StatefulJob guarantees only one job will be running at one time. For example, if you schedule your job to run every 1 minute, but your job took 5 minutes to complete, then the job will not be run again until the previous job has completed.
This is useful to make sure there is only one job running at any given time.
The next job will be run on the next schedule, not immediately after the previous job completed.
jobDetail.getJobDataMap().put("type","FULL");
This line is will decide we are using statefull or non-statefull.
If we are passing the argument then it will be statefull.
With out statefull there is no way to pass the arguments in execute method
In state full while execution time if we modify any value then the execution job will be lost it wont re-triggered at simultaneous process time.
Only one job will execute at a time the second will be sleep until the first one is completed.
In multi scheduling process the second job argument will be share to first job at run time. this is one type of disadvantage in multi scheduling process.

Categories

Resources