For a system monitoring Java application which currently runs on the command line and uses ScheduledExecutorService, I would like to write a simple web application version, to be run in a Servlet container like Apache Tomcat or Eclipse Jetty.
I have read about Quartz as one of the popular job schedulers for web applications. Would it be better (maybe because of better servlet container integration) to port this application from ScheduledExecutorService to Quartz?
Adding another library dependency to the application is not a problem, I am interested in technical reasons against usage of ScheduledExecutorService.
It depends on what you are using it for.
Quartz is useful for programmed times e.g. every hour on the hour.
ScheduledExecutorService is useful for repeating tasks which don't have to occur at a specific time. Its simpler and possibly more efficient. If you have this working it indicates to me that you don't need Quartz.
ScheduledExecutorService operates at a lower level and you'd have to implement all scheduling monitoring/maintenance facilities yourself.
Quartz has tons of facilities such as Job Persistence, Transactions, Clustering etc.
Java's Executor solution allows you to either:
immediately run a task
start a task after an initial delay (and optionally rerun the task after subsequent delay cycles).
But Quartz empowers you with incredible flexibility on when and how often to run a task/job. For example, one schedule during the Mon-Fri work week and something else (or not at all) during the weekends. Or on the last day of the month and you don't have to figure out if a given month's last day is on the 28th, 29th, 30th, or 31st. Here's some more examples of the flexibility the cron style scheduling accommodates - http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/crontrigger.html#examples
Using Java's library is easier but for anyone that wants a jump start into a bare-bones codebase example of Quartz working, I've put this template together for free download usage - https://github.com/javateer/quartz-example
Related
I did an application to do some testing on network nodes like ping test, retrieve disk space ans so on.
I use a scheduled batchlet to run the actions but I wonder if it is the rigth use of batchlet?
Does an EJB timer should be more relevant? Also, when I run a batchlet, my glassfish server keeps a log of the batch job and I don't necessary need it (especially with the amount of batch jobs genereted during a day).
If I need to run some job in the same schedule time, I think batchled can do it but EJB timer too?
Could you give me your input on the rigth way to achieve this?
Thanks,
Ersch
This isn't a question with a clear answer, but there is a bit of a cost in factoring your application as a batch job, and I would look at what I'm getting to see if it's worth doing so.
So you're thinking about a job consisting of a single Batchlet step. Well, there'd be nothing gained from "restart" functions, neither at the failing step within a job nor leveraging checkpoints within a chunk step. The batchlet programming model is quite simple... even if you really like #BatchProperty you'd have to deal with an XML now to do so.
This only starts to get more interesting if you want to start, view, and manage these executions along with the rest of your batch jobs. This might be because you're working with an implementation that offers some kind of implementation-specific add-on function. An example of this could be an integration with external scheduler software, allowing jobs to be scheduled by it. At the other extreme, if you found value in having a persisted record of all your batch job executions in one place (the job repository, usually a persistent DB), then that could also make this worthwhile for you.
But if you don't care for any of that, then an EJB timer could be the way to go instead.
Using an EJB timer is appropriate when your task executes in an eye blink (or thereabouts).
Otherwise use the batching mechanism.
Long running tasks executed from EJB timers can be problematical because they execute in transactions which normally time out after a short period of time. Increasing this transaction time out also increases the chances of database and perhaps other resource locks which can impact normal operation of your application.
In my application I need to have periodically run background tasks (which I can easily do with Quartz - i.e. schedule a given job to be run at a specific time periodically).
But I would like to have a little bit more control. In particular I need to:
have the system rerun a task that wasn't run at its scheduled time (i.e. the server was down and because of this the task was not run. In such a situation I want the 'late' task to be run ASAP)
it would be nice to easily control tasks - i.e. run a task on demand or see when a given task was last run or reschedule a given task to be run at a different time
It seems to me that the above points can be achieved with Spring Batch Admin, but I don't have much experience in this area yet. Also, I've seen numerous posts on how Spring Batch is not a scheduling tool so I'm becoming to have doubts what the right tool for the job is here.
So my question is: can the above be achieved with Spring Batch Admin? Or perhaps Quartz is enough but needs configuring to do the above? Or maybe I need both? Or something else?
Thanks a lot :)
Peter
have the system rerun a task that wasn't run at its scheduled time
This feature in Quartz is called Misfire Instructions and does exactly what you need - but is a lot more flexible. All you need is to define JDBCJobStore.
it would be nice to easily control tasks - i.e. run a task on demand or see when a given task was last run or reschedule a given task to be run at a different time
You can use Quartz JMX to access various information (like previous and next run time) or query the Quartz database tables directly. There are also free and commercial management tools basex on the above input. I believe you can also manually run jobs there.
Spring Batch can be integrated with Quartz, but not replace it.
I have a Java EE application which downloads stock prices from the internet every fifteen minutes. From a timing accuracy perspective is is best for the application to internalise this periodic operation i.e. use Thread.sleep() in combination with a counter or set up a timer Or would it be better to expose the task via a URL and have a platform cron job hit the URL periodically (at the the required frequency of course).
What are the pros and cons of both approaches?
I've been spooked by a timer bug I saw reported against the OpenJDK implementation. The bug stated that changes in the system time affected the operation of the time related operations and methods such as sleep and timer periodicity.
Timer is not deprecated, but a better alternative now exists: SheduledExecutorService. One of what makes it better is that it uses relative time rather than absolute time for scheduling.
Using an external cron script or an internal timer is just a matter or preference, IMHO. An internal timer is easier to setup, but if you already have other crons in place, you might want to use an additional one and have this responsibility in a single place.
Definitely consider using a scheduling job that was built for the task. Try and split the task itself from any timing considerations.
As was suggested, Quartz is a good choice.
Cron is not bad, but would require more setup to integrate with your
task.
If on Java EE you can use an EJB timer.
You can roll your own with ScheduledExecutorService (not recommended)
Thread.sleep is not a recommended way to execute code periodically. It's inaccurate and usually a sign of bad design. I suggest you use the Timer class to easily schedule the execution of code periodically.
I have a general question related to the quartz scheduling framework:
I need to perform a task after a fixed amount of time after a user registration. For the sake of simplicity let's say exactly 1 hour after registration of a user in my system. The job MUST be done, even if the system is restarting during this one hour the task must be remembered and it MUST be performed later if my system is down at the usual time.
Is this something where I can or where I would use Quartz? I looked at persistent jobs which looks quite promising but I am not sure if this will still work out for 1000 jobs a day. Furthermore, I am not sure about the performance implications. Maybe someone can help me with information here.
If Quartz is not the right choice, which other ways/frameworks do you see for this issue? My application is a Java 6/Spring 3 based Web-App.
Thanks for your help!
We are using quartz persisted job store successfully in our production environment for a SaaS platform application where 100s of jobs are running.
I'm looking for an effective way to execute a method everyday at 3PM regardless of when the application was initially run or how long it has been running.
This must be done entirely from the application with no OS intervention (ex. Windows Task Scheduler)
I have been experimenting with java.util.Timer in varies configurations but I have had no success.
Any help would be appreciated.
Thanks.
You should take a look at Quartz which is a Java-based job scheduling system.
You will probably want to use something like the quartz engine it can do things like execute tasks that missed (like during a ahem crash) and it takes the work out of trying to manage threads.
For example if you use threads and put it to sleep and wake it up 86400 seconds (one day) later you will wake up and hour late (day = 82800 seconds) or early (day = 90000 seconds) on DST change over day, so be careful with whatever solution you choose
A built-in JDK way is to do what others suggested and first calculate :
currentTime - desiredTime
Then you can use something like a schedule executor to submit the tasks, and run them with a particular delay. This is far simpler than the options you have with frameworks like Quartz, but doesn't require an external dependency.
Also, you should always list which JDK you're using, so people can provide solutions for your version of the JDK.
You can start a thread that calculates the difference to the next 3pm and sleeps for that time. When it wakes up it executes the method and recalculates and sleeps. Is this what you meant?
As stated by others Quartz is a choice, with it you can do cron-like operations, jobs or triggers, here is a link on this subject: http://www.ibm.com/developerworks/java/library/j-quartz/index.html
Jcrontab
Jcrontab is a scheduler written in Java. The project objective is to provide a fully functional schedules for Java projects.