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)).
Related
I have an UI Interface where user can define Job name, interval, active/Inactive etc.
How we can achieve this with Quartz Scheduler or any java/Spring api ?
Ex. Suppose any Quartz job is started and interval is set as 10 min, So in ideal case job will run in next 10 min interval. But every time job runs we want to fetch the latest interval from database and schedule it.
10:00 Job runs and in the database interval is set to 10 min
10:10 Job runs and in the database interval is set to 20 min
So next time job should run at 10:30
If you use Quartz, you can implement a custom Trigger. Your implementation would lookup the value in the database and return when the next time the run should happen in the getFireTimeAfter.
Another option is to use Spring Scheduling APIs and implement the Trigger interface. Same here, the nextExecutionTime method would decide when the next run should happen.
The advantage of using a custom implementation is that you have full control over the triggering logic (like in your case, do a lookup in the database and dynamically set the next run time).
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 * ?")
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.
I use appengine (java) as a backend for a mobile app (android). The user of my app can create public events consisting of a title and a date/time. Those events are stored on my appengine backend. Any user can subscribe for events and will receive a push notification at that time the events starts.
So I want to schedule a job / task on appengine to run at the events date/time to send a push notification to all subscribers.
Example:
User A creates an event that will start on Saturday next week on 8 pm. User B and User B subscribe for this event.
On Saturday 8 pm a job/task should start to send the push notifications to User B and User C to inform that the event has started.
My question is:
How do I implement something like this in an efficient way on appengine? I want to say, start a Task for the Event on Saturday at 8 pm. There are Cronjobs and TaskQueues. Cronjobs can not be created programmatically. TaskQueue needs to be pulled and can not be scheduled to pull at a given date / time, right?
So the only solutions I see is to create a cronjob that will run every minute to check if there is a Event that starts right now.
An event can be created at any time and any day in the week. However the most events are created for the weekend and There are days when no event has been created for. So running a cronjob periodically ever minute is very inefficient. Im looking for a smarter solution, any ideas?
Indeed, based on your description, the solution is to create a cron job that checks for new events that start right now.
However, you should be careful not to exceed the 60 second window you have for each cron job. If you have a lot of events, you probably should move the actual processing from the cron job to background tasks using Push Task Queues.
You may take a look to this post for a combination of cron jobs and Task Queues.
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.