I'm looking for a cron scheduling system with the possibility to 'spread out' tasks. Lets say I need to do a bit of work at 'round about' 12 o clock. The 'round about' is defined as lets say +/- 30 minutes. I need such a thing for my automated update task to not have all my millions of customers (joking...) downloading updates at exact the same time and just blowing up my webserver...
I know little about the existing cron libraries in java. I know there are cronj or quarz and dozen other systems - but I can't find out if one of them is able to do what I'm looking for.
At least I can implement such a 'spreading' feature (don't know how to call it better) myself by using a normal cron job and trigger a new Plain-Old-TimerTask with a little bit of Math.round() thingy... But would be nice if there is a native possibility for this.
Thanks and greetings
This looks being easy to implement on your own.
Create a worker thread which gets a random timer, sleeps and wakes up.
Related
I am using the Quartz Scheduler (version 1.8.3 due to project constraints) and I as assigned the task of creating an "MS Outlook-like" scheduler for Jobs specific to my project. Everything seems fine to work fine but I have a really huge problem with CronTriggers (this problem also exists in version 2.1 of Quartz):
I am using CronTriggers for recurrence patterns of DAILY, WEEKLY and MONTHLY. In addition to the recurrence pattern, I also provide an option for 'No. of occurrences'. This has become the bane of my life! CronTrigger does not provide an option for 'repeatCount' like SimpleTriggers do (bug: https://jira.terracotta.org/jira/browse/QTZ-242?page=com.atlassian.jira.plugin.system.issuetabpanels%3Achangehistory-tabpanel). Apparently this may be fixed in version 2.2 but I cannot wait that long nor do I believe my problem is unique!
A few options that I deemed worthy of investigation:
Calculate the 'EndTime' for the CronTrigger but using my own logic - this fails to cover all possible cases and is only approximate at best even for simple cases.
Use a TriggerListener or JobListener to keep track of no. of iterations of the job since I just need the job to stop after 'N' iterations and I have a 1:1 mapping from Job instance to Trigger. This does not seem very feasible and/or efficient by any stretch of the imagination.
Could any of you who have used CronTriggers with the option of 'No. of occurrences' please give some insights on how to solve this conundrum?
It seems that Quartz have implemented something that can help: TriggerUtils.computeEndTimeToAllowParticularNumberOfFirings.
I haven't tested it yet, but this is the code I have wrote for now:
CronTrigger trigger = newTrigger()
.withSchedule(cronSchedule(cronExpression))
.build();
Date endDate = TriggerUtils.computeEndTimeToAllowParticularNumberOfFirings((OperableTrigger) trigger,
new BaseCalendar(Calendar.getInstance().getTimeZone()), 10);
trigger = trigger.getTriggerBuilder().endAt(endDate).build();
If this won't work, then as said here and here, you can't set a repeat count, and you should use TriggerListener.
In any case, version 2.2 doesn't have this feature.
Update
I've tested it, and it works.
Wy dont't you instead use the Simple Trigger? You would have the additional taks of calculating the time interval at the time of scheduling the job, but that will be a one time activity.
I'm about to create a small application which will be responsible for sending out various reports to various users at various intevals. We might be talking about 50 or 100 different reports going to different people. Some reports needs to be generated every day, some every week, and some every month.
I've been using the Quartz library earlier to run tasks at regular intervals. However, in order to keep things simple I like the thought of having a single Quartz thread taking care of all reports. That is, the thread should loop through all reports, say every 15 minutes, and determine wether it is time for one or more to be generated and sent. It does not matter if a report is generated at 12:00 or 12:15.
I'm thinking about wether it would be possible, somehow, for each report to set up specific times such as "mon#12:00,wed#12:00" or "fri#09:30". Then, based on that, the thread would determine if it was time to send a report or not.
My question is; has anyone else done something like this and does any libraries exist which can make it easy to implement this task?
why not simply register a separate quartz task instance for each report and let Quartz handle all the scheduling for you? That is after all the point behind it.
you can create just single thread and it would ping a "job schedule data structure" at some time interval to see if it needs to run a report. If yes, it would run the report, otherwise, it would go for a short nap and ping again after specified sleep time.
It will cause problem if one job takes too much time to complete and you start accumulating jobs.
The job schedule data structure would keep its record sorted by time stamp.
I'm using a ScheduledExecutorService to provide an update to a database every hour with the scheduleAtFixedRate method. The problem is that it gradually gets later - in long service I've been logging it and it's about a second a day.
I made a small class just to examine this aspect - seems to work fine when nothing is happening on the PC ( running WinXP ) but if things are going on it rapidly gets later. 18:00:00.5 last night was its first log and this morning was 09:00:00.5 then 10:00:05.9, 11:00:26.8, 12:00:45.3, 13:01:07.8...
I can attach the code although my example isn't the smallest.
Anyone else experienced this? Any ideas why this isn't working properly?
I can think of lots of ways around it but I'd really like to know why it doesn't work as advertised!
Thanks, Mike
This is normal AFAIK. With scheduleAtFixedRate, If any execution of this task takes longer than its period, then subsequent executions may start late. That being said, I'd recommend scheduleWithFixedDelay. This will ensure that tasks are carried out at the specified delay interval.
I am thinking how to build a real-time system using java without Sun Real-time System API.
Say, a boss generates an event at 11:00 am, he has to get feedback at 11:10 am. If no any feedback, he will resend his event.
The staff gets his boss's event at 11:01 am, he has to leave 1 minutes for sending his result back to his boss. So actually he has 8 minutes to do his job. At 11:09 am, he has to send a feedback no matter he finished it or not.
This is a real-time system, isn't it ?
In this case, how to design our system using java ? This is a producer-consumer pattern. In the consumer side, use which object (blockingqueue, delayqueue ...) to meet this requirement ?
Any weblink, open source will be welcome.
Thank.
You cannot do real-time programming in the real computer engineering sense in Java. You are at the mercy of a thread scheduler and an operating system with totally unknown underlying properties. If the OS felt like waiting until 11:20 until it got back around to giving the JVM some CPU time, that's its business.
If you mean "realtime" in the Microsofty way as in "Things respond really really fast and we're careful never to block the main UI thread" that doesn't have a well defined technical meaning. It just means "architecture user facing code to give the appearance they don't have to wait on the computer."
--edit in response to comment.
If by 11:08 you mean 'between 11:07:59 and 11:08:01' then regular java can generally do that for you on a modern platform with careful programming. What it can't deliver is a situation where the event happening at 11:08:01 is considered a platform defect, it just doesn't make that guarantee.
When we say 'real time' and what the RTS API is for, is a situation more like "The bonding head must be at these coordinates at exactly this millisecond, if it's more than half a millisecond late, the part will be defective, and if it's more than 2 milliseconds early, a $300,000 servo table is going to crash into its bearings and cause a $10,000,000 assembly line outage."
The system you described can be solved with JMS.
Use a pub-sub JMS queue to assign the work. The "boss" is the publisher. Each member of the staff is a "subscriber".
The "boss" will need to store each message it publishes in a "check back" area (perhaps a list) and set a timer for 10 minutes. When it gets a response to a message, it will clear the timer and remove the message from the "check back" area.
I am building an app for which I need to set up cron jobs. What I want to do is to set the specific minutes in a hour where specific crons should run. For instance:
Task1 at 1st minute of the hour
Task2 on every second minute of the hour
Task3 every 2 minute only in the second half of the hour
Building this in the standard Unix cron format is reasonably straightforward, but could not figure out how to do it in the Google-App-Engine.
The documentation does not list any non-trivial examples. Any suggestions on how to do it? Examples would be excellent.
The documentation you linked to seems to indicate that it isn't possible to do what you want using only Cron for Java (unless they have an undocumented feature for it). In particular this doesn't appear to allow for multiple times.
time specifies the time of day, as HH:MM in 24 hour time.
The Python version says the exact same thing.
However, one solution (albeit somewhat more expensive in terms of CPU usage) would be to call a URL every minute, and from the handler for that URL, dispatch out to whatever other calls you need.
In other words, something like:
<?xml version="1.0" encoding="UTF-8"?>
<cronentries>
<cron>
<url>/run-scheduled-tasks</url>
<description>Run all scheduled tasks</description>
<schedule>every 1 minutes</schedule>
</cron>
</cronentries>
Then in run-scheduled-tasks, check a database for when each task last run, and if your complex condition for triggering them has occurred since then.
If the documentation is correct you can't get as granular as you are wanting. Doesn't look like they support picking a particular minute of the hour. Or a subset of an hour.
You might have to get creative. Why do you need such specific timing?
This may seem silly. Write three servlet. And schedule them from another UNIX machine on the other part of the world :D. Or even you can write a java app to do it. enjoy
Have a look at Quartz and see if that'll solve your problem.